Memcached Memory Allocation
Memcached Memory Allocation
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.
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.
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.
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"
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
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.
-f and -n. The objective is to reduce wasted cache memory while maintaining efficient item allocation.
