PHP cUrl循环泄漏内存

以下代码处于循环中.每个循环将URL更改为新地址.我的问题是每次传递占用的内存越来越多.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://site.ru/');
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, 'http://site.ru/');
curl_setopt($ch, CURLOPT_HEADER, false);

$html = new \DOMDocument();
$html->loadHTML(curl_exec($ch));

curl_close($ch);
$ch = null;

$xpath = new \DOMXPath($html);
$html = null;

foreach ($xpath->query('//*[@id="tree"]/li[position() > 5]') as $category) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $xpath->query('./a', $category)->item(0)->nodeValue);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 'http://site.ru/');
    curl_setopt($ch, CURLOPT_HEADER, false);

    $html = new \DOMDocument();
    $html->loadHTML(curl_exec($ch));

    curl_close($ch);
    $ch = null;

    // etc.
}

内存为2000 Mb.脚本执行时间~2h. PHP版本5.4.4.
如何避免内存泄漏?谢谢!

解决方法:

来自互联网的故事表明curl_setopt($ch,CURLOPT_RETURNTRANSFER,true)在某些PHP / cURL版本中被破解:

> #13225 Memmory leak in curl
> #40831 cURL extension doesnt clear buffer for reused handle
> PHP script memory leak issue
> PHP cURL, memory leak when using CURLOPT_RETURNTRANSFER

您还可以找到DOM的故事:

> DOMDocument PHP Memory Leak
> PHP/DOMDocument: unset() does not release resources
> DOMDocument / Xpath leaking memory during long command line process – any way to deconstruct this class

创建一个最小的测试用例,找出泄漏的原因.即从代码删除不相关的包(DOM或cURL).

然后使用最新的PHP版本重现它.如果它仍然导致泄漏,file a bug report否则使用该PHP版本.

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...