Demystifying Linux Virtual Memory: Concepts, Paging, and Practical Commands
This article explains Linux virtual memory fundamentals, covering its purpose, paging and page tables, address translation, swap usage, common issues like 32‑bit vs 64‑bit limits, and practical commands such as free, vmstat, pmap, and sysctl for monitoring and configuring memory management.
What is Virtual Memory?
Virtual memory is one of the most important concepts in operating systems because memory acts as a buffer between the fast, limited CPU and slower I/O devices. Modern OSes run multiple tasks concurrently, which creates conflicts over physical memory; virtual memory solves this by giving each process the illusion of having the entire address space.
The diagram above illustrates the simplest view of virtual memory: the OS pretends that the whole memory belongs to each process, then maps the virtual addresses to actual physical pages on demand.
Paging and Page Tables
Mapping every byte of virtual memory to a physical address would require huge tables (e.g., 32 GB for a 4 GB address space on a 32‑bit system). To reduce this, the OS introduces the concept of a Page, typically 4 KB in size. The OS divides physical memory into pages and maintains a page table that maps virtual pages to physical pages, shrinking the table to a few megabytes for a 4 GB system.
Memory Addressing and Allocation
When a process accesses memory, the CPU’s Memory Management Unit (MMU) translates the virtual address to a physical address. If the required physical page is not yet allocated, the OS raises a page fault interrupt, switches to kernel mode, and assigns a physical page.
Functions of Virtual Memory
Process Memory Management
Memory integrity: each process sees a contiguous address space, so developers can ignore physical fragmentation.
Security: page‑table entries contain permission bits, enabling fine‑grained access control.
Data Sharing
Virtual memory makes sharing libraries and data easy. When multiple processes load the same library, the OS maps each process’s virtual address to the same physical pages, avoiding duplicate copies. Shared memory regions are created by mapping the same physical pages into different processes’ virtual spaces.
Swap
When physical memory runs low, Linux can move inactive pages to a swap partition on disk. This “swap out” mechanism frees RAM for active processes, allowing the system to run more workloads than the physical RAM would otherwise permit.
Common Questions
32‑bit vs 64‑bit
A 32‑bit CPU can address up to 2^32 = 4 GB of memory; installing more RAM is ineffective because the CPU cannot address it. A 64‑bit CPU can address far larger spaces, but Linux currently supports up to 256 GB of RAM in typical configurations. Running 64‑bit applications on a 32‑bit kernel is not possible because the virtual address format differs.
Direct Physical Memory Access
Linux exposes devices under /dev/. The physical memory itself is accessible via /dev/mem; a root user can read or write this file to manipulate memory directly.
JVM Processes Using Excessive Virtual Memory
Java processes often show large values in the VIRT column of top because the Glibc arena allocator reserves virtual address space without immediately backing it with physical memory. Each thread stack also reserves 1 MB of virtual memory. The actual physical usage is shown in the RES column.
Common Management Commands
Viewing System Memory Status
Commands such as free and vmstat display current memory usage. Note that “available” memory includes reclaimable buffers and caches, not just the free column. Detailed information can be obtained with cat /proc/meminfo.
pmap
To inspect a single process’s virtual memory layout, use pmap pid. Adding the -XX option provides more detailed output.
Modifying Memory Configuration
System parameters can be changed with sysctl vm [-options] CONFIG or by writing directly to files under /proc/sys/vm/.
SWAP Operations
Excessive swapping can degrade performance. To reduce swap usage, set vm.swappiness=0 or disable swap entirely with swapoff.
Conclusion
Virtual memory is conceptually simple but leads to many intricate details, such as segment registers, cache optimizations, and advanced paging strategies. This article covered the core principles, leaving deeper topics for future exploration.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
