Professional Documents
Culture Documents
Memory Management in Linux
Memory Management in Linux
40.1 Introduction
In this module, we learn how memory management is handled in the Linux operating
system. We learn how physical memory and virtual memory are managed. Linux memory
management includes allocating and freeing physical memory – pages, groups of pages, and
small blocks of main memory and handling virtual memory that is memory mapped into the
address space of running processes.
In this section we learn what the buddy system is and how it is used for allocating pages.
Adjacent units of allocatable memory are paired together. Each allocatable memory region has
an adjacent partner called a buddy. Whenever two allocated partner regions (adjacent regions)
are freed up, they are combined to form a larger region called a buddy heap. The larger region
also has a partner with which it can combine to form a still larger free region. If there is a
request for a small region and if a region of that size is not available, a larger region is split into
two partners. This splitting can happen till the region goes down to the size of a page. That is,
the smallest size that can be allocated is a single page.
Figure 40.1 shows how memory is split in a buddy heap when there is a request for a
smaller memory space. Suppose there is a request for a memory space of 4KB and the region
that is available is of size 16 KB. This large region is first split into two partners of 8 KB size
each. One of the two 8 KB regions is split into two 4 KB regions. One of the two 4 KB regions is
allocated to the requesting process. Now, there is 4 KB free region and a 8 KB free region.
40.2.2 Management of Physical Memory – Slab Allocator
We now see how memory is allocated for kernel data structures. There are a number of data
structures that are maintained in the kernel. When these data structures grow, there arises a
need of allocating space for the data structures in the main memory.
A slab is made up of one or more physically contiguous pages. A cache consists of one or
more slabs. The slab allocator consists of a variable number of caches that are linked together
on a doubly linked circular list called a cache chain. For each unique kernel data structure, there
is a single cache. For example, there is a cache for inodes, a cache for file objects and so on.
Each cache is populated with objects that are instantiations of the kernel data structure the
cache represents. For example, the inode cache stores instances of inode data structures.
The slab-allocation algorithm uses caches to store kernel objects. When a cache is created,
a number of objects are allocated to the cache. The number of objects in the cache depends on
the size of the associated slab. A 12 KB slab (3 contiguous 4-KB pages) can store six 2-KB
objects. Figure 40.2 shows two 3 KB objects that are kept in two different caches. Caches are
made of slabs and slabs are made of contiguous physical pages.
All objects in the cache are initially marked free. When a new object is needed for a kernel
data structure, the allocator can assign any free object from the cache to satisfy the request.
When a new task is created, a new process descriptor object (struct task_struct) is to be
assigned. The cache will fulfill the request using a free slab.
A slab may be 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 used and free objects
The slab allocator first attempts to satisfy the request with a free object present in a partial
slab. If none exists, the object is assigned from an empty slab. If no empty slabs are available, a
new slab is allocated from contiguous physical pages and assigned to a cache; memory for the
object is allocated from this newly allocated slab.
The slab allocator has three principle aims:
(i) The allocation of small blocks of memory to help eliminate internal fragmentation
that would be otherwise caused by the buddy system
(ii) The caching of commonly used objects so that the system does not waste time
allocating, initializing and destroying objects. Benchmarks on Solaris showed
excellent speed improvements for allocations with the slab allocator
(iii) The better utilization of hardware cache by aligning objects to the L1 or L2
caches.
40.4 Summary
In this module, we learnt how physical memory is managed in Linux. The memory was
divided into zones for better handling of memory for different architectures. We learnt the page
allocator used for allocating memory. We also learnt the slab allocator used for allocating
memory for kernel data structures. We also learnt how virtual memory is managed in Linux.
References
[1] Abraham Silberschatz, Peter B. Galvin, Greg Gagne, “Operating System Concepts”, Ninth
Edition, John Wiley & Sons Inc., 2012.
[2]https://www.kernel.org/doc/gorman/html/understand/understand005.html
[3]https://www.redhat.com/en/about/blog/memory-zones-and-linux-driver-issue
[4]https://www.kernel.org/doc/gorman/html/understand/understand011.html