php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63859 Memory leak when reusing curl-handle
Submitted: 2012-12-27 08:59 UTC Modified: -
From: jille at hexon dot cx Assigned:
Status: Closed Package: cURL related
PHP Version: 5.4.10 OS: Linux
Private report: No CVE-ID: None
 [2012-12-27 08:59 UTC] jille at hexon dot cx
Description:
------------
PHP (or libcurl, I'm afraid I don't know where exactly the problem lies) seems to leak memory when using the same curl-handle for multiple requests.

The test script below reports the same memory_get_usage()-value for the first 300 requests and a 25% increase after but top(1) reports the memory grows up to 100MB when the script is done.

Uncommenting the resetting of CURLOPT_POSTFIELDS doesn't seem to help. Uncommenting the recreation of $ch prevents the memory from being leaked.

Test script:
---------------
<?php

	$ch = curl_init('http://some-domain.com/dump.html');
	for($i = 0; 1000 > $i; $i++) {
#		$ch = curl_init('http://some-domain.com/dump.html');
		$post = array();
		$post['data'] = base64_encode(str_repeat(hash('sha256', uniqid()), 1000));
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_exec($ch);
		# curl_setopt($ch, CURLOPT_POSTFIELDS, '');
		if($i % 100 == 0) {
			var_dump(memory_get_usage(true));
		}
	}
?>



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-27 18:42 UTC] pierrick@php.net
Automatic comment on behalf of pierrick
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ac3d227e28056bf5294a8a64e2f41ce2beebaa05
Log: Fixed #63859 Memory leak when reusing curl-handle
 [2012-12-27 18:42 UTC] pierrick@php.net
-Status: Open +Status: Closed
 [2013-01-12 16:39 UTC] derick@php.net
Automatic comment on behalf of pierrick
Revision: http://git.php.net/?p=php-src.git;a=commit;h=ac3d227e28056bf5294a8a64e2f41ce2beebaa05
Log: Fixed #63859 Memory leak when reusing curl-handle
 [2014-01-13 20:29 UTC] artur77 at freemail dot hu
I found a solution:
Change CURLOPT_POSTFIELDS to CURLOPT_COPYPOSTFIELDS.
CURLOPT_COPYPOSTFIELDS is not defined in php, so you have to use the direct value: 10165
 [2014-10-07 23:20 UTC] stas@php.net
Automatic comment on behalf of pierrick
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=ac3d227e28056bf5294a8a64e2f41ce2beebaa05
Log: Fixed #63859 Memory leak when reusing curl-handle
 [2014-10-07 23:31 UTC] stas@php.net
Automatic comment on behalf of pierrick
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=ac3d227e28056bf5294a8a64e2f41ce2beebaa05
Log: Fixed #63859 Memory leak when reusing curl-handle
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC