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.
- Allocates memory for kernel operations and system tasks
- Requires high speed and efficiency
- Aims to reduce memory fragmentation
- Uses techniques like Buddy System and Slab Allocation
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
- Binary buddy system
- Fibonacci buddy system
- Weighted buddy system
- Tertiary buddy system
Binary buddy system
- Memory is split into blocks of size power of two.
- On request, the nearest larger block is chosen and split repeatedly into equal halves (buddies) until the required size is reached.
- Freed buddies can merge back to form larger blocks (coalescing).
**Example:
If total memory = 256KB and request = 25KB nearest power of two is 32KB.
- 256KB split into 128KB + 128KB
- 128KB split into 64KB + 64KB
- 64KB split into 32KB + 32KB
- One 32KB block is allocated (25KB fits inside).
**Fibonacci buddy system
- A variation of the buddy system where memory blocks are divided into sizes based on **Fibonacci numbers instead of powers of two.
- Block sizes follow the relation:
Zi = Z(i-1)+Z(i-2)
- Example sequence: 1, 2, 3, 5, 8, 13, 21, 34 …
- On a memory request, the system finds the **smallest Fibonacci block that can satisfy it.
- Like binary buddies, free blocks can be merged (coalesced) into larger Fibonacci-sized blocks.
**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:
- **Slab - A slab is made up of one or more physically contiguous pages. The slab is the actual container of data associated with objects of the specific kind of the containing cache.
- **Cache - Cache is a software structure that stores pre-allocated kernel objects of the same type for reuse.

**Example -
- A separate cache for a data structure representing processes descriptors
- Separate cache for file objects
- Separate cache for semaphores etc.
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:
- **Full - All objects in the slab are marked as used
- **Empty - All objects in the slab are marked as free
- **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.
- Eliminates fragmentation by using object-specific caches
- Provides faster memory allocation and deallocation
- Reuses objects by marking them free and returning them to cache
- Efficient for frequent allocation and release of kernel objects
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.
- Assigns weights to memory blocks based on size
- Allocates memory by selecting suitable weighted blocks
- Improves flexibility compared to basic buddy system
Tertiary Buddy System is an extension of the traditional buddy system that introduces an additional memory structure to increase flexibility in allocation.
- Extends basic buddy system with extra memory structure
- Provides more flexible memory allocation
- Reduces limitations of fixed-size block allocation