Buddy Memory Allocator
Linux kernel uses the buddy algorithm[1][2][3][4] as a page allocator, which is simple and efficient. The buddy algorithm is organize physical memory into blocks of various sizes, all of which are powers of two (order 1 means 2^1
). These blocks can be split into two "buddies" or combined with a buddy to form a large block
.
how buddy allocator works ? For example:
- when kernel needs a 16k sized memory, but there's no available 16k chunks
- the allocator will go to 32k size chunk
- 32k will be split into 16k + 16k. one 16k will be used and another 16k will be put in 16k sized chuck
The benefit
from buddy allocator is that it will keep the contiguous, reduce the fragmentation and fast allocation and deallocation of memory blocks. However, it can lead to be inefficient for allocating small amounts of memory
We can see available
page numbers in each order (e.g. 0, 1, 2, 3, ...., 11) using :
cat /proc/buddyinfo
chester@chester-desktop:~$ cat /proc/buddyinfo
Node 0, zone DMA 0 0 0 0 0 0 0 0 0 1 3
Node 0, zone DMA32 6407 5061 3363 2415 1379 510 142 26 6 3 5
Node 0, zone Normal 85846 51111 13170 3747 988 324 138 106 42 130 236
Node
: a NUMA (non-uniform memory access) system.Zone
: memory in the linux kernel is divided into zones based on address ranges.DMA
DMA32
Normal
: regular memory allocations.
The presence of larger blocks suggests lower memory fragmentation, as it indicates there are contiguous pages available for allocation. If the system run low on larger blocks, it may indicate memory fragmentation or high memory usage, which could lead to performance issues.