Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Out of Memory - Crashes Apache?

Tags:

php

memory

apache

I am running PHP version 5.3.0 and 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?!

[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?

Thanks all

Update

This error has been logged by my windows machine in the event viewer:

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.

Update 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

I am using the PHP Simple HTML DOM Parser and I have just found this:

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

I think I should be clearing memory otherwise it will crash. Testing now.

Update4

Yep, it was a memory leak! :)

like image 730
Abs Avatar asked Jan 02 '10 00:01

Abs


2 Answers

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.

Thanks all for the help.

like image 97
Abs Avatar answered Nov 12 '22 11:11

Abs


I ran into this today when parsing a ton of HTML in a loop. 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.

like image 2
Joseph Lust Avatar answered Nov 12 '22 09:11

Joseph Lust