Slab allocation

Also known as: slab allocator

On this page 7

Slab allocation is a kernel memory allocator design that keeps caches of preinitialized objects of a single size and type, carved out of contiguous groups of pages called slabs. In operating systems, it sits above the page allocator in memory management and serves the small, frequent allocations that kernel code makes for structures such as inodes, file descriptors and network buffers.

The technique was introduced by Jeff Bonwick for the SunOS 5.4 kernel in 1994 and later adopted, in several variants, by Linux, FreeBSD and other systems. Its central idea is that object construction is often more expensive than memory allocation, so a slab allocator keeps freed objects in a ready state for the next request.

How a slab allocator organizes memory

A slab allocator arranges memory in three levels: caches, slabs and objects. Each cache serves exactly one object size, each slab is one or more physically contiguous page frames obtained from the page allocator, and each slab is divided into equally sized object slots.

ComponentWhat it isRole
CacheCollection of slabs for one object typeEntry point for allocation and free calls
SlabOne or more contiguous pagesUnit obtained from and returned to the page allocator
ObjectFixed-size slot inside a slabMemory handed to the caller
Free listChain of unused slots in a slabFinds a free object in constant time
Per-CPU cacheSmall stack of objects per processorAvoids locks on the fast path
Building blocks of a slab allocator

The allocator tracks slabs in three states: full, partial and empty. Allocation first takes an object from a partial slab, which keeps memory densely packed; only when no partial slab exists does the cache request new pages from the buddy allocator. Empty slabs can be returned to the system when memory runs low.

Why slab allocation reduces fragmentation and overhead

Slab allocation reduces internal fragmentation because each cache matches its object size closely instead of rounding requests to a power of two. It reduces external fragmentation because objects of the same lifetime and size are grouped together, so whole slabs tend to become free at once.

Further gains come from object caching and hardware awareness. A freed object keeps its constructed state, such as initialized locks and list heads, so the next allocation skips that work. Slab coloring shifts the starting offset of objects in different slabs so that they map to different CPU cache lines, which reduces cache conflicts.

The following table compares slab allocation with the other allocators that commonly appear in kernel memory management.

AllocatorAllocation unitBest suited forMain weakness
Slab allocatorFixed-size objectsFrequent small kernel objectsMemory held in partially used slabs
Buddy allocatorPower-of-two page blocksPage-sized and larger regionsInternal fragmentation for small sizes
General heapVariable-size blocksArbitrary user-space requestsSearch cost and fragmentation
Pool allocatorFixed-size blocks, one poolSimple embedded systemsNo sharing between pools
Slab allocation compared with related allocators

Slab, SLUB and SLOB in the Linux kernel

Linux has shipped three implementations behind the same kmem_cache interface, and the generic kmalloc call is itself built on a set of size-class caches. The original SLAB design followed Bonwick closely; SLUB simplified it and became the default in kernel 2.6.23; SLOB targeted very small systems.

ImplementationDesign focusStatus
SLABFaithful Bonwick design with per-CPU and shared queuesRemoved in kernel 6.8
SLUBFewer queues, metadata in the page structure, strong debuggingDefault and only remaining option
SLOBSimple list-based allocator with minimal overheadRemoved in kernel 6.4
Linux slab implementations

SLUB keeps per-CPU active slabs and stores free pointers inside the free objects themselves, which reduces metadata. It also supports cache merging, in which caches with compatible sizes and flags share slabs, lowering the total number of caches at the cost of weaker isolation between object types.

Monitoring and tuning slab caches

Administrators observe slab usage to diagnose kernel memory growth that does not appear in process statistics. On Linux, /proc/slabinfo lists every cache, the slabtop tool shows the largest consumers, and /proc/meminfo separates reclaimable from unreclaimable slab memory.

Reclaimable caches, such as the dentry and inode caches, shrink automatically under memory pressure, so a large value is not necessarily a leak. A steadily growing unreclaimable slab value, by contrast, points to a driver or subsystem that allocates objects without freeing them.

  • Use slub_debug boot options to enable red zones, poisoning and owner tracking when hunting corruption.
  • Disable cache merging with slab_nomerge when isolation between object types matters for security.
  • Compare active objects with total objects to estimate how much memory partial slabs waste.

Limitations and security considerations

Slab allocation is efficient only for objects of known, repeated sizes. Large or highly variable allocations are better served by the page allocator or by vmalloc, and workloads that allocate many objects and then free most of them can leave sparsely populated slabs that cannot be returned to the system.

The predictable layout that makes slabs fast also helps attackers. Use-after-free and heap overflow exploits often rely on placing a controlled object in a slot that a freed kernel object occupied. Modern kernels respond with mitigations that trade some speed for safety.

RiskCauseMitigation
Slab fragmentationLong-lived objects pin partial slabsSeparate caches by lifetime
Use-after-free exploitationFreed slot reused by attacker objectFree list randomization and hardening
Cross-cache attacksMerged caches share slabsDisable merging, dedicated caches
Silent memory corruptionWrites beyond object boundaryRed zones and memory poisoning
Common risks and mitigations

Put simply

Think of a busy restaurant kitchen. Instead of washing and laying out a new plate for every order, the staff keep stacks of ready plates in each size. A soup bowl goes on the soup stack. A dinner plate goes on the dinner stack. When an order comes in, someone grabs the right plate at once.

Slab allocation works the same way inside a computer. The system keeps separate stacks of memory pieces for each common kind of data. Getting one is fast, returning one is fast, and nothing is wasted on odd sizes.

In practice

A system administrator runs a file server with 64 GB of RAM that holds millions of small files. After a nightly backup scans the whole file tree, free memory drops sharply, yet no process shows high usage. Running slabtop reveals that the dentry and inode caches occupy about 20 GB.

Because /proc/meminfo reports this memory as reclaimable slab, the administrator recognizes it as useful caching rather than a leak. When applications later need memory, the kernel shrinks those caches automatically. The investigation ends without a reboot, and the team adds slab metrics to monitoring so that future growth in the unreclaimable part, which would indicate a real driver leak, raises an alert.