#47103 [Ver]: memory leak using foreach and references
| From: | jani@php.net | Date: | Thu, 15 Jan 2009 14:52:57 +0000 |
| Subject: | #47103 [Ver]: memory leak using foreach and references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-132900@lists.php.net to get a copy of this message | ||
ID: 47103
Updated by: jani@php.net
Reported By: soywiz at gmail dot com
Status: Verified
Bug Type: Scripting Engine problem
Operating System: *
PHP Version: 5.*CVS,6CVS (2009-01-15)
New Comment:
This worked:
# php -dmemory_limit=1000M t.php
int(1018887988)
That number is from memory_get_usage() after the foreach.. :)
Previous Comments:
------------------------------------------------------------------------
[2009-01-15 14:51:25] jani@php.net
This is NOT expected but might not be fixable. And foreach with
reference is supposed to work without leaks.
------------------------------------------------------------------------
[2009-01-14 17:35:42] crrodriguez at opensuse dot org
looks like expected, do not use foreach value by reference, it is know
to have "apparently unexpected behaviors" that are normal.
------------------------------------------------------------------------
[2009-01-14 16:35:07] soywiz at gmail dot com
Description:
------------
This snippet causes an out of memory. It seems that it's duplicating
the value (it's trying to allocate a memory chunk as large as each
element on the array) and it's not freeing it.
The problem persists even with a unset($v); within the foreach.
I reproduced it using php 5.2.8-cli on windows and php 5.2.5-cgi on
linux.
I think it should work, but maybe it's a php feature?
Reproduce code:
---------------
<?php
$a = array_fill(0, 99999, str_repeat('*', 9991));
foreach ($a as &$v) ;
?>
Expected result:
----------------
Don't waste all the memory
Actual result:
--------------
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to
allocate 9992 bytes) in test.php on line 3
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47103&edit=1