Buddy memory allocation

Also known as: buddy allocator

On this page 7

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.

StepActionResulting free blocks
1Take one 64 KiB block (order 4)None at order 4
2Split into two 32 KiB buddiesOne 32 KiB block free
3Split one 32 KiB block into two 16 KiB buddiesOne 32 KiB and one 16 KiB free
4Return one 16 KiB block to the caller32 KiB and 16 KiB remain free
Allocation of 16 KiB from a 64 KiB block

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.

OrderPagesBlock sizeTypical consumer
Order 014 KiBOrdinary page faults, page cache
Order 1-32-88-32 KiBKernel stacks, slab caches
Order 4-816-25664 KiB-1 MiBNetwork buffers, DMA buffers
Order 95122 MiBTransparent huge pages
Order 101,0244 MiBLarge contiguous driver buffers
Typical Linux block orders with 4 KiB pages

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.

AllocatorUnit of allocationMain strengthMain weakness
Buddy allocatorPower-of-two page blocksFast coalescing, contiguityInternal fragmentation
Slab allocatorFixed-size objectsNo waste for common objectsNeeds a page source below it
First-fit free listArbitrary sizesFlexible sizingSlow search, external fragmentation
Bitmap allocatorSingle pagesSimple and compactSlow search for long runs
Buddy allocation versus related allocation strategies

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.

TechniqueProblem addressedHow it helps
Migrate typesUnmovable pages scattered everywhereGroups movable and unmovable pages separately
Memory compactionExternal fragmentationMoves pages to rebuild contiguous blocks
Per-CPU page listsLock contention on order-0 requestsServes single pages without the zone lock
Memory zonesHardware addressing limitsSeparate buddy lists for DMA, DMA32 and Normal
WatermarksSudden exhaustionTriggers reclaim before free pages run out
Linux techniques that support the buddy allocator

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 kmalloc fit 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.