See this example:
echo memory_get_usage() . "\n"; // 36640
$a = str_repeat("Hello", 4242);
echo memory_get_usage() . "\n"; // 57960
unset($a);
echo memory_get_usage() . "\n"; // 36744
Can anyone explain why after un-setting the variable the memory usage does not return to 36640
If you do it twice the memory will stay at 36744...
echo memory_get_usage() . "\n"; // 36640
$a = str_repeat("Hello", 4242);
echo memory_get_usage() . "\n"; // 57960
unset($a);
echo memory_get_usage() . "\n"; // 36744
$a = str_repeat("Hello", 4242);
unset($a);
echo memory_get_usage() . "\n"; // -> 36744
Garbage collection is an expensive operation, even if there's only a single variable to unset. PHP won't run the collector each time you unset a var, as that'd waste a huge amount of CPU time.
PHP will only run the collector when it has to, as in when something wants more memory than is available.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With