CURL loop memory growth

I have this problem with a loop using cURL where memory grows exponentially. In this example script, it starts to use about 14 MB of memory and ends with 28 MB, with my original script and repeating up to 1.000.000, the memory grows to 800 MB, 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);
}
+5
source share
3 answers

I think I found a memory leak fix. I have the same problem using curl lib in a PHP script. After repeated calls to the curl_exec () function, the memory runs out.

According to the PHP error report, this memory leak can be fixed if you disable the Curl handler after closing it, as the following code:

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

(, 100 ), , .

0

Late, but I recommend not using curl_close in this instance, or if you do, placing it outside the for loop.

We had a similar problem, which caused curl to spin after many cycles. We used curl_multi and closed each of the individual handlers, which is why our memory left. It seems that rewriting the handler with curl_init is more than enough. There seems to be a problem with curl_close.

0
source

All Articles