Search code examples
cachingapcphp

APC cache fragmentation (endless dilemma)


It's a frequently asked question, I know. But I tried every solution suggested (apc.stat=0, increasing shared memory, etc) without benefits.

Here's the screen with stats (you can see nginx and php5-fpm) and the parameters set in apc.ini:

APC Stats page

APC is used for system and user-cache entries on multiple sites (8-9 WordPress sites and one with MediaWiki and SMF).

What would you suggest?


Solution

  • Each wordpress site is is going to cache a healthy amount in the user cache. I've looked at this myself in depth and have found the best 'guesstimate' is that if you use user cache in APC keep the fragmentation under 10%. This can sometimes mean you need to try and reserve upwards of 10x the amount of memory you intend to actually use for caching to avoid fragmentation. Start where you are and keep increasing memory allocated until fragmentation stays below 10% after running a while.

    BTW: wordpress pages being cached are huge, so you'll probably need a lot of memory to avoid fragmentation.

    Why 10% fragmentation? It's a bit of a black art, but I observed this is where performance start to noticeabily decline. I haven't found any good benchmarks (or run my own controlled tests) however.

    This 10x amount to get this my seem insane, but root cause is APC has no way to defragment other than a restart (which completely dumps the cache). Having a slab of 1G of memory when you only plan to use 100-200m gives a lot of space to fill without having to look for 'holes' to put stuff. Think about bad old FAT16 or FAT32 disk performance with Windows 98--constant need to manually defrag as the disk gets over 50% full.

    If you can't afford the extra memory to spare, you might want to look at either memcached or plain old file caching for your user cache.