Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 37 ยท Performance and Sizing

Slab Allocator and Memory Sizing

Understand how Memcached carves memory into slab classes, how chunk sizes waste space, and how to estimate the RAM a cache needs. Runnable Python slab calculator.

When you start Memcached with -m 1024 you give it about 1 GB for items. But 1 GB of items does not mean 1 GB of your data. Every item carries a header, the allocator rounds sizes up, and the memory is split into classes at startup. Understanding this explains two common surprises: "my cache holds fewer items than I calculated", and "I get evictions with free memory showing in another size class".

This lesson explains the slab allocator that Memcached uses to avoid memory fragmentation, builds a calculator for it in Python, and then shows a practical sizing method. The numbers use the defaults of current Memcached releases; check your version's stats settings, since details can change between releases.

Why slabs exist

A general-purpose allocator such as malloc handles millions of tiny, short-lived objects badly: memory fragments, and over time the process uses much more RAM than the sum of its live objects. Memcached avoids this by pre-dividing memory:

  • Memory is allocated in pages of 1 MB each (this is also the default maximum item size).
  • Each page is assigned to a slab class and cut into equal chunks.
  • An item is stored in the smallest chunk that fits it. The unused tail of that chunk is wasted.
  • Chunk sizes grow geometrically by the growth factor -f (default 1.25), from a small minimum.

So class 1 might use 96-byte chunks, class 2 120-byte chunks, class 3 152-byte chunks and so on, up to the largest class. Each class has its own LRU list.

flowchart LR
  M["-m 1024 (1 GB of pages)"] --> P1["1 MB page -> class 1 (96 B chunks)"]
  M --> P2["1 MB page -> class 5 (240 B chunks)"]
  M --> P3["1 MB page -> class 12 (1184 B chunks)"]

Pages are handed to classes on demand. Once a page belongs to a class, it is not automatically given to another.

Computing the classes

Python Slab class table for growth factor 1.25

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first classes should read 96, 120, 152, 192, 240, 304 and 384, matching what you see in stats slabs on a default server.

Item size = header + key + value

An item stored in Memcached is bigger than its value: it has a header of roughly 48 to 56 bytes (pointers for the hash chain and the LRU, expiry time, flags, lengths, and optionally an 8-byte CAS number), plus the key, plus the value, plus a few bytes of terminator and padding. A 100-byte value with a 20-byte key therefore needs about 175 bytes and lands in the 192-byte class.

Measuring waste

Because chunks come in fixed sizes, every item wastes chunk - item size. With a growth factor of 1.25 the average waste is roughly 10% of memory, but a skewed size distribution can be much worse: if almost all your items are 200 bytes and the nearest chunk is 240, you lose 17%, every time.

Python How much memory is wasted by rounding?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A smaller factor (more classes, finer steps) wastes less per item, but more classes means more partially filled pages spread across them. A larger factor wastes more per item. The default of 1.25 is a balanced choice; change it only after looking at your actual item-size histogram.

The "calcification" problem and slab rebalancing

Pages are given to a class when it needs memory and, in early versions, never taken back. If your workload changes (for example you used to store many 300-byte items and now store 1 KB items), the old class keeps its pages while the new class has too few and starts evicting even though the server shows plenty of memory elsewhere. This is called slab calcification.

Modern Memcached reduces the problem with an automove background thread (slab_reassign, slab_automove) that moves pages from classes with spare capacity to those that evict. It is enabled by default in current versions; verify with stats settings and watch stats slabs for classes with evictions while others have free chunks.

Estimating the memory you need

A practical method:

  1. Count the working set: how many distinct items are read within the period you want to keep them (often the TTL window).
  2. Estimate item size: sample real items and compute header + key + value. Include compression if you use it (see Serialization and Compression).
  3. Add slab overhead: multiply by about 1.1 to 1.3, depending on the waste measurement above.
  4. Add headroom for growth and for the hash table and connection buffers: 10 to 20 percent more.
  5. Test with a real hit-ratio curve, as in Hit Ratio and Metrics: if doubling memory moves the hit ratio by only a point, you have enough.

Example: 5 million items of 400 bytes with a 56-byte header and a 30-byte key is about 486 bytes each, rounded up by classes to maybe 540 bytes on average, so about 2.7 GB. With 15% headroom, around 3.1 GB, so you would run -m 3200 (or split over several servers).

-m limits item memory, not the whole process. The hash table, per-connection buffers, threads and the operating system's needs come on top. Leave room, and never set -m equal to the machine's total RAM: the OOM killer would remove the process.

Large items

Items larger than the chunk limit (512 KB by default in modern versions) are stored as several linked chunks, and items above the -I limit (1 MB by default) are rejected. Big values also cost network time and block the connection for longer. If you find yourself raising -I, consider splitting the object or storing it in a blob store and caching only its metadata.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does Memcached divide memory into slab classes?

    Fixed-size chunks inside 1 MB pages keep memory usage predictable and fragmentation low.
  2. 2. An item needs 175 bytes and the nearest chunk size is 192 bytes. What happens to the extra 17 bytes?

    An item occupies a whole chunk, so the unused rest of the chunk is internal waste.
  3. 3. What is slab calcification?

    Pages stay assigned to a class, so a changed workload can leave memory in the wrong class unless automove rebalances it.
  4. 4. What should you add on top of item memory when choosing the -m value for a machine?

    -m limits item storage only; the rest of the process and the OS need memory too.

Summary

  • Memory is cut into 1 MB pages, assigned to slab classes with fixed chunk sizes.
  • Chunk sizes grow by the factor -f (default 1.25); items use the smallest chunk that fits.
  • Item size is header + key + value; the chunk rounding wastes part of every chunk.
  • Page assignments can become imbalanced; automove helps, and stats slabs shows it.
  • Estimate size from the working set, per-item size, slab overhead and headroom, then verify with the hit-ratio curve.

Next: Eviction and Capacity Planning