0

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

4
  • 2
    what are you looping through, is there any way to batch load the data so you're not loading it all at once? Commented Sep 16, 2010 at 20:50
  • 2
    You would really need to show some code here Commented Sep 16, 2010 at 20:53
  • 2
    If you provide some code / details on what you are doing in the loop, perhaps somebody can suggest ways to use less (or constant) memory. Commented Sep 16, 2010 at 20:54
  • You are not releasing memory in your loop. there is no reason it should run out of memory if you are properly unset()'ng your variables. (watch out for circular references!) Commented Sep 16, 2010 at 20:55

3 Answers 3

1

In PHP 5.3: gc_collect_cycles().

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, will try after i upgrade my server. I'll be back if it works or not
Well, to be honest, I doubt that function alone will help. (Plus, you may need to 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.
0

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.

Comments

0

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.

2 Comments

I'm rewriting one variable over and over again. Check variable, if not match rewrite variable and check again.
Are you concatenating this variable by chance? PHP should be freeing the memory if you aren't. As a test, try doing an unset() on this variable before you re-assign it's value. I have no clue if that will actually help or not, but it's worth a shot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.