Fundamentals 13 min read

How Does Linux Manage Memory? A Deep Dive into Virtual Memory and Paging

This article explains the fundamentals of computer memory, covering linear addresses, virtual memory translation, paging mechanisms, page size queries, and the structure of multi‑level page tables used by Linux to efficiently map virtual to physical memory.

ITPUB
ITPUB
ITPUB
How Does Linux Manage Memory? A Deep Dive into Virtual Memory and Paging

Memory Basics

Memory is the computer's primary storage, allocating a separate address space for each process where data is kept. The smallest addressable unit is a byte, and memory addresses are numbered sequentially from 0, forming a linear address space. Hexadecimal notation (e.g., 0x00000003, 0x1A010CB0) is commonly used to represent these addresses. The range of the address space is limited by the width of the address bus; a 32‑bit CPU such as the Intel 80386 can address from 0x00000000 to 0xFFFFFFFF.

Virtual Memory

Virtual memory abstracts physical memory, giving each process its own virtual address space. Processes cannot directly access physical addresses; they can only use virtual addresses, which the operating system translates to real memory locations via page tables. This isolation ensures that processes cannot interfere with each other's data.

Each process has its own set of virtual addresses, which are independent of other processes. For example, two processes may both have a virtual address 0x10001000, but the OS maps them to different physical pages.

int v = 0;
printf("%p", (void *)&v);

The translation from virtual to physical addresses is performed entirely by the OS, allowing applications to remain unaware of the underlying hardware.

Memory Paging

Linux manages memory using paging, which groups memory into fixed‑size pages (typically 4 KB). Paging reduces the amount of address‑to‑page mapping the kernel must store.

$ getconf PAGE_SIZE
4096

The value 4096 indicates that each page holds 4096 bytes. Both physical memory and process address spaces are divided into these pages.

Multi‑Level Page Tables

To avoid the waste of a single linear page table, Linux employs multi‑level page tables. The first level indexes the high bits of a virtual page number, pointing to a second‑level table that contains the actual physical page mappings. Only the second‑level tables for used address ranges need to exist, dramatically reducing memory consumption.

In a simplified example, a virtual page number is split into two parts: the upper 8 bits are looked up in the first‑level table, which yields the address of a second‑level table. The lower 12 bits are then used to find the specific physical page entry.

This structure is analogous to a telephone directory where the area code (first level) points to a local directory (second level) that lists individual numbers. Unused area codes are left empty, so no second‑level tables are allocated for them.

Multi‑level tables also allow the kernel to store second‑level tables in non‑contiguous memory, making efficient use of fragmented RAM. Modern Linux kernels may use up to three levels of page tables for larger address spaces.

Conclusion

By dividing memory into pages and using multi‑level page tables, Linux efficiently maps virtual addresses to physical memory, enhancing process isolation, security, and overall system stability.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

linuxPage TableVirtual MemoryPaging
ITPUB
Written by

ITPUB

Official ITPUB account sharing technical insights, community news, and exciting events.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.