A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Buddy System Memory Allocation

College Depth 96 in the knowledge graph ☐ I know this ☆ Set as goal
1topic build on this
356prerequisites beneath it
See this on the map →
Memory Management FundamentalsContiguous Memory Allocation and Fragmentation→→Slab Allocator for Kernel Memory
allocation memory fragmentation

Core Idea

The buddy system allocates memory in power-of-two sizes, recursively subdividing large blocks and merging free blocks of equal size. It reduces external fragmentation compared to simple contiguous allocation and enables efficient coalescing of freed memory. The algorithm is practical for kernel memory allocation but has internal fragmentation overhead due to power-of-two constraints.

Explainer

From your study of memory management and contiguous allocation, you know the fundamental problem: as processes allocate and free memory over time, free space becomes scattered into small, unusable fragments — external fragmentation. Simple first-fit or best-fit allocators struggle with this because freed blocks of different sizes are difficult to recombine. The buddy system attacks fragmentation with an elegant constraint: every allocated block must be a power of two in size, and every block has exactly one natural partner — its buddy — that it can merge with when both are free.

Here is how allocation works. Suppose you manage 1024 KB of memory and a process requests 100 KB. The smallest power of two that fits is 128 KB. You start with the full 1024 KB block and split it in half: two 512 KB blocks. The first 512 KB block is still too large, so you split it again: two 256 KB blocks. Split once more: two 128 KB blocks. You hand one 128 KB block to the process and keep the other as free. The remaining 512 KB and 256 KB blocks stay on their respective free lists. Each split creates two buddies — blocks of equal size at adjacent, predictable addresses. The address of a block's buddy can be computed with a single XOR operation on the block's address, which makes the data structure extremely fast.

Deallocation is where the buddy system truly shines. When a block is freed, you check whether its buddy is also free. If it is, you coalesce them back into a single block of double the size. Then you check whether that merged block's buddy is free, and merge again if possible, continuing up the hierarchy. This recursive merging happens in O(log n) time and aggressively combats external fragmentation — free blocks naturally reassemble into larger ones without any compaction or copying. Compare this to a general-purpose allocator, where two adjacent free blocks of different sizes require complex bookkeeping to merge.

The tradeoff is internal fragmentation: every allocation is rounded up to the next power of two. That 100 KB request gets a 128 KB block, wasting 28 KB. A 33 KB request wastes nearly half its 64 KB block. In the worst case, you waste almost 50% of each allocation. This is why the buddy system is most commonly used in kernel memory allocation (where allocation sizes tend to cluster around powers of two) rather than as a general-purpose user-space allocator. Linux, for example, uses the buddy system to manage physical page frames, with a separate slab allocator layered on top to efficiently handle the small, fixed-size objects (like process descriptors and inode structures) that would waste too much space under buddy allocation alone.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Boolean Algebra and Fundamental Laws → Logic Gates Fundamentals → Implementing Boolean Functions with Gates → Karnaugh Map Simplification → Combinational Circuit Design → Flip-Flops and Latches → Binary Counters: Design and Analysis → Binary Arithmetic → Fixed-Point Number Representation → Two's Complement Representation → Overflow and Underflow Detection → Binary Adders: Half-Adders and Full-Adders → Full Adder and Carry Propagation → Carry Lookahead Adder Design → Half Adder Circuit Design → Multiplication Circuit Design → Sequential Circuit Design → Registers and Register Files → Instruction Set Architecture (ISA) → Assembly Language Basics → Memory Organization and Addressing → Memory Hierarchy → Memory Management Fundamentals → Contiguous Memory Allocation and Fragmentation → Buddy System Memory Allocation

Longest path: 97 steps · 356 total prerequisite topics

Prerequisites (2)

Leads To (1)