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).

Sebastian Hoitz
  • 9,343
  • 13
  • 61
  • 77

3 Answers3

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

Spudley
  • 166,037
  • 39
  • 233
  • 307
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.

symcbean
  • 47,736
  • 6
  • 59
  • 94
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.

Htbaa
  • 2,319
  • 18
  • 28
  • 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). – symcbean Feb 03 '11 at 14:59
  • 5.3 only add a "circular reference detector". The rest of the GC is identical – KingCrunch Feb 03 '11 at 15:05
  • 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 :-). – Htbaa Feb 03 '11 at 15:07