微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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版本.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐