How Memory Management Works in Linux
Published: 2023-04-22 07:01:46

---
title: How Memory Management Works in Linux
published: true
date: 2023-04-21 19:08:23 UTC
tags: 
canonical_url: https://harshmange.hashnode.dev/how-memory-management-works-in-linux
---

Linux uses a demand-paged virtual memory management system, which allows applications to use more memory than is physically available in the system. The virtual memory system is based on the concept of memory pages, which are fixed-size blocks of memory. When an application requests memory, the OS allocates memory pages to the application, and when the application finishes using the memory, the pages are released back to the system.

The Linux kernel has several memory management components, including the memory manager, the page fault handler, and the swap manager. These components work together to manage the system's memory resources and ensure that applications have enough memory to run efficiently.

## Memory Manager

The Memory Manager is responsible for managing the allocation and deallocation of memory pages to applications. It tracks the physical memory usage of the system and the amount of free memory available. When an application requests memory, the Memory Manager uses an allocation algorithm to determine which memory pages to allocate to the application.

## Page Fault Handler

The Page Fault Handler is responsible for handling page faults, which occur when an application attempts to access a memory page that is not currently in physical memory. When a page fault occurs, the Page Fault Handler retrieves the page from disk and copies it into physical memory.

## Swap Manager

The Swap Manager is responsible for managing the system's swap space, which is a portion of the hard disk used as an extension of physical memory. When the system runs out of physical memory, the Swap Manager swaps out the least frequently used memory pages from physical memory to the swap space. When an application requests a swapped-out page, the Swap Manager retrieves the page from the swap space and copies it back into physical memory.

## Memory Allocation Algorithms

Linux uses several memory allocation algorithms, including the buddy algorithm and the slab allocator. The buddy algorithm is used to allocate memory pages of a fixed size, while the slab allocator is used to allocate memory pages for data structures of varying sizes.

The buddy algorithm works by dividing the physical memory into blocks of fixed sizes, known as buddies. When an application requests memory, the Memory Manager looks for the smallest available buddy that can satisfy the request. If there is no available buddy of the appropriate size, the Memory Manager combines two or more smaller buddies to create a larger one.

The slab allocator works by dividing the memory into caches, each of which is used to store objects of a specific size. When an application requests memory for a particular data structure, the Memory Manager looks for an available cache that can satisfy the request. If there is no available cache of the appropriate size, the Memory Manager creates a new cache for the data structure.

## Memory Management in Practice

To see memory management in action, we can use the Linux command-line utility top. Top provides a real-time view of the system's memory usage and processes.

To open top, open the terminal and type "top" and press enter. This will show a list of processes running on the system, along with their CPU and memory usage.

The memory usage is displayed in the VIRT, RES, and SHR columns. VIRT represents the total virtual memory used by the process, RES represents the physical memory used by the process, and SHR represents the shared memory used by the process.

We can use the kill command to terminate a process and free up its memory resources. For example, to terminate a process with a PID of 1234, type "kill 1234" and press enter.