开发者

Common technique in cleaning cache with php infinite loop

开发者 https://www.devze.com 2023-01-24 14:32 出处:网络
Hi the question is when you are executing infinite loop with php, how do you control memory clean up ?

Hi the question is when you are executing infinite loop with php, how do you control memory clean up ? The rough example is to get the result or to update the result f开发者_Python百科rom / to mysql in infinity while loop.

Need any common methods . Thank you.

PS - all the nemesis and bugs of PHP were replaced by moving to python completely ...


As far as i know in PHP memory is freed when variable goes out of scope. But there are some other problems:

  1. circullar references - PHP 5.3 should solve it - it also allows to run GC when you want
  2. If PHP takes for example 5 MB of memory in first iteration its process will occupy this memory even if later iterations would take for example 1 MB
  3. You have to free some things manually (like for example mentioned before database results)

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

Try do it other way:

  1. Write a script which would processs amount of data that it would take approximately 55-60 seconds to run.
  2. Add a cron job to run it every minute.
  3. Add some kind of mutual exclusion to script so cron would not run concurrent scripts - you can synchronise it on database table (using SELECT FOR UPDATE)


As of PHP 5.3, you can explicitly trigger a GC cycle with gc_collect_cycles() as documented here.

Before that, it was out of your control, and you'd have to wait for PHP to decide it was time to take out the trash on its own - either by trying to exceed the memory limit with a significant amount of used-but-unattached memory objects or sacrificing a goat under the full moon and hoping for the best.

0

精彩评论

暂无评论...
验证码 换一张
取 消