![]() |
Services | Software | Partners | Articles | Contact |
Drupal site excessive memory usage traced to APC and php5-memcachedWe were recently troubleshooting a client site running Drupal 7.x, and main complaint was high memory usage on all the site's pages. We were able to diagnose and solve two main causes that range from the common to unusual. This is a Drupal 7 Commerce site with 173 modules, and 3 themes enabled. Apache Solr is used for search, and there is custom code to talk over the network to a non-Drupal backend server. The site runs on a Debian Squeeze Xen VPS. For most of the site's pages, the client was seeing high memory usage, as follows: Problem: high memory usageWhen every page load has extremely excessive memory usage, this could be a bottleneck for the site scaling well, since the server has to have enough memory to cope with many pages at the same time using lots of memory. The first access to a page, where APC has not yet cached anything, would look like this in devel:
Subsequent access would show less memory usage, since APC caches the Drupal PHP files, like so:
Some page even reported up to 192 MB of peak memory! That is still excessive. For a site with that many modules, we expected that memory usage would be high, but not to that extent. Solutions to high memory usageIncreasing APC shared memory sizeFirst, the allocated shared memory for APC was not enough. The apc.shm_size parameter for APC is set to the default of 32MB. The code base with that many modules needed at least double that or more. So, increasing this to 96MB, solved that part. To do so on Debian or Ubuntu, change the following line in the file /etc/php5/apache2/conf.d/apc.ini apc.shm_size = 96 Replacing php5-memcached with php-memcacheThe other big cause of excessive memory usage was quite unusual. It was the use of php5-memcached (notice the "d") to connect PHP with the memcached daemon, rather than the more commonly used php5-memcache (without a "d"). For some unknown reason, the PHP memcached extension (from the Debian package php5-memcached) uses way more memory than the php5-memcache extension. In order to remedy this, do the following: $ sudo aptitude purge php5-memcached $ sudo aptitude install php5-memcache What a difference a "d" makes! ResultsThe results after doing both of the above things were dramatic. Instead of 175 MB per page, it is now a more typical (for a complex site): 60 MB!
Note that these figures are not absolute, and will vary from distro to distro and server to server, depending on what modules you have enabled in PHP and Apache, and many other factors. What matters is the comparative figures, not absolute figures. For example, the same site on an Ubuntu Server LTS 12.04, which we used for our lab servers:
It will be different on CentOS. Is your Drupal site slow? |



I don't see any difference,
I don't see any difference, you can have both these PHP modules installed and choose what to use. Without optimizing the cache files, its just flaw to say one did better performance than other. For memcached you have to use extra initiative what database load does it caches. A bit of tweaking can give more performance, and to squeeze the performance, you need time and patience...
Nice article though, btw how I can RSS your articles ???
Not performance, memory usage
This article is not about performance of one configuration over the other. It is about excessive memory usage when one is used but not the other.
This was verified on both Debian and Ubuntu. Are you on CentOS?
You can subscribe to our Drupal performance articles via the Drupal Planet.
sorry didn't notice your
sorry didn't notice your blogs in Drupal Planet before
As you mentioned APC is also used and you have increased the memory allocation to APC, though enabling user cache in APC is one viable option IMO. I haven't performed the test myself about this memcache or memcached pecls, but memcached needs some configuration with external library also used. So, the issue might be memcached already installed but didn't serve any caches (just a guess). It would be interesting to learn from you, if you have already tested what memcached/memcache was serving.
I retired RHEL/CentOS long time ago, I use Ubuntu and FreeBSD these days.
Yes, there were two issues
Yes, there were two issues, but one alone did not solve the excessive memory usage issue.
Memcache was working fine, but pages were using too much memory with php5-memcached, and back to normal with php5-memcache.
Trouble replicating
I tried to see if I could replicate this on an Ubuntu 12.04 VM and can't see any difference in memory use. This was on a fresh site install with lots of code, but little content. It makes me wonder if there's something in the Drupal Commerce stack that is triggering it. Also, was using the 7.x-1.0 release of the memcache Drupal module which could be related if you're on a different version.
Hmmm. Interesting ...
Interesting observation.
We verified it on Ubuntu 12.04 (dedicated server) as well as Debian Squeeze (Virtual Machine).
Can't see why Drupal Commerce, or anything else use php5-memcached or php5-memcache differently than other modules? It is all cache_set() and cache_get() after all.