Engineering Blog

Memcached Memory Management

The  “memcached” is one of the most popular open source on-memory key-value caching systems. I will briefly talk about the design of memory management of memcached.

Chunk and Slab

This is the struct declaration of slabclass_t. Each slab class contains the same size of chunk, but different classes have different chunk sizes. The size is calculated by this algorithm:

<!--while (++i < POWER_LARGEST && size 

The content value factor is defined when memcached memory is deployed with -f, which can change the size between slab classes. For this loop, the size is multiplied by a specified factor. 


You can get this information by adding -vvv and you can use the command

Every time when a new memory needs to be allocated. It will scan the slab class to find the most suitable class to store the chunk.

Rebalance and reassign slab memory:

From memcached’s wiki:

overview:Memcached 1.4.11. Fixes race conditions and crashes introduced in 1.4.10. Adds the ability to rebalance and reassign slab memory.

Slab Reassignment

Long running instances of memcached memory may run into an issue where all available memory has been assigned to a specific slab class (say items of roughly size 100 bytes). Later the application starts storing more of its data into a different slab class (items around 200 bytes). Memcached could not use the 100 byte chunks to satisfy the 200 byte requests, and thus you would be able to store very few 200 byte items.

1.4.11 introduces the ability to reassign slab pages. This is a beta feature and the commands may change for the next few releases, so please keep this in mind. When the commands are finalized they will be noted in the release notes

Slab Automove

While slab reassign is a manual feature, there is also the start of an automatic memory reassignment algorithm.

From the source code in slabs.c we can see, memcached uses two threads to monitor the slabs class, one is to do maintenance and another one is to do the re-balance the class.

Memcached defines a global variable struct slab_rebalance slab_rebal, which is used to store the start, end information of slab. s_clsid is the source slab id and d_clsid is the destination slab id. The detailed blog in Chinese memcached源码分析—–slab automove和slab rebalance could be helpful.

Memory Pool

Memcached implements its own memory pool, which is used to avoid system memory allocation and memory fragmentation. That will make your memory efficient and easy to manage. Here is a demo implementation of memory pool. Basically it is a large pre-allocated chunk of memory.

 mem_avail) {
        return NULL;
    /* mem_current pointer _must_ be aligned!!! */
    if (size % CHUNK_ALIGN_BYTES) {
        size += CHUNK_ALIGN_BYTES - (size % CHUNK_ALIGN_BYTES);
    mem_current = ((char*)mem_current) + size;
    if (size 

About Mark Duan

Related Posts