PHP CLI - Detect where my memory is wasted
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 migh开发者_开发技巧t 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).
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
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.
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.
精彩评论