DOMDocument PHP 内存泄漏

2021-12-30 00:00:00 memory-leaks php domdocument

在 MAC 上的 MAMP 下运行 PHP 5.3.6,内存使用量每 x 次调用(3 到 8 次)增加一次,直到脚本因内存耗尽而终止.我该如何解决这个问题?

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();
}

推荐答案

Using libxml_use_internal_errors(true); 会抑制错误输出,但会构建一个连续的错误日志,并附加到每个循环中.要么禁用内部日志记录并抑制 PHP 警告,要么像这样清除每次循环迭代的内部日志:

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) . "
"; flush();
}
?>

相关文章