Allocating kernel memory (buddy system and slab system) (original) (raw)

Last Updated : 25 Apr, 2026

Kernel memory allocation is the process of managing memory used by the operating system’s kernel for performing critical tasks. It requires fast and efficient allocation while minimizing fragmentation to ensure smooth system operation.

Types of Allocating kernel memory

1. Buddy system

Buddy Allocation System divides a large memory block into smaller power-of-two blocks called buddies to satisfy a request. If needed, a block keeps splitting until the required size is reached. When freed, buddies can merge back into larger blocks, making memory reuse efficient.

_Example - If the request of 25Kb is made then block of size 32Kb is allocated.

Four Types of Buddy System

  1. Binary buddy system
  2. Fibonacci buddy system
  3. Weighted buddy system
  4. Tertiary buddy system

Binary buddy system

**Example:
If total memory = 256KB and request = 25KB nearest power of two is 32KB.

**Fibonacci buddy system

Zi = Z(i-1)+Z(i-2)

**Coalescing: It is defined as how quickly adjacent buddies can be combined to form larger segments this is known as coalescing.
For example, When two adjacent free buddy blocks are released, they merge to form a larger block (coalescing). when the kernel releases the C1 unit it was allocated, the system can coalesce C1 and C2 into a 64kb segment. This segment B1 can in turn be coalesced with its buddy B2 to form a 128kb segment. Ultimately we can end up with the original 256kb segment.

**Drawback: The main drawback in buddy system is internal fragmentation as larger block of memory is acquired then required. For example if a 36 kb request is made then it can only be satisfied by 64 kb segment and remaining memory is wasted.

2. Slab Allocation -

A second strategy for allocating kernel memory is known as slab allocation. It eliminates fragmentation caused by allocations and deallocations. This method is used to retain allocated memory that contains a data object of a certain type for reuse upon subsequent allocations of objects of the same type. In slab allocation memory chunks suitable to fit data objects of certain type or size are reallocated. Cache does not free the space immediately after use although it keeps track of data which are required frequently so that whenever request is made the data will reach very fast. Two terms required are:

12

**Example -

Each cache is populated with objects that are instantiations of the kernel data structure the cache represents. For example the cache representing semaphores stores instances of semaphore objects, the cache representing process descriptors stores instances of process descriptor objects.

**Implementation -
The slab allocation algorithm uses caches to store kernel objects. When a cache is created a number of objects which are initially marked as free are allocated to the cache. The number of objects in the cache depends on size of the associated slab.
_Example - A 12 kb slab (made up of three contiguous 4 kb pages) could store six 2 kb objects. Initially all objects in the cache are marked as free. When a new object for a kernel data structure is needed, the allocator can assign any free object from the cache to satisfy the request. The object assigned from the cache is marked as used.

In Linux, a slab may in one of three possible states:

  1. **Full - All objects in the slab are marked as used
  2. **Empty - All objects in the slab are marked as free
  3. **Partial - The slab consists of both

Benefits of Allocating kernel memory types:

Slab Allocator improves memory management by reducing fragmentation and providing fast allocation for frequently used kernel objects. It maintains caches of pre-created objects, making allocation and deallocation efficient.

Weighted Buddy System is a variation where each memory block has a weight representing its size, and allocation is done based on matching request size with block weight.

Tertiary Buddy System is an extension of the traditional buddy system that introduces an additional memory structure to increase flexibility in allocation.