|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-11-07 09:26 UTC] datibbaw@php.net
Description:
------------
When stream_context_create() is used in conjunction with file_get_contents() or other stream related functions that accept a context parameter, memory is being leaked.
Reproduce code:
---------------
for ($i=0;$i<5;++$i){
$m0 = memory_get_usage();
file_get_contents('http://www.google.com', false, stream_context_create(array()));
$m1 = memory_get_usage();
echo $m1-$m0,PHP_EOL;
}
Expected result:
----------------
X (where X is the memory increase for the first iterator)
0
0
0
0
Actual result:
--------------
X (where X is the memory increase for the first iterator)
384 (or something similar)
420
420
480
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 20 12:00:01 2025 UTC |
Code to reproduce can be made simpler, since the problem seems to be with stream_context_create(): $m0 = $m1 = $i = 0; $c = null; while ($i<5) { $m0 = memory_get_usage(); $c = stream_context_create(); $m1 = memory_get_usage(); echo $m1-$m0,PHP_EOL; ++$i; }Test code changed to (added the unset() statement): $m0 = $m1 = $i = 0; $c = null; while ($i<5) { $m0 = memory_get_usage(); $c = stream_context_create(); unset($c); $m1 = memory_get_usage(); echo $m1-$m0,PHP_EOL; ++$i; } Output: 336 420 420 420 420 Though, as a language feature I would assume that in the second iteration the memory occupied by the previous assignment would be freed implicitly. Is that a wrong assumption?<?php echo "This is PHP ", phpversion(),PHP_EOL; ini_set("memory_limit", "0"); for ($i=0;$i<2000;++$i) { $x = stream_context_create(array()); unset($x); } echo "Done", PHP_EOL; ?> ---------------- $ /usr/local/php-5.1.0/bin/php lala.php This is PHP 5.1.0 Done ---------------- $ /usr/local/php-5.1.1/bin/php lala.php This is PHP 5.1.1 Done ---------------- $ /usr/local/php-5.1.2/bin/php lala.php This is PHP 5.1.2 Done ---------------- $ /usr/local/php-5.2.9/bin/php lala.php This is PHP 5.2.9 Fatal error: Allowed memory size of 262144 bytes exhausted at /usr/local/src/php-5.2.9/Zend/zend_list.c:47 (tried to allocate 12 bytes) in /home/tjerk/lala.php on line 7 ---------------- $ /usr/local/php-5.2.11/bin/php lala.php This is PHP 5.2.11 Fatal error: Allowed memory size of 262144 bytes exhausted at /usr/local/src/php-5.2.11/main/streams/streams.c:1945 (tried to allocate 16 bytes) in /home/tjerk/lala.php on line 7 ---------------- $ /usr/local/php-5.3/bin/php lala.php This is PHP 5.3.0RC3-dev DoneI'm not trying to disable memory_limit. Setting it to "0" just happens to make the issue appear faster. Changing it to the below value of 1k yields the same results as mentioned earlier, tested on the same 5 php configurations: ini_set("memory_limit", "1000");