Re: Memory Leaks with Objects (no circular reference)

From: Date: Fri, 06 Mar 2009 07:16:38 +0000
Subject: Re: Memory Leaks with Objects (no circular reference)
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-43266@lists.php.net to get a copy of this message
Hi Igor, It's definitely not a memory leak. The memory manager in php-5.2 was changed so it behaves little bit different. BTW: both 5.1 and 5.2 use some caches of the chunks with most useful sizes. They allow to eliminate annecessury free()/malloc() calls and speedup the whole system. memory_get_usage() returns allocated memory including these caches, so you can think that memory lost however it's just preallocated and ready for next emalloc()s. Thanks. Dmitry. Igor Feghali wrote:
Hi, I am facing some memory leaks with php 5.1.6 that has been fixed in 5.2.0. So I suppose the fix should be somewhere between those versions, but I couldn't find exactly which line in the NEWS file is that. <?php class foo { var $a1; var $a2; var $a3; var $a4; var $a5; var $a6; var $a7; var $a8; var $a9; } class bar { var $a1; var $a2; var $a3; var $a4; var $a5; var $a6; var $a7; var $a8; } echo "--- 9 ---\n"; for ($i = 0; $i < 5; $i++){ $u =& new foo(); unset($u); echo memory_get_usage()."\n"; } echo "--- 8 ---\n"; for ($i = 0; $i < 5; $i++){ $u =& new bar(); unset($u); echo memory_get_usage()."\n"; } ?> Output for 5.1.6: --- 9 --- 44712 44784 44848 44912 44976 --- 8 --- 45008 45008 45008 45008 45008 Conclusion: memory consumption keeps constant for the class with 8 properties. unset() doesnt frees all the memory up for the class with 9 properties. Output for 5.2.0: --- 9 --- 66784 66828 66828 66828 66828 --- 8 --- 66896 66896 66896 66896 66896 Conclusion: memory consumption constant along the whole code, as expected. I have seen that reported many times in the bugs system, but in all the cases it ended up being something related to circular references which is not the case here. Some people also reported that replacing "unset($obj)" with "$obj = null" would help but I tried it and the problem remains. Any help on how to workaround that in userland would be very appreciated. Surely upgrading will fix that, but I am trying to keep compatibility with prior versions as much as possible. Best, ~IF.


« previous php.internals (#43266) next »