Running PHP 5.3.6 under MAMP on MAC, the memory usage increases every x calls (between 3 and 8) until the script dies from memory exhaustion. How do I fix this?
libxml_use_internal_errors(true);
while(true){
$dom = new DOMDocument();
$dom->loadHTML(file_get_contents('http://www.ebay.com/'));
unset($dom);
echo memory_get_peak_usage(true) . '<br>'; flush();
}
Testing your script locally produces the same result. Changing
file_get_contents()
to a local HTML file however produces a consistent memory usage. It could be that the output from ebay.com is changing every X calls.Based on @Tak answer and @FrancisAvila comment, I found that this snippet works better for me:
This has the added benefits of 1) not discarding the errors of the last parse if you ever need to access them via
libxml_get_errors()
, and 2) callinglibxml_clear_errors()
only when necessary, sincelibxml_use_internal_errors()
returns the previous setting state.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:You can try forcing the garbage collector to run with
gc_collect_cycles()
, but otherwise you're out of luck. PHP doesn't expose much of anything to control its internal memory usage, let alone memory used by a plugin library.