Allocating Kernel Memory (Buddy System and Slab System)


In operating system design, the kernel memory allocation is a critical aspect which involves the allocation of memory for kernel level operations and data structures. When a process is executing in user mode and it requests the additional memory, then the kernel maintains the allocation of pages from the list of free page frames.

Hence, the kernel memory allocation can be defined as under −

"The process by which the kernel of the operating system allocates memory for its internal operations and data structures is called kernel memory allocation."

In other words, the process of allocating memory in the kernel space of an operating system is known as kernel memory allocation. Where, the kernel space is a part of the memory which is reserved for the operating system. The kernel space is primarily used to rum the system level jobs like scheduling tasks, hardware device management, memory management, etc. Hence, the kernel memory is different from the user level memory. The kernel memory typically remains protected so that the user level processes cannot directly access or modify it.

In operating system, allocating kernel memory is a critical task, therefore, it must be performed correctly and efficiently.

Depending upon the requirements of the system and type of memory allocation, there are two important kernel memory allocation techniques namely buddy system and slab system. In this article, we will discuss these two approaches, i.e. buddy system and slab system, for allocating kernel memory. So let us start with the buddy system.

What is Buddy Memory Allocation System?

The parts of equal size of a block are called buddies. In context of the operating system, the buddy system is an algorithm used for memory allocation. In buddy system, the available memory space is divided into blocks of a fixed and equal size. These blocks are then organized in the form of a binary tree structure. In this binary tree structure, each block has a buddy block whose size is same as that of the adjacent block.

The buddy system is one of the efficient memory allocation technique because it avoids the fragmentation of memory space. Buddy system ensures that all the allocated blocks have the same size so that they can be easily merged together with their buddy blocks. Another major advantage of buddy system is that it allows for quick allocation and deallocation of memory blocks that is an important requirement in real time systems to give enhanced performance.

In buddy system, whenever a request is made for memory allocation, the memory allocator finds a bock of appropriate memory size. If it found a block of a larger size than required, it is repetitively divided into smaller blocks until a block of the desired size is obtained. Once the block of the desired size is found, the allocator marks it as allocated and sends a pointer to the requesting process.

Also, if a memory block is freed from a process, then the allocator searches for its buddy block and checks whether it is also free or not. If the buddy block is also free, then the two blocks are merged together to form a larger block. This larger block is then marked as free and added back to binary tree structure. The process of merging of free blocks is repeated until no more merges are possible.

Advantages of Buddy System

The following are the major advantages of the buddy system −

  • The buddy system involves less external fragmentation as compared to other memory allocation algorithms.

  • The buddy system uses binary tree structure to represent used or unused memory blocks.

  • In buddy system, it is easy to merge adjacent blocks.

  • Buddy system provides quick allocation and deallocation of memory.

  • Buddy system allocates a block of correct memory size.

Disadvantages of Buddy System

Though, the buddy system has several advantage. But there are also some disadvantages of the buddy system which are as follows −

  • Buddy system leads to the internal fragmentation.

  • Buddy system uses binary tree, hence it requires all the allocation units to be powers of 2.

What is Slab Memory Allocation System?

Slab system is another technique used for allocating kernel memory. The major advantages of the slab memory allocation system is that it eliminates the fragmentation due to allocation and deallocation of memory. In other words, the slab allocation system is a type of memory allocation strategy used in the operating system to manage kernel memory.

Note − In the slab system, the two required terms are: slab and cache.

  • Cache − Cache is a very high speed small sized memory space. In slab system, a cache has one or more slabs. For each unique kernel data structure, there is a single cache space present.

  • Slab − Slab is a container used a container which stores data of a kernel object of a specific type. It is made up of physically contiguous pages as shown in the above figure.

In the slab memory allocation system, the memory allocation for kernel data structures is done in fixed size blocks, known as slabs. Each slab contains one or more objects of the same kind. Once the allocation of a slab is done, then its objects can be allocated and deallocated efficiently as needed.

We can also use the slab allocation system to manage the kernel memory which is not directly accessible by the user space programs. This process includes the data structures used by the kernel itself as well as the data structures used by device drivers and other kernel modules.

Whenever a request for allocating memory for a specific data structure is made, the allocator searches for a slab of the appropriate size. If a slab of the desired size is already available, then it is utilized to allocate the necessary memory. If a slab of the desired size is not available, then a new slab is allocated from a pool of memory that is reserved for that specific data structure.

In the slab system, a same slab is used to allocate all the objects of the same size, hence it helps to reduce the fragmentation of kernel memory. It also helps to improve the performance of the system by reducing the overhead associated with the memory allocation and deallocation.

Therefore, in the operating system, the slab system for allocating kernel memory is also an efficient strategy to manage the kernel memory.

Conclusion

In operating systems, allocating kernel memory is a critical part that needs careful consideration to ensure the stable system performance. Based on the requirement and the nature of the data to be stored, there are two most commonly used allocation techniques viz. buddy allocation system and slab allocation system used to allocate kernel memory. In the above sections of this article, we discussed these two approaches in detail.

Updated on: 04-Apr-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements