A
A
Alexey2018-12-07 11:47:31
PHP
Alexey, 2018-12-07 11:47:31

PHP. Why does the peak value of used memory increase when clearing variables and arrays?

I've been doing performance optimizations in my PHP scripts.
I see that in the course of script execution there is a lot of unnecessary data in memory.
There was an idea to clear the data received from a DB when they were used, by means of unset.
The sooner I erase, the faster they will not take up memory - the logic is this.
For example:

$a=mysql_query("SELECT etc");
 while($b=mysql_fetch_assoc($a)){
     //do some
}
unset($a);

I introduced more and more unsets.
It turned out that with each new unset($a) I decrease the value issued by memory_get_usage() (not a surprise), but at the same time the value of the peak memory consumption by the script, issued by memory_get_peak_usage(), increases.
This is how I got data on the consumed memory
$log->LogDebug('MemUsage (after all) '.(memory_get_usage()-$base_memory_usage).'. Memory peak was '.memory_get_peak_usage(TRUE).'.');

And in the log I see with each iteration after introducing a new unset into the code (not in one run!):
MemUsage (after all) 281080. Memory peak was 559024.
MemUsage (after all) 275008. Memory peak was 576728.
MemUsage (after all) 274608. Memory peak was 579024.
MemUsage (after all) 274272. Memory peak was 786432.

It is not very clear why when deleting arrays and variables, the peak memory usage of the script grows. I was expecting both values ​​to decrease as I thought the peak value of memory_get_peak_usage(TRUE) is the maximum value of memory_get_usage()
So I don't know if I'm doing better or worse?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Nesmeyanov, 2018-12-07
@SerafimArts

It is not very clear why when deleting arrays and variables, the peak memory usage of the script grows.

Because unset does not delete anything, but adds new data to the zval - the deletion flag, so that the GC can clean it up the next time it starts (Although this is also not the case, the GC cleans up almost nothing in the puff, but simply overwrites it, i.e. memory allocations take time).
And the unset operation itself is needed only in order to correctly remove data in real-time (that is, roughly speaking, while true) applications, if there is no certainty that the refcount of the zval will be equal to 0 after the code is processed. If the application does not have any event loop, then using unset and gc_collect_cycles will only add brakes and peaks in memory consumption.
This is not an absolute truth, of course. But it is better to entrust memory management to PHP. But for example, in the code above, which is 10 years out of date and now simply will not work - after the last line there will be no variables left in memory at all. More precisely, they will remain, but with refcount = 0, which means that creating a new variable will simply overwrite the already allocated memory.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question