php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40257 stream_context_create() causes memory leaks
Submitted: 2007-01-27 15:25 UTC Modified: 2007-01-27 16:20 UTC
From: angry dot slipper at gmail dot com Assigned:
Status: Not a bug Package: Streams related
PHP Version: 5.2.1RC5 OS: Linux Fedora 4
Private report: No CVE-ID: None
 [2007-01-27 15:25 UTC] angry dot slipper at gmail dot com
Description:
------------
stream_context_create() causes memory leaks.

PHP version is actualy 5.2.1RC5 - latest available.

Reproduce code:
---------------
#!/www/httpd/php/cli -ne
<?php

$postdata=array_fill(0,128,"blah");
for ($a=0;$a<100;$a++){
	$options=array('http'=>array(
				'method'=>"POST",
				'request_fulluri'=>"POST http://www.google.com HTTP/1.0",
				'header'  => 'Content-type: application/x-www-form-urlencoded',
				'content' => $postdata
		)
	);
	$context=stream_context_create($options);
	unset($context,$options);
	echo "current: ",memory_get_usage()," delta: ",(memory_get_usage()-$prev)."\n";
	$prev=memory_get_usage();
}
?>

Expected result:
----------------
"current: 74496 delta: 0" line repeated.

Actual result:
--------------
current: 87368 delta: 87368
current: 100008 delta: 12520
<...many lines here>
current: 1319348 delta: 12440

As you can clearly see each iteration eatch 12kb of RAM, which it shouldn't.

If you comment the

$context=stream_context_create($options);

line out, delta in the example above will always be at 0.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-27 15:32 UTC] tony2001@php.net
All memory leaks are reported automatically.
stream_context_create() creates stream context and adds it to the resource list, which is freed on shutdown.
You cannot free the context with unset(), because there might be open streams using it.
 [2007-01-27 16:01 UTC] angry dot slipper at gmail dot com
I have a daemon written in PHP which connects to remote servers via fopen() sending POST data. 
How do I clean up RAM? I need this daemon to run for months without restarting it.

PS: I can&#8217;t use CURL, cause I need to read from stream with dynamically changed buffer.
 [2007-01-27 16:20 UTC] angry dot slipper at gmail dot com
You could destroy contexts when all streams using it are closed. So when I call fclose() my previously defined $context gets unset freeing up RAM... I think that would solve the whole thing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC