Domdocument PHP Memory Leak

DOMDocument PHP Memory Leak

Using libxml_use_internal_errors(true); suppresses error output but builds a continuous log of errors which is appended to on each loop. Either disable the internal logging and suppress PHP warnings, or clear the internal log on each loop iteration like this:

<?php
libxml_use_internal_errors(true);
while(true){
$dom = new DOMDocument();
$dom->loadHTML(file_get_contents('ebay.html'));
unset($dom);
libxml_use_internal_errors(false);
libxml_use_internal_errors(true);
echo memory_get_peak_usage(true) . "\r\n"; flush();
}
?>

PHP Simple HTML DOM Parser Memory Leak

See http://simplehtmldom.sourceforge.net/manual_faq.htm

Q: This script is leaking memory seriously... After it finished running, it's not cleaning up dom object properly from memory..

A: Due to php5 circular references memory leak, after creating DOM object, you must call dom->clear() to free memory if call file_get_dom() more then once.

Example:

$html = file_get_html(...);  // do something...  
$html->clear();
unset($html);

This happens a lot when you are using this library in a loop.

PHP memory leak of simple_html_dom (after the 'parse()' function)

You're not clearing the dom objects correctly, you forgot the parentheses:

$html->clear();
$images->clear();

Here's a demo of the corrected code

And here's the ouput:

Also, if you have any detected bug or possible patch, feel free to report it to their bugs/patch tracker



Related Topics



Leave a reply



Submit