Buddy System – Memory allocation technique
Prerequisite – Partition Allocation Methods Static partition schemes suffer from the limitation of having the fixed number of active processes and the usage of space may also not be optimal. The buddy system is a memory allocation and management algorithm that manages memory in power of two increments. Assume the memory size is 2U, suppose a size of S is required.
The Buddy System is a memory allocation technique used in computer operating systems to efficiently allocate and manage memory. The technique is based on dividing the memory into fixed-size blocks, and whenever a process requests memory, the system finds the smallest available block that can accommodate the requested memory size.
Here are the basic steps in the Buddy System memory allocation technique:
- The memory is divided into fixed-size blocks that are a power of 2 in size (such as 2, 4, 8, 16, 32, etc. bytes).
- Each block is labeled with its size and a unique identifier, such as a binary number.
- 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 wastage 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 the loop.
System also keep the record of all the unallocated blocks each and can merge these different size blocks to make one big chunk. Advantage –
- Easy to implement a buddy system
- Allocates block of correct size
- It is easy to merge adjacent holes
- Fast to allocate memory and de-allocating memory
- Provides good memory utilization as it allocates memory blocks of the correct size and prevents unnecessary memory wastage, unlike other allocation techniques that allocate memory blocks larger than required.
- Offers a relatively simple and efficient way to manage memory allocation in systems that require dynamic memory allocation, such as embedded systems and operating systems.
- Can handle a large number of small memory allocations efficiently due to its block division mechanism, which helps prevent fragmentation and maintains system performance.
- Can prevent memory leaks by ensuring that all allocated memory is deallocated when it’s no longer in use, which can improve system stability and reliability.
Provides a high level of flexibility in managing memory allocation and usage, which is particularly useful in systems that require frequent memory allocation and deallocation, such as real-time systems.
- It requires all allocation unit to be powers of two
- It leads to internal fragmentation
- It requires a significant amount of overhead to manage the free memory blocks and keep track of the allocated blocks. This overhead can become a performance bottleneck in systems with limited resources.
- It is designed for fixed-sized memory allocations, and it is not suitable for variable-sized allocations. This can limit its applicability in some systems, such as databases and file systems.
- It is a general-purpose memory allocation technique and may not be optimal for all applications. Some applications may require more specialized memory allocation techniques to achieve the best performance.
Example – Consider a system having buddy system with physical address space 128 KB.Calculate the size of partition for 18 KB process. Solution – So, size of partition for 18 KB process = 32 KB. It divides by 2, till possible to get minimum block to fit 18 KB.
Level Up Your GATE Prep!
Embark on a transformative journey towards GATE success by choosing Data Science & AI
as your second paper choice with our specialized course. If you find yourself lost in the vast landscape of the GATE syllabus, our program is the compass you need.