views:

2272

answers:

3

What's a good way to profile a PHP page's memory usage? For example, to see how much memory my data is using, and/or which function calls are allocating the most memory.

  • xdebug doesn't seem to provide memory information in its profiling feature.

  • xdebug does provide it in its tracing feature. This is pretty close to what I want, except the sheer amount of data is overwhelming, since it shows memory deltas for every single function call. If it were possible to hide calls below a certain depth, maybe with some GUI tool, that would solve my problem.

Is there anything else?

+2  A: 

Well, this may not be exactly what you're looking for, but PHP does have a couple of functions built-in that will output memory usage. If you just wanted to see how much memory a function call is using, you could use memory_get_peak_usage() before and after a call, and take the difference.

You use the same technique around your data using the very similar memory_get_usage().

Pretty unsophisticated approach, but it's a quick way to check out a piece of code. I agree that xdebug mem deltas can be too verbose to be useful sometimes, so I often just use it to narrow down to a section of code, then dump out specific memory usage for small pieces manually.

zombat
+3  A: 

Check out Rasmus Lerdorf's talk called "Simple is Hard" (http://talks.php.net/show/froscon08 for slides, http://www.youtube.com/watch?v=RWRYX5eJbG0 for video). He goes over a lot of useful tools such as "inclued" (http://pecl.php.net/package/inclued), xdebug, and KCacheGrind.

TML
+2  A: 

I found a patch to xdebug, which provides memory information in the profiles. So far it's working very well.

JW