#45161 [Opn]: Reusing a curl handle leaks memory
| From: | magicaltux@php.net | Date: | Thu, 11 Dec 2008 13:51:12 +0000 |
| Subject: | #45161 [Opn]: Reusing a curl handle leaks memory | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-131912@lists.php.net to get a copy of this message | ||
ID: 45161
Updated by: magicaltux@php.net
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:
This patch will get rid of to_free.str if libcurl version is >= 7.17.0
Also:
[09:25:46] <MT`AwAy> btw curl doc says: "curl_slist_append() copies the
string.", is this true for all versions of libcurl ?
[09:26:33] <B4gder> yes
So, code duplicating strings before passing them to curl_slist_append()
has been completly removed.
http://ookoo.org/svn/snip/php_bug45161_curl_textcopy.patch
Previous Comments:
------------------------------------------------------------------------
[2008-12-11 06:03:56] magicaltux@php.net
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.
------------------------------------------------------------------------
[2008-10-28 22:25:04] jani@php.net
I have no time to spend on this right now.
------------------------------------------------------------------------
[2008-08-15 15:37:26] jani@php.net
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.
------------------------------------------------------------------------
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