Fundamentals 13 min read

Understanding Linux Virtual Memory: From Pages to Swap

This article explains the fundamentals of Linux virtual memory, covering its origin, paging and page tables, memory addressing, the role of swap, common questions about 32‑bit vs 64‑bit systems, and practical commands for monitoring and configuring virtual memory on Linux.

Programmer DD
Programmer DD
Programmer DD
Understanding Linux Virtual Memory: From Pages to Swap

Recently, our team held a much‑anticipated sharing session on "Linux Virtual Memory" after the leader noticed that several colleagues were unclear about the concept.

The author, feeling a bit guilty for not having studied operating systems during university, spent personal time watching Harbin Institute of Technology's OS course on NetEase Cloud Classroom, reading the book "Linux Kernel Design and Implementation", and exploring low‑level system knowledge while writing a simple C server.

When another colleague asked a virtual‑memory‑related question, the author realized that his understanding was still shallow and decided to reorganize the knowledge.

Origin of Virtual Memory

Virtual memory is one of the most important concepts in an operating system because memory plays a strategic role: the CPU is fast but has limited capacity, while I/O devices are slower and provide various functions, requiring memory as a buffering layer.

Modern OSes support multitasking, which greatly improves CPU utilization but creates conflicts when multiple processes access memory; virtual memory was introduced to solve this problem.

The above diagram gives the simplest and most intuitive explanation of virtual memory.

The OS has a block of physical memory (the middle part) and multiple processes (e.g., P1 and P2). It tells each process that the entire memory space is theirs, but in reality the OS only assigns a virtual address space and maps it to physical pages on demand. When a process accesses memory, the OS translates the virtual address to a physical address, possibly moving data around so that each process thinks it owns the whole memory.

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 system). To reduce this, the OS introduces the concept of a Page, dividing memory into 4 KB units.

At boot, the OS splits physical memory into pages. Memory allocation then works in page units, and the virtual‑to‑physical mapping table (the page table) becomes much smaller—only about 8 MB for 4 GB of RAM. Unused virtual pages need no entries, and Linux uses multi‑level page tables to further reduce memory consumption.

Memory Addressing and Allocation

When a process accesses memory, the CPU’s Memory Management Unit ( MMU) translates the virtual address to a physical address, using the page table. If the required physical page is not yet allocated, the OS generates a page‑fault interrupt and allocates a physical page while switching to kernel mode.

Functions of Virtual Memory

Process Memory Management

Memory integrity: each process sees a contiguous address space, so developers can ignore physical fragmentation.

Security: the OS can set permission bits in page‑table entries to control access.

Data Sharing

Virtual memory makes sharing libraries and data between processes easier. When multiple processes load the same library, the OS maps the same physical pages into each process’s virtual address space, avoiding duplicate copies.

Swap

When physical memory is exhausted, Linux can use a swap partition. Pages that are not actively used are moved to disk, freeing RAM for active processes. This allows processes to use more memory than physically available.

Common Questions

32‑bit vs 64‑bit

A 32‑bit CPU can address up to 4 GB of memory (2³² addresses). A 64‑bit CPU can address far more, but Linux currently supports up to 256 GB of RAM. Running 64‑bit software on a 32‑bit system is not possible because the virtual address space layout differs.

Direct Physical Memory Access

Linux maps devices under /dev/. The memory device /dev/mem can be read/written by the root user to access 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 a lot of virtual memory. Each Java thread stack consumes 1 MB of virtual memory by default. The actual physical memory used is shown in the RES column.

Common Management Commands

To view memory status:

free
vmstat
cat /proc/meminfo

(detailed info, e.g., dirty pages)

To inspect a process’s virtual memory layout: pmap pid (add -XX for more detail)

To modify memory configuration: sysctl vm [options] CONFIG or edit files under /proc/sys/vm/ Swap settings:

Set vm.swappiness=0 to minimize swap usage.

Use swapoff to disable swap entirely.

Conclusion

Virtual memory is easy to grasp at a high level, but it leads to many complex topics such as segment registers, cache enhancement, and buffer management, which deserve separate discussion.

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.

Memory ManagementLinuxVirtual MemoryOperating SystemSwapPaging
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

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.