简体   繁体   English

cURL循环内存增长

[英]cURL loop memory growth

I have this problem with a loop using cURL where memory grows exponentially. 我有一个使用cURL循环的问题,其中内存以指数方式增长。 In this example script, it starts using approximately 14MB of memory and ends with 28MB, with my original script and repeating to 1.000.000, memory grows to 800MB, which is bad. 在这个示例脚本中,它开始使用大约14MB的内存并以28MB结束,使用我的原始脚本并重复到1.000.000,内存增长到800MB,这很糟糕。

PHP 5.4.5 PHP 5.4.5
cURL 7.21.0 cURL 7.21.0

for ($n = 1; $n <= 1000; $n++){

    $apiCall = 'https://api.instagram.com/v1/users/' . $n . '?access_token=5600913.47c8437.358fc525ccb94a5cb33c7d1e246ef772';

    $options = Array(CURLOPT_URL => $apiCall,
                     CURLOPT_RETURNTRANSFER => true,
                     CURLOPT_FRESH_CONNECT => true
    );

    $ch = curl_init();
    curl_setopt_array($ch, $options);
    $response = curl_exec($ch);
    curl_close($ch);

    unset($ch);
}

I think I found a fix to the memory leak. 我想我找到了内存泄漏的修复程序。 I've got the same problem using curl lib in a PHP script. 我在PHP脚本中使用curl lib时遇到了同样的问题。 After repeated calls to curl_exec() function, memory becomes exhausted. 在重复调用curl_exec()函数后,内存就会耗尽。

According to a PHP bug report this memory leak may be fixed unsetting the Curl handler after closing it, like next code: 根据PHP错误报告,可以修复此内存泄漏,在关闭它之后取消设置Curl处理程序,如下一个代码:

...
curl_close($ch);
unset($ch);

一种解决方案是调用curl(比如100次)然后刷新页面,这可能允许释放内存。

This is late, but I recommend against using curl_close in this instance, or if you do, placing it outside the for loop. 这是迟到的,但我建议不要在这个实例中使用curl_close,或者如果你这样做,将它放在for循环之外。

We had a similar issue where curl memory started leaking after many loops. 我们有一个类似的问题,卷曲内存在许多循环后开始泄漏。 We were using curl_multi and closing each of the individual handlers, which caused our memory go bonkers. 我们使用curl_multi并关闭每个处理程序,这导致我们的记忆变得疯狂。 Overwriting the handler with the curl_init seems to be more than enough. 使用curl_init覆盖处理程序似乎绰绰有余。 There seems to be an issue with curl_close. curl_close似乎存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM