Buddy memory allocation
The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible.[src] This system makes use of splitting memory into halves to try to give a best fit.[src] According to Donald Knuth, the buddy system was invented in 1963 by Harry Markowitz, and was first described by Kenneth C.[src] Knowlton (published 1965).[src] The Buddy memory allocation is relatively easy to implement.[src] It supports limited but efficient splitting and coalescing of memory blocks.[src]
The buddy memory allocation technique is a memory allocation algorithm that divides memory into partitions to try to satisfy a memory request as suitably as possible.[src] This system makes use of splitting memory into halves to try to give a best fit.[src] According to Donald Knuth, the buddy system was invented in 1963 by Harry Markowitz, and was first described by Kenneth C.[src] Knowlton (published 1965).[src] The Buddy memory allocation is relatively easy to implement.[src] It supports limited but efficient splitting and coalescing of memory blocks.[src] The following is an example of what happens when a program makes requests for memory.[src] Assume that in this system, the smallest possible block is 64 kilobytes in size, and the upper limit for the order is 4, which results in a largest possible allocatable block, 2 4 times 64 K = 1024 K in size.[src] The following shows a possible state of the system after various memory requests.[src] This allocation could have occurred in the following manner The initial situation.[src] Program A requests memory 34 K, order 0.[src] No order 0 blocks are available, so an order 4 block is split, creating two order 3 blocks.[src] Still no order 0 blocks available, so the first order 3 block is split, creating two order 2 blocks.[src] Still no order 0 blocks available, so the first order 2 block is split, creating two order 1 blocks.[src] Still no order 0 blocks available, so the first order 1 block is split, creating two order 0 blocks.[src] Now an order 0 block is available, so it is allocated to A.[src] Program B requests memory 66 K, order 1.[src] An order 1 block is available, so it is allocated to B.[src] Program C requests memory 35 K, order 0.[src] An order 0 block is available, so it is allocated to C.[src] Program D requests memory 68 K, order 1.[src] No order 1 blocks are available, so an order 2 block is split, creating two order 1 blocks.[src] Now an order 1 block is available, so it is allocated to D.[src] Program B releases its memory, freeing one order 1 block.[src] Program D releases its memory.[src] One order 1 block is freed.[src] Since the buddy block of the newly freed block is also free, the two are merged into one order 2 block.[src] Program A releases its memory, freeing one order 0 block.[src] Program C releases its memory.[src] One order 0 block is freed.[src] Since the buddy block of the newly freed block is also free, the two are merged into one order 1 block.[src] Since the buddy block of the newly formed order 1 block is also free, the two are merged into one order 2 block.[src] Since the buddy block of the newly formed order 2 block is also free, the two are merged into one order 3 block.[src] Since the buddy block of the newly formed order 3 block is also free, the two are merged into one order 4 block.[src] As you can see, what happens when a memory request is made is as follows: If memory is to be allocated Search for an exact match: Look for a free memory slot of exactly that target 2k size.[src] If found: Allocate it to the program immediately.[src] If not found: The system looks up the chain for the next available larger free power-of-two block (e.g.,2k+1,2k+2, etc.).[src] Split the larger block: Once a larger free block is found, the system executes a loop Split the larger block in half into two equal "buddies." If the resulting halves are still larger than your target 2k size, keep one half and split the other half again.[src] Repeat this splitting process until you have successfully created a block that matches your target 2k size.[src] Allocate: Allocate one of the newly created target-sized blocks to the program and leave its "buddy" in the free list.[src] If memory is to be freed A buddy should only be tied with the true buddy where it got divided with.[src] It cannot be tied with a neighboring block (Next to a buddy) not associated with the buddy prior to splitting.[src] Wait till the program is done with the block currently assigned with the program.[src] If buddy A is now free check buddy B if free then combine the two into one unified single block.[src] Check if the 64kb combined block from Buddy A & Buddy B is free, repeat the process upward.[src] In comparison to other simpler techniques such as dynamic allocation, the buddy memory system has little external fragmentation, and allows for compaction of memory with little overhead.[src] The buddy method of freeing memory is fast, with the maximal number of compactions required equal to O(highest order) = O(log 2 (total memory size)).[src] Typically the buddy memory allocation system is implemented with the use of a binary tree to represent used or unused split memory blocks.[src] The address of a block's "buddy" is equal to the bitwise exclusive OR (XOR) of the block's address and the block's size.[src] However, there still exists the problem of internal fragmentation – memory wasted because the memory requested is a little larger than a small block, but a lot smaller than a large block.[src] Because of the way the buddy memory allocation technique works, a program that requests 66 K of memory would be allocated 128 K, which results in a waste of 62 K of memory.[src] This problem can be solved by slab allocation, which may be layered on top of the more coarse buddy allocator to provide more fine-grained allocation.[src] One version of the buddy allocation algorithm was described in detail by Donald Knuth in volume 1 of The Art of Computer Programming.[src] The Linux kernel also uses the buddy system, with further modifications to minimise external fragmentation, along with various other allocators to manage the memory within blocks.[src] jemalloc is a modern memory allocator that employs, among others, the buddy technique.[src] ==See also== Memory pool Stack-based memory allocation Fibonacci number[src]
62 sentences · 62 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Buddy memory allocation." encyclopegia.org, 2026, https://encyclopegia.org/wiki/buddy-memory-allocation. Accessed 27 November 2026.
Chicago
"Buddy memory allocation." encyclopegia.org. Accessed November 27, 2026. https://encyclopegia.org/wiki/buddy-memory-allocation.
APA
encyclopegia.org. (2026). Buddy memory allocation. Retrieved November 27, 2026, from https://encyclopegia.org/wiki/buddy-memory-allocation
Accessed November 27, 2026 · CC BY-SA 4.0 · citation page · JSON
Claims
| Predicate | Value | Source |
|---|---|---|
| publication date | 1965 | Wikipedia |