ID: 45161 Updated by: [EMAIL PROTECTED] Reported By: humbads at alum dot mit dot edu Status: Open Bug Type: cURL related Operating System: * PHP Version: 5CVS, 6CVS (2008-12-08) New Comment:
Hi, Managed to get rid of to_free.str, with one exception bugging me: when we call curl_slist_append(), we duplicate the string before, however curl doc says: "curl_slist_append() copies the string." Is this true with all versions of curl? I see nothing written about this in the curl man page. Previous Comments: ------------------------------------------------------------------------ [2008-10-28 22:25:04] [EMAIL PROTECTED] I have no time to spend on this right now. ------------------------------------------------------------------------ [2008-08-15 15:37:26] [EMAIL PROTECTED] FYI: This is by design in any resource in PHP. Freeing happens in the request shutdown. Just do unset($ch); in the end of the for loop and $ch = curl_init(); in the start of the loop and you get "static" memory usage. I'm going to only look at what Daniel pointed out and make PHP not duplicate unnecessarily if curl version is >= 7.17.0. ------------------------------------------------------------------------ [2008-06-22 22:06:30] daniel at haxx dot se I could also add that libcurl does duplicate the input strings since a few versions back (since libcurl 7.17.0), so if such a recent version of libcurl is detected there's no reason at all for PHP to keep copies. This goes for everything except CURLOPT_POSTFIELDS which isn't duplicated for backwards compatiblity reasons, but that one can then be replaced with CURLOPT_COPYPOSTFIELDS which does copy. ------------------------------------------------------------------------ [2008-06-03 19:24:34] humbads at alum dot mit dot edu I did some more investigation, and found that this is not really a leak as much as it is runaway memory usage. PHP does indeed free the memory eventually. The problem is in interface.c curl_setopt under CURLOPT_URL. The function is making a copy of the string parameter, in this case, the URL, and saving that to the "to_free" list. That list is not freed until sometime later, so repeatedly setting CURLOPT_URL on the same handle will keep using more memory. In the sample code I posted, the memory only increases during the curl_setopt CURLOPT_URL function call. This is just an inefficient design of the PHP curl handle. One way around it is for the code to maintain only one copied string for each CURLOPT string option. If the same CURLOPT string option is set again, it should free the previously created string, and create a new one. At the PHP level, the only workaround for now is to periodically close the handle and then create a new one with curl_init. This seems to cap the memory usage. ------------------------------------------------------------------------ [2008-06-03 16:07:04] humbads at alum dot mit dot edu Below is the r.php script. When testing this bug report, please use this script (or some other URL) so my poor server does not get hammered. <?php // This script runs a variable amount of time // and generates a variable amount of data // Output a random number of blank space $s = microtime(true); $m = rand(100,200); $bytes = 0; for($i = 0; $i < $m; $i++) { $message = " \n"; print $message; $bytes += strlen($message); usleep(10); } // Print time taken and the value of the "echo" parameter print isset($_REQUEST['echo']) ? 'echo: '.$_REQUEST['echo'].' ' : ""; print $bytes.' bytes '; print " in "; print round(microtime(true) - $s, 4)." seconds"; exit(); ?> ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/45161 -- Edit this bug report at http://bugs.php.net/?id=45161&edit=1