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.
| Component | What it is | Role |
|---|---|---|
| Cache | Collection of slabs for one object type | Entry point for allocation and free calls |
| Slab | One or more contiguous pages | Unit obtained from and returned to the page allocator |
| Object | Fixed-size slot inside a slab | Memory handed to the caller |
| Free list | Chain of unused slots in a slab | Finds a free object in constant time |
| Per-CPU cache | Small stack of objects per processor | Avoids locks on the fast path |
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.
| Allocator | Allocation unit | Best suited for | Main weakness |
|---|---|---|---|
| Slab allocator | Fixed-size objects | Frequent small kernel objects | Memory held in partially used slabs |
| Buddy allocator | Power-of-two page blocks | Page-sized and larger regions | Internal fragmentation for small sizes |
| General heap | Variable-size blocks | Arbitrary user-space requests | Search cost and fragmentation |
| Pool allocator | Fixed-size blocks, one pool | Simple embedded systems | No sharing between pools |
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.
| Implementation | Design focus | Status |
|---|---|---|
| SLAB | Faithful Bonwick design with per-CPU and shared queues | Removed in kernel 6.8 |
| SLUB | Fewer queues, metadata in the page structure, strong debugging | Default and only remaining option |
| SLOB | Simple list-based allocator with minimal overhead | Removed in kernel 6.4 |
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_nomergewhen 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.
| Risk | Cause | Mitigation |
|---|---|---|
| Slab fragmentation | Long-lived objects pin partial slabs | Separate caches by lifetime |
| Use-after-free exploitation | Freed slot reused by attacker object | Free list randomization and hardening |
| Cross-cache attacks | Merged caches share slabs | Disable merging, dedicated caches |
| Silent memory corruption | Writes beyond object boundary | Red zones and memory poisoning |
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.