Take the 2-minute tour ×

Here is the stack of my web app. Simply nginx + php-fpm + php5.3 + mysql + memcache. Recently we deployed some refactored code. Which involves SQL refactoring and caching adjustments. We found that after the deployment, the server load had an sharp growth. The memory usage climbed even sharper. And from the top command many php-fpm processes were using 2 times of memories than before. So yes there's something wrong in the deployed code. The problem only occured in production env, in test & dev envs it's all fine, so it's correlated to traffic.

My question is generally how can I find out which requests(scripts) or which parts of my code are consuming too much memory? What's the easiest way to find out?

Thanks a lot.

share|improve this question
running profiler can give you an idea of scripts taking long time runs –  Mian Khurram Ijaz Jun 8 '11 at 9:55
Actually I'm using xdebug as the profiler, but I didn't see much info about memory usage. –  Shawn Jun 8 '11 at 9:58
firebug also helps and i had a good article on it devzone.zend.com/article/2899 –  Mian Khurram Ijaz Jun 8 '11 at 10:01
brilliant, This helps me too –  etbal Jun 8 '11 at 11:06

2 Answers 2

up vote 1 down vote accepted

Create an include file to log the memory usage when the program exits, then map it using auto-prepend. If this were apache, then it'd be safe to write this to stderr and it would appear in the error_log - not sure if this works with nginx:

 function logit()
    $line = $_SERVER['REQUEST_URI'] 
       . ' ' . memory_get_peak_usage(true);
    // if stderr works...
    $stderr = fopen('php://stderr', 'w');
    fputs(stderr, date('r') . ' ' . $line);
    // alternatively
    openlog("php_memory", LOG_PID | LOG_PERROR, LOG_LOCAL0);
    syslog(LOG_INFO, $line); 
share|improve this answer

We are using XHProf for memory profiling. It's not perfect, but you get a pretty good sense of whats happening. If you are running it more then once (which is what you need to do to find the spiking reqeust) I also recommend to use the following GUI: XHPROF GUI


share|improve this answer

Your Answer


