Memcached - Stats sizes
Memcached - Analyzing Item Sizes with stats sizes
Section titled “Memcached - Analyzing Item Sizes with stats sizes”The stats sizes command provides a histogram of item sizes currently stored in the cache. This is an invaluable tool for understanding your application’s memory usage patterns and for fine-tuning Memcached’s slab allocator for maximum efficiency.
Syntax
Section titled “Syntax”The command is simple and takes no arguments.
stats sizesExample
Section titled “Example”stats sizesSTAT 96 15STAT 120 4ENDInterpreting the Output
Section titled “Interpreting the Output”The output is a series of lines, each with two numbers. STAT <size> <count>.
size: The size of the chunk in bytes. Memcached rounds up the actual item size to the nearest available chunk size.count: The number of items currently stored that fit into this chunk size.
In the example above, STAT 96 15 means there are 15 items in the cache that are stored in 96-byte chunks. Their actual size could be anywhere from 1 to 96 bytes (plus metadata overhead).
Practical Application: Optimizing Slab Allocation
Section titled “Practical Application: Optimizing Slab Allocation”The primary use of stats sizes is to combat memory waste. When an item is stored, Memcached places it into the smallest available slab class that can fit it. If your item is 100 bytes but the next available slab class has a chunk size of 120 bytes, you are wasting 20 bytes per item. This is called internal fragmentation.
By analyzing the output of stats sizes on a production load, you can identify common item sizes. If you find a large number of items are, for example, between 100-110 bytes, but they are all being stored in a 144-byte slab, you could configure Memcached at startup to create a more optimally sized slab.
# Example: Start memcached with a custom slab size of 112 bytes# This can be done if `stats sizes` shows many items slightly over 96 bytesmemcached -o slab_chunk_max=112Fine-tuning slab sizes based on real-world data from this command is an advanced optimization technique that can significantly improve memory efficiency, allowing you to store more items in the same amount of RAM.