The Wayback Machine - https://web.archive.org/web/20240930152608/https://www.geeksforgeeks.org/buddy-system-memory-allocation-technique/
Open In App

Buddy System – Memory Allocation Technique

Last Updated : 29 Feb, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

What is the Buddy System?

Buddy System is a memory allocation technique used in computer OS to allocate and manage memory efficiently. This technique by 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.

The Buddy System is a reminiscence control method utilized in working systems to allocate memory dynamically. It is by and large applied in systems wherein reminiscence is allocated and deallocated frequently, inclusive in multitasking environments or structures with varying memory demands. In the Buddy System, the memory is split into fixed-length blocks, regularly in powers of (e.g., 1KB, 2KB, 4KB, and so on.). When a request for memory allocation is made, the device seems for the correct-sized block. If an appropriate block is determined, it’s far allocated to the inquiring manner. However, if the asked size doesn’t shape any existing block precisely, the device allocates a bigger block after which splits it into smaller blocks till an accurately sized one is received.

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.

Types of Buddy System

The Buddy System in memory control generally refers to a selected method used to allocate and deallocate memory blocks. However, within this framework, versions or adaptations may additionally exist relying on specific necessities or optimizations wished for distinctive structures. Here are a few types of Buddy Systems:

  • Fibonacci Buddy System: A Fibonacci buddy system has block sizes of 16, 32, 48, 80, 128, and 208 bytes, with each block size equal to the total of the two blocks previous it. When a block is divided from one free list, its two portions are added to the two free lists that came before it. It can be minimized by bringing the allowable block sizes close together. Now, let’s look at an example to better grasp the Fibonacci buddy system. Assume the memory size is 377 kb. Then, 377 kb will be partitioned into 144 kb and 233 kb. Following that, 144 will be divided into (55 + 89), whereas 233 will be divided into (89 + 144). This separation will continue based on memory requirements.
  • Binary Buddy System: The buddy system keeps track of the free blocks of each size (known as a free list) so that you can easily discover a block of the necessary size if one is available. If no blocks of the requested size are available, Allocate examines the first non-empty list for blocks of at least the requested size. In both cases, a block is deleted from the free list. For ex: The 512 KB memory size is initially partitioned into two active partitions of 256 KB each, with additional subdivisions based on a capacity of 2 to handle memory requests.
  • Weighted Buddy System: In a weighted peer system, each memory block is associated with a weight, which represents its size relative to other blocks. When a memory allocation request occurs, the system searches for the appropriate block considering the size of the requested memory and the weight of the available blocks.
  • Tertiary Buddy System : In a traditional buddy system, memory is divided into blocks of fixed size, usually a power of 2, and allocated to these blocks but the tertiary buddy system introduces a third memory structure, which allows flexibility large in memory allocation.

Features of Buddy System

  • Scalability: The Buddy System can scale nicely with growing memory demands. It can manage huge quantities of memory efficiently by means of dividing it into smaller blocks and dynamically adjusting block sizes based totally on allocation and deallocation styles.
  • Efficient Splitting and Merging: The Buddy System ensures that memory blocks are split and merged effectively. When a block is allotted, it’s divided into smaller pal blocks. Conversely, when memory is deallocated, the gadget tests if the friend of the deallocated block is also free. If so, they’re merged back into a larger block.
  • Reduced Fragmentation: By correctly splitting and merging reminiscence blocks, the Buddy System enables reduce memory fragmentation. Fragmentation occurs whilst memory is allotted and deallocated in a manner that leaves small, unusable gaps between allocated blocks. The Buddy System minimizes this by way of merging adjacent loose blocks into large ones whenever feasible.
  • Allocation Efficiency: The Buddy System presents quite speedy allocation and deallocation of memory blocks. Since memory blocks are pre-allotted and managed in a based manner, the overhead of finding and allocating reminiscence is decreased compared to extra complicated reminiscence control strategies.
  • Power of Two Block Sizes: The Buddy System commonly divides memory into blocks of sizes which can be powers of (e.g., 1KB, 2KB, 4KB, etc.). This simplifies the splitting and merging strategies, as blocks may be easily divided or blended.

Advantages of Buddy System

  • 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
  • It provides optimal memory performance while allocating blocks of memory of appropriate size and prevents unnecessary memory waste, unlike other allocation techniques which allocate larger memory blocks than necessary
  • It provides a flexible and efficient way to manage memory allocation in systems that require dynamic memory allocation, such as embedded systems and operating systems
  • It can handle a large number of small memory allocations efficiently thanks to its block partitioning mechanism, which helps prevent fragmentation and keeps system performance up
  • It can prevent memory leaks by ensuring that all shared memory is cleared when not in use, which can improve system stability and reliability
  • It provides a high level of flexibility in memory allocation and monitoring processes, which is particularly useful in systems that require frequent memory allocation and sharing, such as real-time systems

Disadvantages of Buddy System

  • It requires all allocation unit to be powers of 2
  • It leads to internal fragmentation
  • 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 of Buddy system

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.

Frequently Asked Question on Buddy System – FAQs

How does the Buddy System work?

In the Buddy System, memory is split into constant-size blocks. When a reminiscence allocation request is made, the system searches for the ideal-sized block. If an actual match is discovered, the block is allotted. Otherwise, a bigger block is cut up into smaller “friend” blocks till an accurately sized one is acquired. When reminiscence is deallocated, the system merges adjacent loose blocks back into larger blocks to reduce fragmentation.

What are the advantages of the Buddy System?

Some advantages of the Buddy System encompass decreased fragmentation, efficient reminiscence allocation and deallocation, scalability with increasing reminiscence demands, and comparatively easy implementation as compared to more complicated reminiscence management techniques.

Where is the Buddy System used?

The Buddy System is normally used in running structures, mainly in environments wherein memory is allotted and deallocated frequently, including multitasking structures or structures with varying memory needs. It is frequently hired in reminiscence allocators, which includes the pal allocator in the Linux kernel.



Previous Article
Next Article

Similar Reads

Buddy Memory Allocation Program | Set 1 (Allocation)
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
13 min read
Buddy Memory Allocation Program | Set 2 (Deallocation)
Prerequisite - Buddy Allocation | Set 1 Question: Write a program to implement the buddy system of memory allocation and deallocation in Operating Systems.Explanation - As we already know from Set 1, the allocation is done via the usage of free lists. Now, for deallocation, we will maintain an extra data structure-a Map (unordered_set in C++, HashM
15+ min read
Allocating kernel memory (buddy system and slab system)
Prerequisite - Buddy System Introduction: Allocating kernel memory is a critical task in operating system design, as the kernel needs to manage memory efficiently and effectively to ensure optimal system performance. Two common methods for allocating kernel memory are the buddy system and the slab system. The buddy system is a memory allocation alg
9 min read
Difference between Static allocation and Stack allocation
Static Allocation: Static allocation is a procedure which is used for allocation of all the data objects at compile time. Static allocation is possible only when the compiler knows the size of data object at compile time. In this type of allocation, formation of data objects is not possible under any circumstances at run time. In static allocation,
2 min read
Difference Between Static Allocation and Heap Allocation
In programming, there are two approaches to memory management and these are static allocation and heap allocation. In static allocation, memory is determined at the compile time, where the size and position of each data structure for example arrays, variable are constant throughout the program. While this approach may help to give faster access, it
5 min read
Memory Allocation Techniques | Mapping Virtual Addresses to Physical Addresses
Prerequisite : Requirements of Memory Management System, Logical and Physical Address Memory Allocation Techniques:To store the data and to manage the processes, we need a large-sized memory and, at the same time, we need to access the data as fast as possible. But if we increase the size of memory, the access time will also increase and, as we kno
5 min read
MCQ on Memory allocation and compilation process
1. What will be the output of the following code? C/C++ Code #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; int main() { union test { int i; float f; char c; }; union test* t; t = (union test*)malloc(sizeof(union test)); t-&gt;f = 20.60f; printf(&quot;%f&quot;, t-&gt;f); return 0; } (a) Garbage value (b) 20.60000
3 min read
Partition Allocation Methods in Memory Management
In the operating system, the following are four common memory management techniques. Single contiguous allocation: Simplest allocation method used by MS-DOS. All memory (except some reserved for OS) is available to a process. Partitioned allocation: Memory is divided into different blocks or partitions. Each process is allocated according to the re
4 min read
Difference Between Contiguous and Non-Contiguous Memory Allocation
Memory management is one of the fundamentals of operating systems that has implications for its performances and use of resources. These are discussed below: Contiguous and non-contiguous allocation of memory Allocation of memory may take either of the following forms: This article enables you to know the difference between the two, how memory is a
5 min read
Allocation of frames in Operating System
An important aspect of operating systems, virtual memory is implemented using demand paging. Demand paging necessitates the development of a page-replacement algorithm and a frame allocation algorithm. Frame allocation algorithms are used if you have multiple processes; it helps decide how many frames to allocate to each process. There are various
3 min read
Best-Fit Allocation in Operating System
INTRODUCTION: Best-Fit Allocation is a memory allocation technique used in operating systems to allocate memory to a process. In Best-Fit, the operating system searches through the list of free blocks of memory to find the block that is closest in size to the memory request from the process. Once a suitable block is found, the operating system spli
4 min read
Resource Allocation Graph (RAG) in Operating System
As Banker's algorithm using some kind of table like allocation, request, available all that thing to understand what is the state of the system. Similarly, if you want to understand the state of the system instead of using those table, actually tables are very easy to represent and understand it, but then still you could even represent the same inf
5 min read
Non-Contiguous Allocation in Operating System
Non-contiguous allocation, also known as dynamic or linked allocation, is a memory allocation technique used in operating systems to allocate memory to processes that do not require a contiguous block of memory. In this technique, each process is allocated a series of non-contiguous blocks of memory that can be located anywhere in the physical memo
6 min read
Difference between Virtual memory and Cache memory
Cache Memory: Cache memory increases the accessing speed of CPU. It is not a technique but a memory unit i.e a storage device. In cache memory, recently used data is copied. Whenever the program is ready to be executed, it is fetched from main memory and then copied to the cache memory. But, if its copy is already present in the cache memory then t
2 min read
Difference between Volatile Memory and Non-Volatile Memory
Memory is one of the most fundamental components in computing systems. Broadly, computer memory can be divided into two types namely volatile memory and non volatile memory. Even though both are crucial in the total functioning of the electronics, they are not interchangeable, each playing specific roles. Volatile memory deploys power in order to s
6 min read
Random Access Memory (RAM) and Read Only Memory (ROM)
Memory is a fundamental component of computing systems, essential for performing various tasks efficiently. It plays a crucial role in how computers operate, influencing speed, performance, and data management. In the realm of computer memory, two primary types stand out: Random Access Memory (RAM) and Read-Only Memory (ROM). Understanding the diff
9 min read
Resource Allocation Techniques for Processes
The Operating System allocates resources when a program need them. When the program terminates, the resources are de-allocated, and allocated to other programs that need them. Now the question is, what strategy does the operating system use to allocate these resources to user programs? There are two Resource allocation techniques: 1. Resource parti
2 min read
Resource Allocation
To assign the available resources in an economic way is known as resource allocation. The planning of the activities and the resource required by these activities while taking into consideration both resources availability and project time is termed as resource allocation in project management. There are 2 parts of resource allocation: Strategic Pl
2 min read
First-Fit Allocation in Operating Systems
INTRODUCTION: First-Fit Allocation is a memory allocation technique used in operating systems to allocate memory to a process. In First-Fit, the operating system searches through the list of free blocks of memory, starting from the beginning of the list, until it finds a block that is large enough to accommodate the memory request from the process.
3 min read
Worst-Fit Allocation in Operating Systems
For both fixed and dynamic memory allocation schemes, the operating system must keep a list of each memory location noting which are free and which are busy. Then as new jobs come into the system, the free partitions must be allocated. These partitions may be allocated in 4 ways: 1. First-Fit Memory Allocation 2. Best-Fit Memory Allocation 3. Worst
2 min read
Register Allocation Algorithms in Compiler Design
Register allocation is an important method in the final phase of the compiler . Registers are faster to access than cache memory . Registers are available in small size up to few hundred Kb .Thus it is necessary to use minimum number of registers for variable allocation . There are three popular Register allocation algorithms . Naive Register Alloc
5 min read
Channel Allocation Problem in Computer Network
Channel allocation is a process in which a single channel is divided and allotted to multiple users in order to carry user specific tasks. There are user's quantity may vary every time the process takes place. If there are N number of users and channel is divided into N equal-sized sub channels, Each user is assigned one portion. If the number of u
3 min read
IPC technique PIPES
Pipes are a type of IPC (Inter-Process Communication) technique that allows two or more processes to communicate with each other by creating a unidirectional or bidirectional channel between them. A pipe is a virtual communication channel that allows data to be transferred between processes, either one-way or two-way. Pipes can be implemented using
4 min read
Common Subexpression Elimination - Code optimization Technique in Compiler Design
Code Optimization Technique is an approach to enhance the performance of the code by either eliminating or rearranging the code lines. Code Optimization techniques are as follows: Compile-time evaluation Common Sub-expression elimination Dead code elimination Code movement Strength reductionCommon Sub-expression Elimination:The expression or sub-ex
2 min read
Requirements of Memory Management System
Memory management keeps track of the status of each memory location, whether it is allocated or free. It allocates the memory dynamically to the programs at their request and frees it for reuse when it is no longer needed. Memory management meant to satisfy some requirements that we should keep in mind. These Requirements of memory management are:
5 min read
Best Ways for Operating System Memory Management
Prerequisite - Partition Allocation Methods Contiguous Memory Allocation :Probably the least difficult technique for the memory portion is to isolate memory into a few fixed partitions. Each partition contains precisely one process. The level of multi-programming relies upon the number of partitions. In the different partition technique, when a par
7 min read
Memory Management in Operating System
The term memory can be defined as a collection of data in a specific format. It is used to store instructions and process data. The memory comprises a large array or group of words or bytes, each with its own location. The primary purpose of a computer system is to execute programs. These programs, along with the information they access, should be
14 min read
Levels of Memory in Operating System
Memory hierarchy of a computer system it handles differences in speed. "Hierarchy" is a great way to say "order of thinks" like top to bottom, fast to slow, most important to least important. If you look at the memory hierarchy inside the computer, according to the fastest to the slowest: 1. CPU Registers 2. Caches memory 3. Main or Primary Memory
3 min read
Virtual Memory in Operating System
Virtual Memory is a storage allocation scheme in which secondary memory can be addressed as though it were part of the main memory. The addresses a program may use to reference memory are distinguished from the addresses the memory system uses to identify physical storage sites and program-generated addresses are translated automatically to the cor
15 min read
System Protection in Operating System
Introduction: System protection in an operating system refers to the mechanisms implemented by the operating system to ensure the security and integrity of the system. System protection involves various techniques to prevent unauthorized access, misuse, or modification of the operating system and its resources. There are several ways in which an op
3 min read
Article Tags :