Slab vs Buddy Allocator: Where Fragmentation Is Paid
Every allocator is a compromise between speed, wasted space and the ability to return memory to the operating system. Two designs sit at opposite ends of that compromise. A buddy allocator manages physical pages in power-of-two blocks, splitting and merging them to satisfy arbitrary sizes. A slab allocator asks for a run of pages once, carves it into fixed-size objects and hands those out without…
The buddy allocator and slab allocator represent two contrasting approaches to memory allocation. The buddy allocator splits and merges power-of-two blocks to satisfy arbitrary memory requests, aiming for predictability and coalescing ability but at the cost of internal fragmentation. In contrast, the slab allocator requests a run of pages, carves it into fixed-size objects, and maintains a free list per cache, offering fast allocation and deallocation for specific workloads but suffering from idle object waste and memory pining when demand drops.
A 70-byte allocation in a 64-bit system with 16-byte alignment would waste 45% of the 128-byte block in a buddy system, versus 12% in a slab cache, but the slab's per-cache residue could increase waste after long uptime. This sawtooth pattern of memory usage is more pronounced in the buddy system, where external fragmentation can occur when free pages are too small or scattered.
The choice of allocator often depends on the application's workload characteristics, as most general-purpose allocators combine elements of both. In stable size distributions, the slab allocator excels, while in highly variable workloads, the buddy allocator's coalescing capabilities may be more beneficial.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to