6

I have this problem with a loop using cURL where memory grows exponentially. 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.

PHP 5.4.5
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);
}
2
  • Did you try curl_multi_init? Maybe it helps. tr.php.net/manual/en/function.curl-multi-init.php Commented Aug 15, 2012 at 14:21
  • I try curl_multi, bud that's the same problem, even it's more slower. Commented Aug 16, 2012 at 7:30

3 Answers 3

3

I think I found a fix to the memory leak. I've got the same problem using curl lib in a PHP script. After repeated calls to curl_exec() function, memory becomes exhausted.

According to a PHP bug report this memory leak may be fixed unsetting the Curl handler after closing it, like next code:

...
curl_close($ch);
unset($ch);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but this is in my code, and no effect. Memory still growth
1

This is late, but I recommend against using curl_close in this instance, or if you do, placing it outside the for loop.

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. Overwriting the handler with the curl_init seems to be more than enough. There seems to be an issue with curl_close.

Comments

0

One solution would be to call curl less (say 100 times) and then to refresh page, which may allow the memory to be freed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.