Fundamentals 11 min read

Why Virtual Memory Matters: Understanding Paging, Page Tables, and Swap in Linux

This article explains the fundamentals of virtual memory in Linux, covering its purpose, paging and page tables, address translation, swap usage, common questions about 32‑ vs 64‑bit systems, direct memory access, JVM memory consumption, and essential commands for monitoring and configuring memory.

Open Source Linux
Open Source Linux
Open Source Linux
Why Virtual Memory Matters: Understanding Paging, Page Tables, and Swap in Linux

Virtual Memory

Virtual memory is one of the most important concepts in operating systems because it acts as a buffer between the fast CPU and slower I/O devices, allowing multiple processes to share memory without conflicts.

Modern OSes support multitasking, which increases CPU utilization but creates conflicts when several processes access memory; virtual memory solves this by giving each process the illusion of having the entire address space.

The diagram above shows the simplest illustration of virtual memory.

The OS has a physical memory region in the middle and several processes (e.g., P1 and P2). It tells each process that the whole memory belongs to it, but actually assigns only a number; the real mapping occurs only when the process accesses memory, at which point the OS translates virtual addresses to physical ones.

This deception means each process believes it occupies the whole memory while being unaware of the actual physical addresses.

Paging and Page Tables

Virtual memory is implemented as a set of translation tables. Mapping each byte would require huge tables (e.g., 32 GB for a 4 GB address space), so operating systems introduce the concept of a Page.

At boot the OS divides physical memory into 4 KB pages. Memory allocation is then done in page units, reducing the size of the page‑to‑page mapping table to about 8 MB for 4 GB of RAM. Linux also uses multi‑level page tables to further cut memory consumption.

The mapping table is called a page table.

Memory Addressing and Allocation

When a process accesses memory, the OS translates the virtual address to a physical address using the Memory Management Unit (MMU). If the required physical page is not yet allocated, the CPU triggers a page fault, and the kernel allocates a physical page for the process.

Functions

Virtual memory not only resolves address conflicts but also provides several benefits:

Process Memory Management

Memory integrity: each process sees a contiguous address space, so developers need not worry about fragmented physical memory.

Security: the OS can set access‑permission bits in page‑table entries to control which processes may read or write a page.

Data Sharing

Virtual memory makes sharing libraries and shared memory regions straightforward: different processes can map the same physical pages into their own virtual address spaces.

SWAP

When physical memory is exhausted, Linux can move rarely used pages to a swap partition on disk, allowing processes to continue running with more apparent memory.

FAQ

32‑bit vs 64‑bit

A 32‑bit CPU can address up to 2^32 = 4 GB of memory, so installing more than 4 GB is ineffective. A 64‑bit CPU can address far more, but the actual limit depends on the OS; Linux currently supports up to 256 GB.

Direct Physical Memory Access

Linux exposes devices under /dev/. The memory device /dev/mem can be read or written by the root user to manipulate physical memory directly.

JVM Virtual Memory Usage

Java processes often show large values in the VIRT column of top because the Glibc arena allocator reserves virtual address space, and each thread stack consumes 1 MB of virtual memory. The RES column shows the actual resident physical memory.

Common Management Commands

To inspect memory usage you can use free, vmstat, or cat /proc/meminfo. The pmap pid command displays a process’s virtual memory layout; adding -XX provides more detail.

System configuration can be changed with sysctl vm … or by writing to files under /proc/sys/vm/. To reduce swap usage you can set vm.swappiness=0 or disable swap entirely with swapoff.

Conclusion

Virtual memory is easy to grasp conceptually, but it leads to many advanced topics such as segment registers, cache enhancement, and buffer management, which are beyond the scope of this article.

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.

LinuxVirtual Memorypage-tablesPaging
Open Source Linux
Written by

Open Source Linux

Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.

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.