Buddy memory allocation is an algorithm that manages a region of memory as blocks whose sizes are powers of two, splitting a larger block into two equal halves called buddies when a smaller request arrives and merging free buddies back together when both are released. In memory management for operating systems, the buddy allocator is the classic method for handing out physically contiguous page frames, and it is the foundation of the Linux page allocator.
The algorithm was described by Harry Markowitz and Kenneth Knowlton in the 1960s and popularized by Donald Knuth. Its appeal lies in fast splitting and coalescing with very little bookkeeping, at the price of internal fragmentation when requests do not match a power of two.
How splitting and merging work
The allocator keeps one free list per order, where a block of order k contains 2^k minimum units, typically 4 KiB pages. A request is rounded up to the nearest power of two. If the free list for that order is empty, the allocator takes a block from the next higher order, splits it into two buddies, returns one and places the other on the lower free list. Splitting repeats until a block of the right size exists.
Freeing reverses the process. The allocator computes the buddy address with a single XOR of the block address and the block size. If the buddy is also free and of the same order, the two blocks coalesce into one block of the next order, and the check repeats upward. This address arithmetic is why merging is so cheap.
The table traces a 16 KiB request on a system whose smallest free block is 64 KiB.
| Step | Action | Resulting free blocks |
|---|---|---|
| 1 | Take one 64 KiB block (order 4) | None at order 4 |
| 2 | Split into two 32 KiB buddies | One 32 KiB block free |
| 3 | Split one 32 KiB block into two 16 KiB buddies | One 32 KiB and one 16 KiB free |
| 4 | Return one 16 KiB block to the caller | 32 KiB and 16 KiB remain free |
Orders, block sizes and fragmentation
Linux uses orders 0 to 10 by default, so the largest block is 1,024 pages, or 4 MiB on x86-64 with 4 KiB pages. Order-9 blocks of 2 MiB match the size of transparent huge pages, which makes the buddy allocator central to huge page availability.
| Order | Pages | Block size | Typical consumer |
|---|---|---|---|
| Order 0 | 1 | 4 KiB | Ordinary page faults, page cache |
| Order 1-3 | 2-8 | 8-32 KiB | Kernel stacks, slab caches |
| Order 4-8 | 16-256 | 64 KiB-1 MiB | Network buffers, DMA buffers |
| Order 9 | 512 | 2 MiB | Transparent huge pages |
| Order 10 | 1,024 | 4 MiB | Large contiguous driver buffers |
Rounding to powers of two causes internal fragmentation: a request for 33 KiB consumes a 64 KiB block and wastes almost half the block. In the worst case, waste approaches 50 percent. External fragmentation also occurs when free memory is scattered in small blocks whose buddies are still in use, so a large request fails even though the total free memory is sufficient.
Buddy allocation compared with other allocators
The buddy system works at page granularity and rarely serves small kernel objects directly. Kernels layer a slab allocator on top of it: the slab layer requests pages from the buddy allocator and carves them into fixed-size objects. The comparison below shows how the approaches differ.
| Allocator | Unit of allocation | Main strength | Main weakness |
|---|---|---|---|
| Buddy allocator | Power-of-two page blocks | Fast coalescing, contiguity | Internal fragmentation |
| Slab allocator | Fixed-size objects | No waste for common objects | Needs a page source below it |
| First-fit free list | Arbitrary sizes | Flexible sizing | Slow search, external fragmentation |
| Bitmap allocator | Single pages | Simple and compact | Slow search for long runs |
Virtual memory reduces the need for contiguity, because the page table maps scattered physical frames into a contiguous virtual range. Contiguous physical memory still matters for direct memory access without an IOMMU, for huge pages and for some kernel structures.
Reducing fragmentation in practice
Production kernels add several mechanisms around the basic algorithm to keep high-order blocks available. Each one addresses a different failure mode.
| Technique | Problem addressed | How it helps |
|---|---|---|
| Migrate types | Unmovable pages scattered everywhere | Groups movable and unmovable pages separately |
| Memory compaction | External fragmentation | Moves pages to rebuild contiguous blocks |
| Per-CPU page lists | Lock contention on order-0 requests | Serves single pages without the zone lock |
| Memory zones | Hardware addressing limits | Separate buddy lists for DMA, DMA32 and Normal |
| Watermarks | Sudden exhaustion | Triggers reclaim before free pages run out |
Administrators can inspect fragmentation in /proc/buddyinfo, which lists the number of free blocks of each order per zone. Many order-0 blocks and zeros at high orders indicate that large contiguous allocations are likely to fail or to trigger compaction.
Limitations and common mistakes
- Requesting odd sizes directly: a 9 KiB buffer from the page allocator occupies 16 KiB; slab caches or
kmallocfit such sizes better. - Assuming high-order success: order-4 and larger allocations can fail on a long-running system even with gigabytes free.
- Ignoring zones: a request restricted to the DMA zone can fail while the Normal zone has ample memory.
- Confusing free memory with usable memory: the buddy free lists show how memory is split, not only how much is free.
Explained without jargon
Imagine a chocolate bar that can only be broken in half. If you need a small piece, you break the bar in half, then one half in half again. You keep the spare halves for later.
When you return a piece and its matching half is also back, you join them into the bigger piece again. This is how the buddy system hands out memory. It is fast and keeps memory tidy. The cost is that you sometimes get a slightly bigger piece than you asked for.
A typical use case
A database server with 256 GiB of RAM has run for several months. The database is configured to use 2 MiB transparent huge pages, but monitoring shows that new huge page allocations keep falling back to 4 KiB pages, and query latency has risen by about 15 percent.
An administrator reads /proc/buddyinfo and sees tens of thousands of free order-0 blocks but almost no free order-9 blocks in the Normal zone. The page cache and kernel objects have fragmented physical memory. The administrator triggers memory compaction and enables proactive compaction in the kernel settings. Within minutes, free order-9 blocks rise to several thousand, huge page allocation succeeds again and query latency returns to its baseline.