Does PHP support dynamic memory usage depending on its limit?

I have a script that runs successfully when PHP memory_limit is set to 50Mafter about 25 seconds of runtime. When I type memory_get_peak_usageat the end of the script, it comes pretty close to 50M. When I set the value of memory_limit above 90M, memory_get_peak_usageshows around 75M, and the script is loaded about 10 seconds faster.

It seems intuitive that the script will use about the same amount of memory regardless of memory_limit, but that doesn't seem to be the case. If the script parameter goes beyond less than 50 M with a limit 50M, I expected peak usage to be the same, even though memory_limit was increased.

The only explanation I have is that PHP recognizes its proximity to its limit and spends time cleaning up unused memory to avoid the limit. Does it really work, or did I just scratch the surface of something larger?

+5
source share
2 answers

What you see is a garbage collector doing its job.

When you override the value of a non-primitive variable, the old value is not immediately discarded from memory. It stays there as part of your memory usage script.

script , , script. , script - .

Edit:

. script , , , . , , , fwrite() , .

+2

, /, memory_limit php, script vs memory_get_peak_usage , script ( , php )

memory_get_peak_usage

real_usage

, , . FALSE, , emalloc().

+1

All Articles