General cache flushing technique with php infinite loop

Hi, the question is when you are doing an endless loop with php, how do you control memory cleanup? A rough example is to get the result or update the result from / to mysql at infinity during the loop.

General methods needed. Thanks.

PS - all sworn enemies and PHP errors were replaced by switching to python completely ...

+4
source share
2 answers

As far as I know, in memory PHP is freed when a variable goes beyond. But there are other problems:

  • links around - PHP 5.3 should solve it - it also allows you to run GC whenever you want
  • If PHP takes, for example, 5 MB of memory in the first iteration, the process will occupy this memory even if the subsequent iterations are 1 MB example
  • You have to free some things manually (for example, before the database results)

Using a scripting language for a startup-like process is a very bad idea.

Try to do it differently:

  • Write a script that will process the amount of data that will take approximately 55-60 seconds to run.
  • Add a cron job to run it every minute.
  • Add some kind of mutual exception to the script so that cron does not run simultaneous scripts - you can synchronize it across the database table (using SELECT FOR UPDATE)
+2
source

As with PHP 5.3, you can explicitly start the GC cycle with gc_collect_cycles() , as described here .

Before that, it was out of your control, and you would have to wait until PHP decided that it was time to take out the garbage on its own - either trying to exceed the memory limit with a significant amount of used but not attached memory objects or sacrificing a goat under the full moon and hoping for the best.

+2
source

All Articles