PHP 内存不足 - 使 Apache 崩溃?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1990216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:33:14  来源:igfitidea点击:

PHP Out of Memory - Crashes Apache?

phpapachememory

提问by Abs

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,8600000000000x00083655,8600000000000000000000000

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

更新 4

Yep, it was a memory leak! :)

是的,这是内存泄漏!:)

采纳答案by Abs

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.

感谢大家的帮助。

回答by Joseph Lust

I ran into this today when parsing a ton of HTML in a loop. Here is the simple fix:

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

$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()方法就可以为我清除它。

回答by olimortimer

A bit late to the party, but I've been running into the same issue with a Segmentation Fault when using Simple HTML DOM. I'm making sure I'm using $html->clear(); and unset($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 DOM 时遇到了与分段错误相同的问题。我确保我正在使用 $html->clear(); 并取消设置($ html);清除我加载的 HTML,但在大量数据抓取期间我仍然收到 Segmentation Fault 错误。

I found that I needed to comment out some clear code in the simple_html_dom.php file. Look for function clear() within the simple_html_dom_node class, and comment out everything inside;

我发现我需要在 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;
    }

回答by davidtbernal

I've run into problems like these using PHP with Apache on Windows. 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. In my experience, that combination will generally report this as a memory error, whereas on Windows, nothing seems to happen at all. I've only seen this happen with recursion by the way.

我在 Windows 上使用 PHP 和 Apache 遇到过这样的问题。该过程不会向屏幕报告任何有用的信息,而是简单地终止。如果可能的话,我建议在带有 Apache 和 PHP 的 linux 机器上运行您的代码。根据我的经验,这种组合通常会将其报告为内存错误,而在 Windows 上,似乎什么也没有发生。顺便说一下,我只看到递归发生这种情况。

回答by Rob

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 子进程而没有任何有用的日志输出。