Prerequisite – Buddy System
Question: Write a program to implement the buddy system of memory allocation in Operating Systems.
Explanation –
The buddy system is implemented as follows- A list of free nodes, of all the different possible powers of 2, is maintained at all times (So if total memory size is 1 MB, we’d have 20 free lists to track-one for blocks of size 1 byte, 1 for 2 bytes, next for 4 bytes and so on).
When a request for allocation comes, we look for the smallest block bigger than it. If such a block is found on the free list, the allocation is done (say, the request is of 27 KB and the free list tracking 32 KB blocks has at least one element in it), else we traverse the free list upwards till we find a big enough block. Then we keep splitting it in two blocks-one for adding to the next free list (of smaller size), one to traverse down the tree till we reach the target and return the requested memory block to the user. If no such allocation is possible, we simply return null.
Example:
Let us see how the algorithm proceeds by tracking a memory block of size 128 KB. Initially, the free list is: {}, {}, {}, {}, {}, {}, {}, { (0, 127) }
- Request: 32 bytes
No such block found, so we traverse up and split the 0-127 block into 0-63, 64-127; we add 64-127 to list tracking 64 byte blocks and pass 0-63 downwards; again it is split into 0-31 and 32-63; since we have found the required block size, we add 32-63 to list tracking 32 byte blocks and return 0-31 to user.
List is: {}, {}, {}, {}, {}, { (32, 63) }, { (64, 127) }, {} - Request: 7 bytes
No such block found-split block 32-63 into two blocks, namely 32-47 and 48-63; then split 32-47 into 32-39 and 40-47; finally, return 32-39 to user (internal fragmentation of 1 byte occurs)
List is: {}, {}, {}, { (40, 47) }, { (48, 63) }, {}, { (64, 127) }, {} - Request: 64 bytes
Straight up memory segment 64-127 will be allocated as it already exists.
List is: {}, {}, {}, { (40, 47) }, { (48, 63) }, {}, {}, {} - Request: 56 bytes
Result: Not allocated
The result will be as follows:

Figure – Buddy Allocation-128 shows the starting address of next possible block (if main memory size ever increases)
Implementation –
Java
import java.io.*; import java.util.*; class Buddy { // Inner class to store lower // and upper bounds of the allocated memory class Pair { int lb, ub; Pair(int a, int b) { lb = a; ub = b; } } // Size of main memory int size; // Array to track all // the free nodes of various sizes ArrayList<Pair> arr[]; // Else compiler will give warning // about generic array creation @SuppressWarnings("unchecked") Buddy(int s) { size = s; // Gives us all possible powers of 2 int x = (int)Math.ceil(Math.log(s) / Math.log(2)); // One extra element is added // to simplify arithmetic calculations arr = new ArrayList[x + 1]; for (int i = 0; i <= x; i++) arr[i] = new ArrayList<>(); // Initially, only the largest block is free // and hence is on the free list arr[x].add(new Pair(0, size - 1)); } void allocate(int s) { // Calculate which free list to search to get the // smallest block large enough to fit the request int x = (int)Math.ceil(Math.log(s) / Math.log(2)); int i; Pair temp = null; // We already have such a block if (arr[x].size() > 0) { // Remove from free list // as it will be allocated now temp = (Pair)arr[x].remove(0); System.out.println("Memory from " + temp.lb + " to " + temp.ub + " allocated"); return; } // If not, search for a larger block for (i = x + 1; i < arr.length; i++) { if (arr[i].size() == 0) continue; // Found a larger block, so break break; } // This would be true if no such block was found // and array was exhausted if (i == arr.length) { System.out.println("Sorry, failed to allocate memory"); return; } // Remove the first block temp = (Pair)arr[i].remove(0); i--; // Traverse down the list for (; i >= x; i--) { // Divide the block in two halves // lower index to half-1 Pair newPair = new Pair(temp.lb, temp.lb + (temp.ub - temp.lb) / 2); // half to upper index Pair newPair2 = new Pair(temp.lb + (temp.ub - temp.lb + 1) / 2, temp.ub); // Add them to next list // which is tracking blocks of smaller size arr[i].add(newPair); arr[i].add(newPair2); // Remove a block to continue the downward pass temp = (Pair)arr[i].remove(0); } // Finally inform the user // of the allocated location in memory System.out.println("Memory from " + temp.lb + " to " + temp.ub + " allocated"); } public static void main(String args[]) throws IOException { int initialMemory = 0, val = 0; // Uncomment the below section for interactive I/O /*Scanner sc=new Scanner(System.in); initialMemory = sc.nextInt(); Buddy obj = new Buddy(initialMemory); while(true) { val = sc.nextInt();// Accept the request if(val <= 0) break; obj.allocate(val);// Proceed to allocate }*/ initialMemory = 128; // Initialize the object with main memory size Buddy obj = new Buddy(initialMemory); obj.allocate(32); obj.allocate(7); obj.allocate(64); obj.allocate(56); } } |
C#
using System; using System.Collections.Generic; public class Buddy { // Inner class to store lower // and upper bounds of the // allocated memory class Pair { public int lb, ub; public Pair(int a, int b) { lb = a; ub = b; } } // Size of main memory int size; // Array to track all // the free nodes of various sizes List<Pair> []arr; // Else compiler will give warning // about generic array creation Buddy(int s) { size = s; // Gives us all possible powers of 2 int x = (int)Math.Ceiling(Math.Log(s) / Math.Log(2)); // One extra element is added // to simplify arithmetic calculations arr = new List<Pair>[x + 1]; for (int i = 0; i <= x; i++) arr[i] = new List<Pair>(); // Initially, only the largest block is free // and hence is on the free list arr[x].Add(new Pair(0, size - 1)); } void allocate(int s) { // Calculate which free list to search // to get the smallest block // large enough to fit the request int x = (int)Math.Ceiling(Math.Log(s) / Math.Log(2)); int i; Pair temp = null; // We already have such a block if (arr[x].Count > 0) { // Remove from free list // as it will be allocated now temp = (Pair)arr[x][0]; arr[x].RemoveAt(0); Console.WriteLine("Memory from " + temp.lb + " to " + temp.ub + " allocated"); return; } // If not, search for a larger block for (i = x + 1; i < arr.Length; i++) { if (arr[i].Count == 0) continue; // Found a larger block, so break break; } // This would be true if no such block // was found and array was exhausted if (i == arr.Length) { Console.WriteLine("Sorry, failed to" + " allocate memory"); return; } // Remove the first block temp = (Pair)arr[i][0]; arr[i].RemoveAt(0); i--; // Traverse down the list for (; i >= x; i--) { // Divide the block in two halves // lower index to half-1 Pair newPair = new Pair(temp.lb, temp.lb + (temp.ub - temp.lb) / 2); // half to upper index Pair newPair2 = new Pair(temp.lb + (temp.ub - temp.lb + 1) / 2, temp.ub); // Add them to next list which is // tracking blocks of smaller size arr[i].Add(newPair); arr[i].Add(newPair2); // Remove a block to continue // the downward pass temp = (Pair)arr[i][0]; arr[i].RemoveAt(0); } // Finally inform the user // of the allocated location in memory Console.WriteLine("Memory from " + temp.lb + " to " + temp.ub + " allocated"); } // Driver Code public static void Main(String []args) { int initialMemory = 0; initialMemory = 128; // Initialize the object with main memory size Buddy obj = new Buddy(initialMemory); obj.allocate(32); obj.allocate(7); obj.allocate(64); obj.allocate(56); } } // This code is contributed by 29AjayKumar |
Memory from 0 to 31 allocated Memory from 32 to 39 allocated Memory from 64 to 127 allocated Sorry, failed to allocate memory
Time Complexity –
If the main memory size is n, we have log(n) number of different powers of 2 and hence log(n) elements in the array (named arr in the code) tracking free lists. To allocate a block, we only need to traverse the array once upwards and once downwards, hence time complexity is O(2log(n)) or simply O(logn)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Buddy Memory Allocation Program | Set 2 (Deallocation)
- Buddy System - Memory allocation technique
- Difference between Contiguous and Noncontiguous Memory Allocation
- Partition Allocation Methods in Memory Management
- Implementation of all Partition Allocation Methods in Memory Management
- Resource Allocation
- Best-Fit Allocation in Operating System
- File Allocation Methods
- First-Fit Allocation in Operating Systems
- Worst-Fit Allocation in Operating Systems
- Non-Contiguous Allocation in Operating System
- Resource Allocation Techniques for Processes
- Resource Allocation Graph (RAG) in Operating System
- Implementation of file allocation methods using vectors
- Allocating kernel memory (buddy system and slab system)
- Program for Next Fit algorithm in Memory Management
- Program for Best Fit algorithm in Memory Management using Linked List
- Difference between Virtual memory and Cache memory
- Random Access Memory (RAM) and Read Only Memory (ROM)
- Introduction to memory and memory units
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

