7

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! :)

raven
  • 18,004
  • 16
  • 81
  • 112
Abs
  • 56,052
  • 101
  • 275
  • 409
  • What operating system? Are you noticing a problem with your PHP scripts, or just noticing the error in the log file? – Seth Jan 02 '10 at 01:01
  • I am on Windows Vista at the moment and I am currently looking at the script and its over 220,000K for the httpd.exe process and it just got killed. – Abs Jan 02 '10 at 01:06
  • What's the source of your php.exe? Is it from http://windows.php.net/download/ or maybe xampp or ...? Is this your development/test/personal/fun machine where you can install (almost) anything you like? – VolkerK Jan 02 '10 at 01:21
  • This is wampserver (WAMP5) - my fun box! :) I thought I solved this problem before and I fixed another script before by making sure it was compatible with PHP 5.3 but its back again and its worrying! – Abs Jan 02 '10 at 01:37

5 Answers5

2

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.

Joseph Lust
  • 19,340
  • 7
  • 85
  • 83
  • I cannot find `clear()` in the `DOMDocument`'s methods on http://php.net/manual/en/class.domdocument.php . Or I assume this is not about `DOMDocument` ? – Ahmad Alfy Apr 18 '13 at 14:02
  • Best luck with that documentation. It is abysmal, so don't read too deeply into it. – Joseph Lust Apr 18 '13 at 17:43
  • @AhmadAlfy I think it's the Simple HTML DOM clear() function that you want to be looking for, although that still isn't perfect - see http://stackoverflow.com/a/15946851/1426354 – olimortimer Oct 13 '14 at 14:16
2

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.

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;

    function clear() {
            // These were causing a segmentation fault...
            // $this->dom = null;
            // $this->nodes = null;
            // $this->parent = null;
            // $this->children = null;
    }
olimortimer
  • 1,373
  • 10
  • 23
2

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.

Abs
  • 56,052
  • 101
  • 275
  • 409
1

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.

davidtbernal
  • 13,434
  • 9
  • 44
  • 60
  • `exception code 0xc0000005` means "Access violation" which tells you as much or as little as "Segmentation fault" on a linux machine ;-) – VolkerK Jan 02 '10 at 01:25
  • Right, my point was that on a linux machine, PHP would catch the error, rather than just dying a fiery death. Anyway, it looks like they solved the problem. – davidtbernal Jan 02 '10 at 19:47
0

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.

Rob
  • 47,999
  • 5
  • 74
  • 91
  • It's likely that recursion would actually increase the amount of memory consumed, since a stack frame would need to be stored for each call(it doesn't look like PHP supports tail recursion). – Mike Jan 02 '10 at 01:05
  • I am using recursion and a lot of stuff is being held in memory. This script will be on some good hardware, I am wondering what I should increase temporarily to get the script to finish? – Abs Jan 02 '10 at 01:08