How to monitor and tune APCu

Have you already been confronted to APCu memory exhaustion issues like below?

PHP Warning: apc_store(): Unable to allocate memory for pool.

This means the value for apc.shm_size is too low.

Increase your apc.shm_size value

First things first, you need to understand what's the current value set to. The easiest way is to check the apc.shm_size setting from the command line.

$ php -i | grep apc.shm_size
apc.shm_size => 8M => 8M

So, let's say you're getting memory exhaustion errors with 8M, then you'll need to increase the value in php.ini, e.g.

apc.shm_size = 16M

Then, restart php-fpm

$ sudo service php7.0-fpm restart
[ ok ] Restarting PHP 7.0 FastCGI Process Manager: php-fpm7.0.

As you can see, this is pretty arbitrary, as you don't have much evidence to properly tune this value. How can you then effectively monitor APCu to better understand what your site actually needs?

Enter APCu monitoring

I recommend you use the apc.php script, which is part of the official APCu - APC User Cache repo. It'll give you simple, yet meaningful data such as:

APCu memory usage

The script breaks down free and used memory, and also gives a clear overview on the percentage of cache hits and misses you're getting. As with any caching system, the more hits the better.

APCu memory usage pie chart, broken down by free and used memory

Frédéric makes a good point. The pie chart is only there to give you a quick and easy visual representation of your APCu usage. But take this with a grain of salt. E.g. if you're seeing memory exhaustion errors but your pie chart is mostly green, then it means it cannot be trusted and you need to correlate more data to understand exactly what's going on.

APCu cache fragmentation

The fragmentation is a bit more complex to understand as you're overloaded with data. Simply keep in mind the lower the fragmentation, the better.

Detailed view of APCu cache fragmentation

But not only that. The script comes with useful information such as:

Host stats

  • General Cache Information (APCu version, uptime...)
  • Cache Information (cached variables, hit rate...)
  • Runtime settings (APCu settings, e.g. apc.shm_size)

User Cache Entries

This one requires you edit the apc.php script and change the default password string to enable the feature.

defaults('ADMIN_USERNAME','apc'); // Admin Username
defaults('ADMIN_PASSWORD','password'); // Admin Password - CHANGE THIS TO ENABLE!!!

Don't overlook this as it well worth the effort. You'll get granular data on every single cache entry, such as:

  • Number of hits
  • Size
  • Last modified, created at and timeout (if set)
  • Path to file (needs you to double-click on any entry)

You also have the option to arbitrarily delete any cache entry or all APCu cache. Nifty.

APCu monitoring alternatives?

There are other, more advance options, that were mostly built for APC monitoring back in the day. Obviously they aren't so relevant for APCu, but watch these projects regardless as they might evolve:

Aurelien Navarre

Senior Technical Solutions Analyst @Acquia - Drupalist by day, DevOps by night.

Lyon, France