I did a very quick test by doing the following:
- Declaration
$iterations = 10; at the beginning of the script - using
$p , not $i in a for loop (you use $p to access the elements of the array, not $i ) - adding a line to display used memory before unsets
I get this using PHP 5.3.1 (today is a snapshot):
328616 332176 331728
Three outputs:
- first: start of a script (one that says: "Show current memory usage BEFORE")
- second: the one that I added before the calls to cancel; immediately after the
for loop ends - third: the one you put at the end of your script.
Thus, unsets really provoke the release of some memory, rather than increasing the memory used; -)
But only a relatively small fraction of the allocated memory is freed ...
Now the question may be, “what causes some memory to be used and not freed, an event if I call unset on arrays” - in fact, maybe this is what you meant for the first time?
Well, I suppose that disabling the arrays themselves does not make PHP free the memory allocated for elements inside the arrays ...
Similarly, this note on the uninstalled manual page says (quoted):
unset () does what it calls - disable the variable. This does not immediately release memory. The PHP garbage collector will do this when it sees seizures - at will, as soon as these processor cycles are not needed anyway, or even before the script runs out of memory, whatever the first happens.
If you do $ whatever = null; then you rewrite the variable data. You can free memory / shrink faster, but can steal the loop processor from the code that you really need them earlier, resulting in a total runtime.
And, in fact, this may seem interesting; -)
However, running a few quick tests does not cause anything interesting; I believe my script / data is not large enough to force PHP to free up unused memory or something like that ...
Note: if I use gc_collect_cycles (PHP> = 5.3) to force garbage collection after unsets, it doesn’t change anything - - I suppose this is because there is no "lost cycle".
* BTW: running your code gave me some comments (e.g. $ p vs $ i); Are you developing with setting error_reporting to send notifications? *