Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php high memory problem

Tags:

php

memory

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.

like image 503
Shawn Avatar asked Jun 08 '11 09:06

Shawn


2 Answers

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');
like image 162
symcbean Avatar answered Oct 04 '22 05:10

symcbean


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

like image 27
Christoph Fink Avatar answered Oct 04 '22 06:10

Christoph Fink