I do not think this is a memory leak. The memory is really freed and available, it just seems that php saves it for its use and does not return it to the system. I would suggest that this has anything to do with the garbage collector. This seems to be bad behavior, but maybe there is a good reason ...
This is my proof: (Due to my configuration, I used lower values, but the behavior was the same)
/* You class definition */ print 'Memory before: '.memory_get_usage(1).' <br>'; // 262 144 $a = []; $b = []; for ($i=0; $i<5000; $i++) $a[] = new Test_Class($i); print 'Memory after create: '.memory_get_usage(1).' <br>'; // 2 359 296 for($i=0; $i < count($a); $i++) unset($a[$i]); unset($a); print 'Memory after unset: '.memory_get_usage(1).' <br>'; // 1 572 864 for ($i=0; $i<1000; $i++) $b[] = $i; print 'Memory after $b: '.memory_get_usage(1).' <br>'; // 1 572 864
Here you can see that no more memory is needed to create $b . Which is strange, because an array needs memory when you do nothing before:
$b = []; print 'Memory before: '.memory_get_usage(1).' <br>'; // 262 144 for ($i=0; $i<1000; $i++) $b[] = $i; print 'Memory after: '.memory_get_usage(1).' <br>'; // 524 288
This is why I think the memory is freed, but php just sits on it.
source share