Management of Physical Memory

In Linux, The Physical memory is divided into 3 different zone or regions known as:
-
ZONE_DMA
-
ZONE_NORMAL
- ZONE_HIGHMEM
Let’s discuss each of them.
Zone_DMA: We are already aware that DMA stands for direct memory access.
Here the data is directly transferred between memory and peripheral devices without the intervention of CPU. Memory is divided between different ranges or zone. And this separation varies from one architecture to another.
If we take the example of Intel 80X86 architecture, the lower portion of the memory less than 16 MB is reserved for the Zone_DMA.
Zone_NORMAL: It is nothing but the physical memory which is mapped to the CPU. Whenever a process is accessing the memory, that memory is allocated from this zone onl;y. This zone is mostly used for most routine memory requests.
If we take example of Intel 80X86 architecture, the memory between 16 MB to 896 MB is reserved for the Zone_NORMAL.
Zone_HIGHHEM: HIGHMEM stands for high memory. The physical memory that isn't mapped into the kernel address space is referred to as ZONE_HIGHMEM.
If we take an example of Intel 80X86 architecture, the memory greater than 896 MB is reserved for the Zone_HIGHHEM.

Page allocator and Buddy heap
In this section of the blog, we will see how the memory manager allocates memory in the Linux kernel. The primary physical memory manager in the linux kernel is known as page allocator. Here every zone has its own page allocator. This page allocator is responsible for allocating contiguous memory depending upon the request. The allocator uses a buddy system to to keep track of available physical pages. There is a neighboring buddy or partner for each assignable memory region. A buddy heap is created if two designated partner regions become available.
This page allocator algorithm will check whether its adjacent memory region is free or not. In case it is free and allocatable, then they will be paired together. That means if the current memory and its adjacent memory are free, they will be paired together. That's why it is called the buddy system.
So page allocators in Linux get the help of a buddy system to allocate and deallocate memory. One important function of the buddy system is that if a small memory request cannot be satisfied to by allocating an existing small free region. Then the larger free region will be subdivided to form two partners to satisfy the request.
Let us take an example to understand the buddy heap algorithm.
Example
Suppose there is a memory request for 4 KB. And the smallest memory region available is 16KB. Then the 16 KB memory will be divided into 8 KB each. And further 8 KB will be divided into 4 KB and the memory will be allocated.

If required, the spilling of the memory will also be done by the buddy heap system in the Linux system.
Apart from this, linux hs different memory allocator methods. Whatever we have seen of the page allocator and buddy heap system, they allocate the memory dynamically. Memory allocation also occurs statically(during system boot time itself). For kernel data structures, a separate slab allocator is used.
For maintaining the pages of file contents, separate cache memory is also maintained, which is called page cache. This page cache is separately maintained by the kernel main cache for files and the main mechanism for I/O devices to block devices.
Slab Allocation Strategy
This strategy is used for allocating kernel memory. First, let us know what is basically a slab. Memory is allocated for kernel data structures using a slab. It has one or more physically connected pages.
There may be one or multiple slabs in a cache. Additionally, each distinct kernel data structure has its own cache. That means there is a different cache for different memory requirements.

Let us see the working of this algorithm.
This algorithm uses caches for storing kernel objects. When a cache is created, a number of objects are allocated to the cache.
Initially, all the objects in the cache are marked as free.
The slab is in three states:
-
full,
-
empty,
- and partial.
Whenever a new object is allocated, the allocator will assign any free object from the cache to satisfy the request. And that object will be marked as used. The number of objects in the cache depends on the size of the associated slabs.
So far, we’ve discussed the allocation and deallocation of physical memory, it is time to discuss the management of virtual memory by Linux.
Must Read, Components of Operating System
Management of Virtual Memory

Before we start discussing the management of virtual memory in the Linux operating system, let us first discuss what virtual memory is.
Virtual memory is nothing but the address space visible to each process. As we already know that every program is associated with an address space which is the set of addresses used by the processes, and it owns the virtual memory starting from the address zero. Management of that address space is done by the virtual memory system of the Linux Operating System.
In a nutshell, what operations are being performed are:
Wherever a process makes a request, it creates pages of virtual memory on demand and manages the loading of those pages from disk or their swapping back out to disk as required.
The VM manager maintains two separate views of a process’s address space:
Logical View and Physical View

The logical view describes the instructions that the virtual memory manager receives concerning the layout of the address space.
-
This logical view contains the set of non-overlapping regions, each representing a continuous, page-alligned subset of the address space.
-
Each region is described internally by a single vm_area_struct structure that defines the properties of the region, including all process’s read, write and execute permission in the region as well as information about any files associated with the region.
- The regions for each address space are linked into a balanced binary tree to allow fast lookup of the region corresponding to any virtual address.

The physical view is nothing but the view of the virtual memory represented by the information stored in the hardware page tables of the process. For every process, we have a separate page table which is another view of virtual memory.
So, what information is going to be stored in the page table?
Every page table entry will store the exact current location of each table in the virtual memory. Virtual memory is used to implement the user’s view of the logical address space.
Now, a question may arise what if the request is made for the page that is not currently present in the page table?
-
In that case, some set of routines needs to be maintained.
-
The physical view manages the set of routines that are invoked from the kernel’s software-interrupt handlers whenever a process tries to access a page that is not currently present in the page tables.
-
Each vm_area_struct in the address space description contains a field that points to a table of functions that implement the key-page management functions for any given virtual memory region.
- All requests then to read or write for an unavailable page is eventually dispatched to the appropriate handler in the function table for the vm_area_struct.
Let us now discuss the virtual memory regions and lifetime.
Virtual Memory Regions and Lifetime

Linux implements several types of virtual memory. Most memory regions are backed either by a file or nothing. A region backed by nothing is the simplest type of virtual memory region. Such a region represents demand-zero memory: When a process tries to read a page in such a region, it is simply given back a page of memory filled with zeroes.
The lifetime of a virtual address space:
The kernel creates a new virtual address space:
-
When a process runs a new program with the exec() system call.
-
Upon creation of a new process by the fork() system call.
Frequently asked questions
What is the exit code in Linux?
An exit code in Linux represents the result of the execution of a command or script. It ranges from 0 to 255. We can tell whether a process ran successfully or not by looking at the exit codes.
What is a firewall?
A firewall is a collection of rules. The contents of each data packet that enters or leaves a secure network space are checked against the firewall rules to determine if they should be permitted to pass.
What is the maximum length for a filename under Linux?
Under Linux, a filename can only be 255 bytes long.
Conclusion
In this blog, we have discussed Memory management in Linux. We have seen different memory management tricks along with slab allocation strategy and page allocator.
If you think this blog has helped you enhance your knowledge about the above question, and if you want to learn more, check out our articles.
🔥 Linux - Decision Making
🔥 Linux File System
🔥 Linux- Shell Loops
🔥 Linux Security
Touch command in linux
And many more on our Coding Ninjas Studio.
Visit our website to read more such blogs. Make sure that you enroll in the courses we provide, take mock tests, solve problems available, and interview puzzles. Also, you can pay attention to interview stuff- interview experiences and an interview bundle for placement preparations.
Please upvote our blog to help other ninjas grow.
Happy Learning!