|  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
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
44 + 45 = ?
Subscribe to this entry?

 [2007-01-27 15:25 UTC] angry dot slipper at gmail dot com
stream_context_create() causes memory leaks.

PHP version is actualy 5.2.1RC5 - latest available.

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

for ($a=0;$a<100;$a++){
				'request_fulluri'=>"POST HTTP/1.0",
				'header'  => 'Content-type: application/x-www-form-urlencoded',
				'content' => $postdata
	echo "current: ",memory_get_usage()," delta: ",(memory_get_usage()-$prev)."\n";

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


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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-27 15:32 UTC]
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-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 27 05:03:35 2021 UTC