I'm working with a site that was built in Drupal and generally runs well. After some optimization it takes 20-75MB of memory and 0.5-2s for most requests. But once in a while it will take 15-20s and the process will run over 1GB+ memory usage.

I've tried tracing the time and memory usage at the beginning and end of the PHP execution (putting the code in Drupal's index.php) and it shows nothing unusual (it reports a difference of 1 seecond between the start and end, and memory usage goes up 72MB in one example). Somehow PHP is using a lot of resources but not within the actual PHP code.

This was first observed with apache. Moving to a new server (using standard Ubuntu packages) made the site a bit more reliable, but it still happens. Even after switching to nginx+php-fpm and turning off apache entirely, the php-fpm processes show the same memory usage pattern. Other than standard PHP modules the only extras are memcache, stats, and xhprof.

What could reveal why PHP is using so much memory outside the script itself?


I believe memory leaks are quite common in php and it's libraries. Also usually PHP opcode cache use most of the memory provided to PHP.

Are you 100% sure that you have xhprof dump files for problem situation and it doesn't show excessive memory usage?

If you are right - you need to create and analyze core dumps of suspicious PHP processes with gdb - it requires some basic C knowledge.

Also check bug trackers for your php and libs version.

| improve this answer | |

It turned out that a function was using register_shutdown to run after the end of the main code (to show debugging information). This explains why it wasn't showing in the logging that I did or in xhprof, which ended before the shutdown function. Modifying this seems to have resolved the issue for now.

| improve this answer | |

Chances are you've got an inefficient module enabled in Drupal. Start by disabling all of those and seeing if that changes anything.

Additionally, it sounds like your trace isn't a real trace, have a look at this to get a better trace: http://www.youtube.com/watch?v=eF-p--AH37E

An Opcode cache (APC) will only get you so far in the name of performance, so I'd look there last to improve performance.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.