You are on page 1of 13

MEMORY MANAGEMENT IN

LINUX OPERATING SYSTEM


-Sneha Chouhan
Msc cs III
Memory Management
In Linux, memory management system is designed to:

Efficiently manage memory usage


Allowing processes to access and
Use memory they require while preventing them from accessing memory they do
not own.
Physical vs Virtual Memory in Linux

◦ 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

◦ Virtual memory is a memory management technique that allows a program to use


more memory than is physically available in system.
◦ In Linux, virtual memory is implemented using a combination of hardware and
software.
◦ Hardware component is Memory Management Unit (MMU), which is responsible for
translating virtual memory addresses to physical memory addresses.
◦ Software component is kernel's Virtual Memory Manager (VMM), which manages
allocation and deallocation of virtual memory.
Swapping

◦ 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.
:

Commands for Memory Management in Linux


Let’s go over some of the commands for managing memory in
Linux.
1./proc/meminfo
The /proc/meminfo file contains all the information related to memory. To view this file use the cat command

1$ cat /proc/meminfo

2.The top command


The top command lets you monitor processes and system resource usage on Linux. It gives a dynamic real-
time view of the system. When you run the command, you’ll notice that the values in the output keep
changing. This happens as it displays the values in real time.

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

◦ In conclusion, process memory management is a crucial aspect of any operating


system, and Linux is no exception.
◦ Linux kernel provides a robust and efficient memory management system,
allowing processes to access and use memory they require while preventing them
from accessing memory they do not own.
◦Thankyou

You might also like