Answer the question
In order to leave comments, you need to log in
Forced memory cleanup PHP?
Faced a problem, it is necessary to perform an infinite number of iterations. But with each iteration, the amount of memory consumed by the script increases. Here's a little test.
public function memory()<br>
{<br>
$text = 'Много текста';<br><br>
$baseMemory = memory_get_usage();<br>
gc_enable();<br>
for ($x = 0; $x++ < 100000;) {<br>
Test::create($text);<br>
if ($x % 500 === 0) {<br>
echo $x . "\n";<br>
echo get_file_size(memory_get_usage() - $baseMemory), "\n";<br>
}<br>
}<br>
}<br>
Answer the question
In order to leave comments, you need to log in
Problem solved. The database query profiler has been enabled. Which saved all requests to a variable.
If you have a memory leak in PHP. then this is usually not a PHP bug. Surely, somewhere in the library you are using, there is a log or cache and it accumulates without being released (there was a case, for example, when all SQL queries were logged into an array that was never cleared).
For example, there is such a phpQuery library, once I tried to use it, it consumed memory in unmeasured quantities.
Extensions like DOM can also leak.
In general, study the code carefully and look for dubious places.
I also want to send rays of hatred to everyone who, without understanding the problem, writes nonsense like “PHP is leaking”. PHP has a better memory management system than other languages, as it has Copy-on-Write, a reference counter, and a garbage collector. This is not available in all languages.
You can try periodically for every hundredth iteration
gc_collect_cycles()
I created an object and destroyed it through unset() and assigned NULL too, the result is the same
upd: not there
I once wrote a wild perversion, a multi-process program in PHP, so there each fork during iteration also irrevocably ate memory and the number of iterations was unpredictable, therefore conditionally tending to infinity.
Killed then a day to find a leak, then scored.
I made it so that when a given amount of memory was eaten, the program ended and then restarted from the same place and so on ad infinitum. Works successfully to this day.
Yes, a perversion, but it’s not a fact that the leak could not be found at all, because this is PHP.
I have to use mb_ functions instead of preg_ in the regex, because at least once a week I stumble upon regex + page pairs in which preg_ causes php to crash with segfault.
Please provide a minimally finished piece of code with a loop where you delete a variable using unset
Also at one time I played with the consumed memory in cycles for a long time, I did not find the reason.
As a solution, nohup is practically an analogue of a fork, but the new process is launched independently, it can be passed the current loop iterator and, in principle, any parameters.
In the loop, you can check how much memory is used, and set the bar to N bytes / KB / MB, as soon as the memory starts to approach the limit, we break the loop and start the process through nohup with new parameters.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question