PHP内存不足 – 崩溃的Apache?

我正在运行PHP版本5.3.0和Apache:2.2.11

当我运行消耗大量内存(我认为)的PHP脚本 – 大循环等我的Apache Web服务器报告崩溃?!

[Sat Jan 02 00:51:30 2010] [notice] Parent: child process exited with status 255 -- Restarting. 

我需要增加内存吗? 我目前有内存设置

 memory_limit = 512M 

PHP没有抱怨,所以我在想其他的东西?

谢谢大家

更新

我的Windows机器在事件查看器中logging了这个错误:

错误应用程序httpd.exe,版本2.2.11.0,时间戳0x493f5d44,错误模块php5ts.dll,版本5.3.0.0,时间戳0x4a4922e7,exception代码0xc0000005,错误偏移量0x00083655,进程ID 0x1588,应用程序启动时间0x01ca8b46e4925f90。

更新2

有问题的脚本。 我已经移除了url

 <?php error_reporting(E_ALL); set_time_limit(300000); echo 'start<br>'; include_once('simple_html_dom.php'); $FileHandle = fopen('tech-statistics3.csv', 'a+') or die("can't open file"); for($i =1; $i < 101; $i ++){ // Create DOM from URL $html = file_get_html("http://www.x.com/$i"); foreach($html->find('div[class=excerpt]') as $article) { $item0 = $article->children(1)->children(1)->children[0]->plaintext; $item1 = $article->children(1)->children(1)->children[0]->plaintext; $item2 = $article->children(1)->children(0)->children(0)->children(0)->plaintext; //$item3 = $article->children(1)->children(0)->children(0)->children[1]->children(0)->next_sibling(); $stringa = trim($item0).",".trim($item1).",".trim($item2)."\r\n"; fwrite($FileHandle, $stringa); echo $stringa.'<br>'; echo '------------>'.$i; } } fclose($FileHandle); echo '<b>End<br>'; ?> 

更新3

我正在使用PHP简单的HTML DOM分析器,我刚刚发现这一点:

http://simplehtmldom.sourceforge.net/manual_faq.htm#memory_leak

我想我应该清除内存,否则会崩溃。 现在testing。

UPDATE4

是的,这是一个内存泄漏! 🙂

由于没有关闭在for循环中反复使用的资源以及使用递归的脚本导致内存泄漏,Apache崩溃了。

感谢所有的帮助。

我今天在循环中解析了大量的HTML时遇到了这个问题。 这是简单的修复:

 $dom = file_get_html("http://www.x.com/$i"); ... // parse your DOM $dom->clear() // clear before next iteration 

只要在每次迭代完成时调用dom对象上的clear()方法就可以解决这个问题。

晚会有点晚,但是在使用Simple HTML DOM时,我遇到了Segmentation Fault的问题。 我确保我使用$ html-> clear(); 和未设置($ html); 清除我已经加载的HTML,但我仍然收到大量的数据中的分段错误错误。

我发现我需要在simple_html_dom.php文件中注释一些清晰的代码。 在simple_html_dom_node类中查找函数clear(),并注释掉里面的所有内容;

  function clear() { // These were causing a segmentation fault... // $this->dom = null; // $this->nodes = null; // $this->parent = null; // $this->children = null; } 

我遇到过这样的问题,在Windows上使用PHP和Apache。 而不是向屏幕报告任何有用的信息,这个过程就会消失。 如果可能的话,我会建议在Apache和PHP的Linux机器上运行你的代码。 根据我的经验,这个组合通常会把这个报告为一个内存错误,而在Windows上,似乎没有任何事情发生。 顺便说一下,我只看到了这种递归的情况。

你不使用递归? 我已经看到了PHP bug,并且在发生无限递归时,杀死了Apache子进程而没有任何有用的日志记录输出。