PHP脚本内存泄漏问题 [英] PHP script memory leak issue

查看:71
本文介绍了PHP脚本内存泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从命令行运行下面的PHP代码.问题是,它的内存消耗远远超过了应有的水平.在我的一生中,我无法弄清楚内存的消耗位置.

I'm running the PHP code below from command line. The issue is, its memory consumption far more than what it should be. I can't, for the life of me, figure out where the memory is getting consumed.

for ($i=0;$i<100;$i++)  
        {
            $classObject = $classObjects[$i];                       

            echo $i . "   :   " . memory_get_usage(true) . "\n";
            $classDOM = $scraper->scrapeClassInfo($classObject,$termMap,$subjectMap);           
            unset($classDOM);           
        }

根据我的说法,在每次循环迭代之后,我的脚本所消耗的内存应大致保持不变. $scraper->scrapeClassInfo()消耗的任何内存都应在其成员超出范围时释放.

According to me, the memory consumed by my script should remain more or less constant after every iteration of the loop. Any memory consumed by $scraper->scrapeClassInfo() should be freed when its members go out of scope.

这是我得到的输出文件.为了简洁起见,我显示了输出的第10行:

This is the output file I get. For the sake of brevity, I'm showing every 10th line of the output:

0   :   5767168
10   :   12058624
20   :   18350080
30   :   24903680
40   :   30932992
50   :   37748736
60   :   43778048
70   :   49807360
80   :   55836672
90   :   62914560
97   :   66846720

Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 44 bytes) in /home/content/60/8349160/html/drexel/simple_html_dom.php on line 1255

最后,据我所知,$scraper->scrapeClassInfo()在做什么并不真正是罪魁祸首,但以防万一,这是代码:

Finally, as far as I can see, what $scraper->scrapeClassInfo() is doing should not really be the culprit, but just in case, here is the code:

function scrapeClassInfo($class,$termMap,$subjectMap)
        {
            $ckfile = tempnam ("/tmp", "CURLCOOKIE");
            $ckfile2 = tempnam ("/tmp", "CURLCOOKIE2");
            $ckfile3 = tempnam ("/tmp", "CURLCOOKIE3");         

            $termpage = $termMap[$class['termcode']];
            $subjectpage = $subjectMap[$class['subjectcode']];
            $classpage = $class['classlink'];

            //hit the main page and get cookie
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $this->mainURL);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_exec($ch);
            curl_close($ch);

            //hit the term page and get cookie
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
            curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile2);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $termpage);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_exec($ch);
            curl_close($ch);

            //hit the subject page and get cookie
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile3);
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile2);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $subjectpage);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_exec($ch);
            curl_close($ch);

            //hit the class page and scrape
            $ch = curl_init();              
            curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile3);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_URL, $classpage);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $result = curl_exec($ch);
            curl_close($ch);

            return str_get_html($result);
        }

在最后一行str_get_html()中调用的方法是简单HTML DOM解析器

The method called in the last line, str_get_html() is a member of Simple HTML DOM Parser

这很重要,这就是我调用脚本的方式:

Should it matter, this is how I am calling my script:

/usr/local/php5/bin/php index.php 2>&1 1>output

推荐答案

好的,解决了.显然,这是5.3之前的所有版本的PHP都存在的一个错误.将CURLOPT_RETURNTRANSFER设置为true会导致大量内存泄漏.

Alright, figured it out. Apparently, its a bug that all version of PHP prior to 5.3 suffer from. Setting CURLOPT_RETURNTRANSFER to true causes massive memory leaks.

我再次运行了脚本,这次调用了php 5.3二进制文件:

I ran the script again, this time invoking the php 5.3 binary:

/web/cgi-bin/php5_3 index.php 2>&1 1>output

输出文件显示为:

0   :   6291456
10   :   9437184
20   :   10747904
30   :   11534336
40   :   11534336
50   :   11534336
60   :   11534336
70   :   11534336
80   :   11534336
90   :   11534336
99   :   11534336
152.74998211861 sec

现在这就是我在说的!完全恒定的内存占用量.

Now that's what I'm talking about! Perfectly constant memory footprint.

这篇关于PHP脚本内存泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆