|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-05-30 09:33 UTC] busia at tiscali dot it
Description: ------------ You said me (#48368) that unset != free. The memory can remain occupied also after I unset an array, that memory is reused when I set a new variable and, for this reason, it's not a leak. Ok, now it's clear but a problem remains: how can I optimize my code when I cannot see the php internal memory use? I need a function like: memory_get_usage() - memory_allocated_but_usable_for_newly_created_variables(); Setting a variable and seeing that memory use don't change make scripts impossible to optimize. Thanks PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 20:00:01 2025 UTC |
memory_get_usage(true) is the memory allocated from the system. memory_get_usage() is the memory currently in use by PHP. <?php var_dump(memory_get_usage(), memory_get_usage(true)); $x = str_repeat('a', 4096); var_dump(memory_get_usage(), memory_get_usage(true)); unset($x); var_dump(memory_get_usage(), memory_get_usage(true)); Produces int(332392) <-- Initial int(524288) <-- system int(336996) <-- After variable set int(524288) <-- system int(332916) <-- After variable unset int(524288) <-- systemscott you are right, however it seems the behavior differs a bit when unset()ing small-enough variables. memory_get_usage(false) does not takes into account the "small blocks cache" used by the allocator (not the same as the malloc()ed blocks cache). This causes the reported memory usage to not always decrease when unset()ing a variable: <?php $foo = "bar"; var_dump(memory_get_usage()); var_dump(memory_get_usage()); unset($foo); var_dump(memory_get_usage()); ?> int(91448) <-- first call int(91696) <-- before unset int(91696) <-- after unset (== before unset) This patch may fix this: Index: Zend/zend_alloc.c =================================================================== RCS file: /repository/ZendEngine2/zend_alloc.c,v retrieving revision 1.144.2.3.2.43.2.23 diff -u -p -r1.144.2.3.2.43.2.23 zend_alloc.c --- Zend/zend_alloc.c 1 Apr 2009 16:55:47 -0000 1.144.2.3.2.43.2.23 +++ Zend/zend_alloc.c 30 May 2009 12:12:48 -0000 @@ -2496,7 +2496,11 @@ ZEND_API size_t zend_memory_usage(int re if (real_usage) { return AG(mm_heap)->real_size; } else { - return AG(mm_heap)->size; + size_t usage = AG(mm_heap)->size; +#if ZEND_MM_CACHE + usage -= AG(mm_heap)->cached; +#endif + return usage; } }Look this code: ---------------- <? ini_set('memory_limit','300M'); class a { } echo "a - ".(memory_get_usage(true) - memory_get_usage())."\n"; $a=array(); for ($i=0; $i<=1000000; $i++) { $a[]= new a(); } echo "b - ".(memory_get_usage(true) - memory_get_usage())."\n"; unset($a); echo "c - ".(memory_get_usage(true) - memory_get_usage())."\n"; $foo=10000; echo "d - ".(memory_get_usage(true) - memory_get_usage())."\n"; ?> ------------- That show a - 201640 b - 774800 c - 685360 d - 685360 ------------------------ If it is as you said the output near "d" should be different from the output near "c" but they are identical. I want to know how many internal mamory is used from the "$foo" variable. I know the the system memory is the same because php don't make always garbage collection but it collect on the request end bue i want to be able to know how much memory is interested by section of code. Using your code seems that $foo is registered on the void. ThanksThe correction create an other problem: look this script: <? ini_set('memory_limit','300M'); class a { } echo memory_get_usage()."\n"; $a=array(); for ($i=0; $i<=1000000; $i++) { $a[]= new a(); } echo memory_get_usage()."\n"; unset($a); echo memory_get_usage()."\n"; $b=10000; echo memory_get_usage()."\n"; ?> Regarding this code in bug #48368 you said me that unset != free. The memory can remain occupied also after I unset an array, that memory is reused when I set a new variable. Now it is is possibile to check $foo dimension but the memory is not reused. The output of the script is: 325792 230042232 33847560 33847640 It seems that the memory between 325792 and 33847560 is lost, it is not reused when I set $foo.