0

I'm using PHP + Zend Framework for several CLI daemons. They take up quite a bit of memory. I'm assuming the Zend Framework part might be causing this, but I want to have facts showing me where the memory is wasted.

How can I determine where memory is wasted? Is this just a trial + error process? Also how can I improve garbage collection (I read some articles that this might also be an issue causing big memory usage).

1

3 Answers 3

1

I'd recommend using XDebug's profiler, which should give you the answers you need.

The profiler will generate a cachegrind file, which you can view in a tool such as KCacheGrind to see where your program's bottlenecks and memory usages are.

Find out more on XDebug's profiler page: http://www.xdebug.org/docs/profiler

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

2 Comments

As far as I can tell the profiler only profiles time spent, not memory consumption?
@Sebastian Hoitz, xdebug.org/docs/execution_trace - it (xdebug) can trace memory consumption too
1

IME, PHP uses a huge amount of memory for parsing code - try building a simple script which does nothing other than explicitly including all the libs you're using and track the memory usage at start/finish. Compare this with what you see in your actual script.

Htbaa is partially correct - more recent versions of PHP have a much smarter garbage collector however the earlier versions still do garbage collection - they just don't find all the cases that the newer gc does. But because its garbage collection, you'll typically see something of a sawtooth in memory usage given a steady input load.

But good garbage collection won't fix bad code - if you've stored something in a variable which is not on the stack, then you need to unset it when you're done with it.

Comments

0

What version of PHP are you running? Only PHP >=5.3 has a decent garbage collector. PHP <=5.2 can eat all your memory when used to run daemon scripts.

3 Comments

Not necessarily - only if you've got a problem with isloated references (objects are particularly bad for this - but you get the same problem with arrays).
5.3 only add a "circular reference detector". The rest of the GC is identical
In my experience it doesn't take an awful lot to let a PHP 5.2 daemon process eat up all memory. My case was just a simple job worker based on Zend_Queue. Even when falling back to excluding objects and running an empty while loop it kept eating memory until the memory_limit was reached and the daemon killed itself. With PHP 5.3 these issues were gone. Sadly, no PHP 5.3 yet on the production servers :-).

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.