7

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.

3
  • running profiler can give you an idea of scripts taking long time runs Commented Jun 8, 2011 at 9:55
  • 1
    Actually I'm using xdebug as the profiler, but I didn't see much info about memory usage. Commented Jun 8, 2011 at 9:58
  • 1
    firebug also helps and i had a good article on it devzone.zend.com/article/2899 Commented Jun 8, 2011 at 10:01

2 Answers 2

1

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:

<?php
 function logit()
 {
    $line = $_SERVER['REQUEST_URI'] 
       . ' ' . memory_get_peak_usage(true);
    // if stderr works...
    $stderr = fopen('php://stderr', 'w');
    fputs(stderr, date('r') . ' ' . $line);
    fclose($stderr);
    // alternatively
    openlog("php_memory", LOG_PID | LOG_PERROR, LOG_LOCAL0);
    syslog(LOG_INFO, $line); 
    closelog();
 }
 register_shutdown_function('logit');
Sign up to request clarification or add additional context in comments.

Comments

1

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

regards

Comments

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.