Shawn
Shawn

Reputation: 34279

php high memory problem

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.

Upvotes: 7

Views: 1062

Answers (2)

symcbean
symcbean

Reputation: 48387

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');

Upvotes: 1

Christoph Fink
Christoph Fink

Reputation: 23113

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

Upvotes: 1

Related Questions