I'm trying to understand how unset() works with classes and their properties. I wrote a little test to see memory usage.
class B {}
class A {
private $b;
public function __construct() {
$this->b = new B;
}
public function __destruct() {
unset($this->b);
}
}
echo memory_get_usage() . '<br/>';
$a = [];
for ($i = 0; $i < 5000; $i++) {
$a[$i] = new A;
}
echo memory_get_usage() . '<br/>';
for ($i = 0; $i < 5000; $i++) {
$a[$i]->__destruct();
unset($a[$i]);
}
unset($a);
echo memory_get_usage() . '<br/>';
What I was expecting is that the last memory usage should be similar to the first. But it's a lot higher:
236184 (before)
2845320 (peak)
1219432 (after)
I don't really understand how all this works. Thanks for your help!
Note: I tried to use an other name for __destruct (we never know) but that doesn't change anything.
Following some comments below, I tried with memory_get_usage(true); and the result is this:
262144 (before)
3145728 (peak)
2359296 (after)
real_usageargument