Memory Management in Linux Operating System
Memory Management in Linux Operating System
◦ Before we understand, it’s important to know that there are two types of memories in Linux.
• Physical Memory
• Virtual Memory
Physical memory is the actual memory present in the machine. While Virtual memory is a layer of
memory addresses that map to physical addresses.
Memory Allocation
Memory allocation is process of assigning memory to a process or program. In Linux,
kernel provides two main methods for memory allocation: static and dynamic.
◦ Static Memory Allocation
Static memory allocation is done at compile-time, where memory allocation for a
program is fixed and cannot be changed during runtime. memory is allocated in
program's data section or stack segment. data section contains global variables and
static variables, while stack segment contains local variables.
◦ Dynamic Memory Allocation
Dynamic memory allocation is done during runtime, where memory allocation for a
program can be dynamically adjusted based on program's requirements. kernel provides
various system calls such as malloc(), calloc(), and realloc() to dynamically allocate
memory. These functions allocate memory from heap segment of program's address
space.
Virtual Memory
◦ Swapping is a technique that allows kernel to move pages of memory from RAM to
a swap space on disk when system's memory is low.
◦ In Linux, swapping is implemented using a combination of hardware and software.
◦ Hardware component is disk, which is used as swap space.
◦ Software component is kernel's Swapping Manager, which manages swapping
process.
◦ When system's memory is low, Swapping Manager selects pages of memory to
swap out to disk, freeing up memory for other processes.
:
1$ cat /proc/meminfo
1$ top
3.free command
The free command displays the amount of free and used memory in the system. It’s a simple and compact command to use. It tells you
information such as how much free RAM you have on your system. It also tells you about the total amount
physical and swap memory on your system
1$ free
4.vmstat command
vmstat is a performance monitoring tool in Linux. It gives useful information about processes, memory,
block IO, paging, disk, and CPU scheduling. It reports virtual memory statistics of your system.
1$ vmstat
Demand paging
◦ “Demand Paging” is sometimes also called “Lazy Loading” or “Lazy Evaluation”. The reason for that is
that the memory management subsystem loads pages into memory when the executing program
demands/needs them.
Let’s discuss how the demand paging technique works:
Let’s say the CPU wants to access a page for a specific process. The first step is to look for the required page in
the page table.
Suppose we find the required page in the page table. Therefore, the CPU access the page and forwards it to the
process.
However, if we can’t find the target page, the page table generates a trap or page fault signal
. Furthermore, the page table sends the signal to the operating system.
This’s where we apply the demand paging technique.
The demand paging technique involves six steps:
Overall, demand paging requires complex algorithms and data structures to manage memory efficiently and
ensure that processes have the memory resources they need to execute.
Conclusion