L
L
La2ha2013-01-08 08:28:53
Laravel
La2ha, 2013-01-08 08:28:53

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>

Memory eats up Test::create($text);
I understand that if you call the parameter clearing function in the class destructor, then the memory consumption should not return, but the problem is that the class follows the framework class, which in turn calls another class, in general, there are many framework files involved that I would not want to change.

Answer the question

In order to leave comments, you need to log in

8 answer(s)
L
La2ha, 2013-01-09
@La2ha

Problem solved. The database query profiler has been enabled. Which saved all requests to a variable.

E
egorinsk, 2013-01-08
@egorinsk

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.

@
@resurtm, 2013-01-08
_

Memlik somewhere in third-party libraries?

R
return, 2013-01-08
@return

You can try periodically for every hundredth iteration

gc_collect_cycles()

This is a forced launch of Garbage Collector

L
La2ha, 2013-01-08
@La2ha

I created an object and destroyed it through unset() and assigned NULL too, the result is the same
upd: not there

W
WEBIVAN, 2013-01-08
@WEBIVAN

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.

K
KEKSOV, 2013-01-08
@KEKSOV

Please provide a minimally finished piece of code with a loop where you delete a variable using unset

I
Ivan, 2013-01-08
@Praeses

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 question

Ask a Question

731 491 924 answers to any question