Memcached Memory Allocation

Memcached Memory Allocation

Memcached uses slab-based memory allocation rather than allocating memory separately for each cached item. This approach reduces memory fragmentation, but the slab layout should still match the typical size distribution of your cached objects to avoid wasting memory.

How Slab Allocation Works

Instead of allocating memory item by item, Memcached groups memory into slab classes. Each slab contains one or more 1 MB pages, and every page is divided into equal-sized blocks called chunks.

Page A 1 MB memory area assigned to a slab class.
Chunk A fixed-size block inside a page where a cached item is stored.

When an item is stored, Memcached checks its size and selects a slab class with a suitable chunk size. If an appropriate allocation does not exist, a new slab class can be created with chunks sized for that type of object.

If an existing cached value is updated and becomes too large for its current chunk, Memcached moves the item to another slab class with a larger allocation.

Memcached slab memory allocation
Memcached pages are divided into equal-sized chunks and grouped into slab classes.

Memory Fragmentation and Waste

Slab allocation helps protect Memcached from classic memory fragmentation as cached data expires and is replaced. However, inefficient slab sizing can still waste memory when only a small number of chunks in each allocated page are actually used.

Optimization goal: Choose slab-size progression that fits the normal distribution of your cached item sizes. Too many partially filled slab classes can reduce effective cache capacity.

Adjust the Slab Growth Factor

The slab growth coefficient can be changed from the platform while the application environment is running.

1Open Memcached configuration

Click Config next to the Memcached node, open the conf directory, and select the memcached configuration file.

2Edit the OPTIONS parameter

The source demonstrates the following custom configuration:

OPTIONS="-vv 2>> /var/log/memcached/memcached.log -f 2 -n 32"
Memcached slab growth factor configuration
Adjust the Memcached startup options from the configuration file.
-f 2 Sets the slab growth factor to 2. In the source example, this produces 14 slab classes with chunk sizes that approximately double between classes.
-n 32 Defines the minimum allocation used for the key, flags, and cached value.

Example with Custom Slab Settings

With the custom -f 2 -n 32 configuration, the source shows slab classes such as:

#  Item_Size  Max_age  Pages  Count  Full?  Evicted  Evict_Time  OOM
3     320B      550s      1     113    yes        0           0    0
4     640B      681s      1     277    yes        0           0    0

The corresponding memory snapshot in the example is:

total       used       free      shared    buffers     cached
Mem:          128         84         43           0          0         70
-/+ buffers/cache:         14        113
Swap:           0          0          0

Compare with Default Settings

The source then restores the default Memcached options:

OPTIONS="-vv 2>> /var/log/memcached/memcached.log"

With the default slab-growth behavior, more intermediate chunk sizes appear:

#  Item_Size  Max_age  Pages  Count  Full?  Evicted  Evict_Time  OOM
5     240B      765s      1      27    yes        0           0    0
6     304B      634s      1      93    yes        0           0    0
7     384B      634s      1     106    yes        0           0    0
8     480B      703s      1     133    yes        0           0    0
9     600B      634s      1      57    yes        0           0    0

The corresponding memory snapshot shown by the source is:

total       used       free      shared    buffers     cached
Mem:          128         87         40           0          0         70
-/+ buffers/cache:         17        110
Swap:           0          0          0
These figures are examples from the source environment. The best slab configuration depends on the actual object-size distribution and workload of your application.

Large Memory Pages

The source also notes that the -L parameter can be added to request larger memory pages. This can reduce Translation Lookaside Buffer (TLB) misses and may improve performance on suitable systems.

Test before production use: Memory-allocation tuning changes how cache capacity is distributed. Compare cache hit rate, evictions, memory utilization, and application latency before keeping a custom configuration.
Memory allocation summary: Memcached organizes RAM into 1 MB pages and fixed-size chunks, groups those chunks into slab classes, and allows the slab growth factor and minimum item allocation to be tuned with -f and -n. The objective is to reduce wasted cache memory while maintaining efficient item allocation.

What’s next?