I'm running a PHP script (CLI) to calculate a big thing. It will probably be an (almost) infinite loop but after 17000 runs I run out of memory. Can I some way dump some memory (say like every 1000th run) so I can keep running it forever?
/Max
I'm running a PHP script (CLI) to calculate a big thing. It will probably be an (almost) infinite loop but after 17000 runs I run out of memory. Can I some way dump some memory (say like every 1000th run) so I can keep running it forever?
/Max
In PHP 5.3: gc_collect_cycles().
gc_enable() first.) However, PHP 5.3 does have an improved garbage collector that can detect circular references. That may be what you need; I know it's helped out some of my long-running CLI scripts. So basically: upgrade PHP. If you still get the problem, try that function. If it still persists, then post more code... something else is wrong.Here's an article on memory management in PHP.
You can also increase available memory in PHP if you aren't already maxed out.
Keep in mind:
Sometimes you can't just "dump memory". Depending on your algorithm, you may need the results from previous calculations for future calculations.
The key here might be to break your problem up into smaller problems and solve them individually.
Check out this article on Dynamic Programming.
Provided this is not a server application, I would recommend breaking up the script into cron jobs.
Anyway, the problem is more than likely that you're not being careful with your variables. If you're running out of memory, then then you need to remember to unset() your variables. As well, watch your variable scope. If a variable never goes out of scope, PHP GC cannot clean it up until you remove all references to it.