Common technique in cleaning cache with php infinite loop
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:
- circullar references - PHP 5.3 should solve it - it also allows to run GC when you want
- 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
- 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:
- Write a script which would processs amount of data that it would take approximately 55-60 seconds to run.
- Add a cron job to run it every minute.
- 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.
精彩评论