Reclaiming Memory from #PHP Arrays
https://medium.com/@vectorial1024/reclaiming-memory-from-php-arrays-49c7e63bd3d2
Definitely learned something from reading this. #programming
@davidbisset looks costly on CPU though. Could be useful in long running scripts
@davidbisset Now I want to try the example but set the element to null before unsetting it...
@davidbisset Nope assigning it to null doesn't help.
However, if your array is more than just allocated but has actual values in each element. Say a long string... unsetting it DOES reduce the memory usage.
I modified the example to add a step which assigns each element:
str_repeat("abcdefg", 100);
And memory use went up of course. But then after unsetting each element it does drop the memory back down. So unsetting array elements is worth it.
@syntaxseed @davidbisset I'm guessing assigning to null still keeps the reference count for the array item > 0, so it won't get cleared by garbage collection. And, as you found, explicitly unsetting it does. Tideways has an explanation for memory + variables: https://tideways.com/profiler/blog/what-is-garbage-collection-in-php-and-how-do-you-make-the-most-of-it
@omerida @syntaxseed Like I said, learned something. And thanks for the extra info too.