简体   繁体   English

PHP内存不足 - 崩溃Apache?

[英]PHP Out of Memory - Crashes Apache?

I am running PHP version 5.3.0 and Apache: 2.2.11 我正在运行PHP版本5.3.0和Apache:2.2.11

When I run PHP scripts that consume a lot of memory (I think) - large loops etc. My Apache web server reports a crash?! 当我运行消耗大量内存的PHP脚本时(我认为) - 大型循环等我的Apache Web服务器报告崩溃了?!

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

Do I need to increase memory somewhere? 我需要在某处增加记忆吗? I currently have memory set to 我目前有内存设置

memory_limit = 512M 

PHP hasn't complained about this so I am thinking its something else? PHP没有抱怨这个,所以我在想其他的东西?

Thanks all 谢谢大家

Update 更新

This error has been logged by my windows machine in the event viewer: 我的Windows机器在事件查看器中记录了此错误:

Faulting application httpd.exe, version 2.2.11.0, time stamp 0x493f5d44, faulting module php5ts.dll, version 5.3.0.0, time stamp 0x4a4922e7, exception code 0xc0000005, fault offset 0x00083655, process id 0x1588, application start time 0x01ca8b46e4925f90. 故障应用程序httpd.exe,版本2.2.11.0,时间戳0x493f5d44,故障模块php5ts.dll,版本5.3.0.0,时间戳0x4a4922e7,异常代码0xc0000005,故障偏移0x00083655,进程ID 0x1588,应用程序启动时间0x01ca8b46e4925f90。

Update 2 更新2

Script in question. 有问题的脚本。 I've removed the 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>';

?>

Update 3 更新3

I am using the PHP Simple HTML DOM Parser and I have just found this: 我正在使用PHP Simple HTML DOM Parser,我刚刚发现了这个:

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

I think I should be clearing memory otherwise it will crash. 我想我应该清理内存,否则它会崩溃。 Testing now. 现在测试。

Update4 UPDATE4

Yep, it was a memory leak! 是的,这是内存泄漏! :) :)

I ran into this today when parsing a ton of HTML in a loop. 今天我在循环中解析了大量的HTML时遇到了这个问题。 Here is the simple fix: 这是一个简单的修复:

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

Just calling the clear() method on the dom object when I was done in each iteration cleared it up for me. 当我在每次迭代中完成时,只需在dom对象上调用clear()方法就可以为我清除它。

A bit late to the party, but I've been running into the same issue with a Segmentation Fault when using Simple HTML DOM. 派对有点晚了,但是在使用Simple HTML DOM时我遇到了与分段错误相同的问题。 I'm making sure I'm using $html->clear(); 我确定我正在使用$ html-> clear(); and unset($html); 和未设置($ html); to clear down the HTML I've loaded in, but I was still receiving the Segmentation Fault error during a large scrape of data. 清除我加载的HTML,但是在大量数据中我仍然收到了Segmentation Fault错误。

I found that I needed to comment out some clear code in the simple_html_dom.php file. 我发现我需要在simple_html_dom.php文件中注释掉一些明确的代码。 Look for function clear() within the simple_html_dom_node class, and comment out everything inside; 在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;
    }

Apache was crashing due to a memory leak which was caused by not closing a resource that was being used again and again in a for loop, as well as the script making use of recursion. 由于没有关闭在for循环中一次又一次使用的资源以及使用递归的脚本导致内存泄漏导致Apache崩溃。

Thanks all for the help. 谢谢大家的帮助。

I've run into problems like these using PHP with Apache on Windows. 我在Windows上使用PHP和Apache遇到了类似的问题。 Rather than reporting any useful information to the screen, the process simply dies. 该过程不是向屏幕报告任何有用的信息,而是简单地死亡。 If at all possible, I would suggest running your code on a linux box with Apache & PHP. 如果可能的话,我建议在Apache和PHP的Linux机器上运行你的代码。 In my experience, that combination will generally report this as a memory error, whereas on Windows, nothing seems to happen at all. 根据我的经验,这种组合通常会将此报告为内存错误,而在Windows上,似乎根本没有发生任何事情。 I've only seen this happen with recursion by the way. 我只是看到这种情况发生在顺便递归。

You're not using recursion? 你没有使用递归? I've seen PHP bug out and kill the Apache child without any useful logging output when infinite recursion occurs. 当发生无限递归时,我已经看到PHP错误并且杀死了没有任何有用日志输出的Apache子进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM