Algorithm of Buddy System Memory Allocation Technique
Below are the steps involved in the Buddy System Memory Allocation Technique:
- The first step includes the division of memory into fixed-sized blocks that have a power of 2 in size (such as 2, 4, 8, 16, 32, 64, 128, etc. ).
- Each block is labeled with its size and unique identification.
- Initially, all the memory blocks are free and are linked together in a binary tree structure, with each node representing a block and the tree’s leaves representing the smallest available blocks.
- When a process requests memory, the system finds the smallest available block that can accommodate the requested size. If the block is larger than the requested size, the system splits the block into two equal-sized “buddy” blocks.
- The system marks one of the buddy blocks as allocated and adds it to the process’s memory allocation table, while the other buddy block is returned to the free memory pool and linked back into the binary tree structure.
- When a process releases memory, the system marks the corresponding block as free and looks for its buddy block. If the buddy block is also free, the system merges the two blocks into a larger block and links it back into the binary tree structure.
The Buddy System technique has several advantages, including efficient use of memory, reduced fragmentation, and fast allocation and deallocation of memory blocks. However, it also has some drawbacks, such as internal fragmentation, where a block may be larger than what the process requires, leading to a waste of memory. Overall, the Buddy System is a useful memory allocation technique in operating systems, particularly for embedded systems with limited memory.
- If 2U-1<S<=2U: Allocate the whole block
- Else: Recursively divide the block equally and test the condition at each time, when it satisfies, allocate the block and get out of the loop.
The system also keeps a record of all the unallocated blocks and can merge these different-sized blocks to make one big chunk.
Buddy System – Memory Allocation Technique
Static partition techniques are limited by having a fixed number of active processes, and space use may not be optimal. The buddy system is a memory allocation and management technique that uses power-of-two increments. Assume the memory size is 2U and a size of S is required. In this article, we are going to discuss the Buddy System in detail along with examples, advantages, disadvantages, etc.