Why Does Your Computer Need Virtual Memory? A Deep Dive into Memory Management
This article explains the fundamentals of operating‑system memory management, covering physical memory limits, the need for address spaces, base‑limit protection, paging, virtual memory mapping, page‑fault handling, and a comprehensive review of page‑replacement algorithms such as FIFO, LRU, Clock, and Working‑Set.
What Is Physical Memory?
Physical memory refers to the actual RAM modules installed in a computer. Its usable size is limited not only by the capacity of the modules but also by the CPU's address‑bus width; for example, a 20‑bit bus can address only 1 MiB regardless of a 100 GiB module.
Drawbacks of Direct Physical Memory Access
When programs access physical addresses directly, two major problems arise:
If only one process runs, unrestricted access can corrupt the operating system.
With multiple processes, they may interfere with each other's memory, leading to security and stability issues.
Therefore, safety cannot be guaranteed when processes share raw physical memory.
Creating Isolated Address Spaces
To solve the above problems, an abstract address space is introduced for each process. Each process receives its own contiguous range of virtual addresses, isolated from others.
Implementation often uses a base register A and a limit register B. The physical address for a virtual offset x is computed as A + x, provided A + x < B. If the limit is exceeded, an error is raised.
Virtual Memory Overview
Virtual memory extends the address space beyond physical RAM by mapping virtual pages to physical frames on demand. It combines the benefits of both overlay (coverage) and swapping techniques, allowing programs larger than available RAM to run without programmer intervention.
Mapping Virtual to Physical Memory
The CPU’s Memory Management Unit (MMU) translates virtual addresses to physical addresses. The MMU uses a page table that stores the mapping from virtual page numbers to physical frame numbers.
When a program accesses a page not currently in RAM, a page fault occurs, prompting the OS to load the required page from secondary storage.
Paging Memory Management
Both virtual and physical address spaces are divided into fixed‑size blocks: pages in virtual memory and frames in physical memory. Typical page size is 4 KiB, balancing internal fragmentation and page‑table size.
Each page table entry contains several fields:
Frame number : the physical frame that holds the page.
Valid bit : indicates whether the page is resident in RAM.
Protection bits : read/write/execute permissions.
Modified (dirty) bit : set when the page has been written.
Accessed bit : used by replacement algorithms to track recent use.
Cache‑disable bit : optionally prevents caching of the page.
Speeding Up Address Translation
Every memory access requires a page‑table lookup, which can become a bottleneck. Modern CPUs include a Translation Lookaside Buffer (TLB) that caches recent virtual‑to‑physical mappings. If a lookup misses the TLB, the page table is consulted and the TLB is updated.
Multi‑Level Page Tables
To avoid storing a huge single‑level page table, operating systems use hierarchical page tables. For a 32‑bit address space with 4 KiB pages, a two‑level scheme splits the address into a 10‑bit first‑level index, a 10‑bit second‑level index, and a 12‑bit offset. Only the second‑level tables that are actually needed are allocated, dramatically reducing memory consumption.
Page Fault Handling
If a free frame exists, allocate it; otherwise select a victim frame using a replacement algorithm.
If the victim page is dirty, write it back to secondary storage.
Update the victim’s page‑table entry to mark it invalid.
Load the required page into the chosen frame.
Update the new page’s entry to mark it valid and record the frame number.
Restart the faulting instruction.
Page Replacement Algorithms
When RAM is full, the OS must decide which page to evict. Common algorithms include:
Optimal (OPT) : replaces the page whose next use is farthest in the future (theoretical benchmark).
First‑In‑First‑Out (FIFO) : evicts the oldest loaded page.
Least‑Recently‑Used (LRU) : evicts the page that has not been used for the longest time.
Second‑Chance (Clock) : a practical approximation of LRU using a reference bit.
Least‑Frequently‑Used (LFU) : evicts the page with the smallest access count, often combined with periodic counter decay.
Working‑Set : keeps pages referenced within a recent time window, evicting those outside the window.
Working‑Set Clock : combines the working‑set concept with the clock algorithm to avoid scanning the entire table.
Segmentation Memory Management
Historically, segmentation divided a program into logical segments (code, data, stack) with separate base addresses, allowing relocation without changing internal addresses. Modern systems largely rely on paging, but segmentation can still be used for logical organization.
References
https://www.zhihu.com/question/50796850
https://www.zhihu.com/question/63375062
https://yuerer.com/操作系统之-虚拟存储页面置换算法/
《Modern Operating Systems》
《Operating Systems: Three Easy Pieces》
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.
