Laravel 4 using nikic phpparser : Going out of memory when sending email

半世苍凉 提交于 2019-12-12 10:43:29

问题


Just learned that Laravel using nikic phpparser internally.

I modified my code to sending emails on one of the conditions & it started dying.
The PHP logs showed this :

[Sat Oct 03 21:18:23 2015] [error] [client xx.xx.xx.xx] PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 1048576 bytes) in /home/yyyy/public_html/vendor/nikic/php-parser/lib/PHPParser/NodeTraverser.php on line 66, referer: http://yyyy.com/home

Temporarily I've increased the memory to resolve the issue.
But, I want to move away from the band-aid.
I see that the NodeTraverser function is doing a clone, would that cause the problem :

   protected function traverseNode(PHPParser_Node $node)
   {

    ini_set('memory_limit', '64M'); // temporary fix
    $node = clone $node;

    foreach ($node->getSubNodeNames() as $name) {
        $subNode =& $node->$name;

        if (is_array($subNode)) {
            $subNode = $this->traverseArray($subNode);
        } elseif ($subNode instanceof PHPParser_Node) {
            foreach ($this->visitors as $visitor) {
                if (null !== $return = $visitor->enterNode($subNode)) {
                    $subNode = $return;
                }
            }

            $subNode = $this->traverseNode($subNode);

            foreach ($this->visitors as $visitor) {
                if (null !== $return = $visitor->leaveNode($subNode)) {
                    $subNode = $return;
                }
            }
        }
    }

    return $node;
}

This is how I'm sending the email. This is no different than anywhere else, hence I doubt this would cause an issue :

                 $this->mailer->queue('emails.forreg',
                        [
                            'toName' =>  $toEmailName,
                            'fromName' =>  $user->username,
                            'site_name' => \Config::get('site_title')
                        ],
                        function($mail) use($toEmailAddress, $user, $subject_to_send, $toEmailName)
                        {
                            $mail->to($toEmailAddress, $toEmailName)
                                ->subject($subject_to_send)
                                ->from('xxx@yyy.com', $user->username);
                        }
                    );

Any ideas on how to resolve this ?


回答1:


You simply have an extreme low memory limit. IIRC PHP limit starts at 128M for a default. When the parser goes through it's building up a node for every single part of the code. Nothing is excluded and there's no easy hot fix.

Memory is cheaper than ever today and this problem is unlikely to be fixed because of the upcoming PHP7. Perhaps give that a try as it's likely to have a smaller memory footprint anyways.



来源:https://stackoverflow.com/questions/32927948/laravel-4-using-nikic-phpparser-going-out-of-memory-when-sending-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!