Virtual Memory Management
Virtual Memory Management
Introduction to Virtual Memory
Virtual memory is the concept of allowing a computer to use more memory than is physically available by treating a portion of the disk (backing storage) as an extension of main memory. The key idea is that only the parts of a process that are actually needed at a given moment are loaded into physical memory, while the rest remains on disk. This enables:
Running programs larger than physical memory – only required pages are brought in.
Supporting more processes simultaneously – each process uses only its active pages.
Increasing CPU utilization – less time wasted loading unnecessary code.
Faster program startup – only the first page(s) need to be loaded initially, not the entire program.
The virtual address space (logical memory) starts at address 0 and extends up to the maximum possible virtual size. It is split into pages, just as physical memory is split into frames. A page table maps each virtual page to either a physical frame (if in memory) or to a location on disk (if swapped out).
Swap space is a dedicated area on disk (often a file or partition) used to hold pages of processes that are not currently in physical memory. It is the enabler of virtual memory.
Demand Paging
Demand paging is the strategy of loading pages only when they are actually referenced (on demand), rather than loading the entire process into memory at startup. This is also called lazy swapping. The operating system component responsible for bringing pages in is the pager.
Page Table Entries and Valid/Invalid Bits
Each entry in the page table contains a valid/invalid bit:
Valid: the page is present in physical memory (frame number is given).
Invalid: the page is not in physical memory (either on disk or not part of the process address space).
If a process tries to access an invalid page, a page fault occurs.
Page Fault Handling Sequence
The process issues a memory reference.
The hardware checks the page table; the valid bit is 0 → hardware traps to the operating system.
The OS first verifies that the reference is legal (within the process address space). If illegal, the process is terminated (segmentation fault).
The OS finds a free frame in physical memory.
The OS initiates a disk I/O to read the required page from swap space into the free frame.
While waiting for the I/O, the process is placed in a waiting queue and the CPU schedules another process.
When the disk I/O completes, the page table is updated (valid bit set, frame number recorded).
The process is moved back to the ready queue, and on resumption the faulting instruction is restarted.
This sequence is similar to a regular I/O wait: the process state is saved, it waits, and then it is re-scheduled.
Pure Demand Paging
Pure demand paging starts a process with no pages loaded. The very first instruction causes a page fault, which brings in the needed page. This continues as the process runs. In practice, many systems may bring in the first page(s) to reduce startup faults, but the principle remains.
Locality of Reference
Programs tend to access a small set of pages repeatedly over short time intervals (e.g., loops, sequential code execution). This is called locality of reference (spatial and temporal locality). It reduces the frequency of page faults because the same pages are reused often.
Performance of Demand Paging
The effective access time (EAT) for a memory access in a demand-paged system depends on the probability of a page fault:
where:
p = probability of a page fault (0≤p≤1)
memory access time = time to access physical RAM (nanoseconds)
page fault time = time to handle the fault, including disk I/O (milliseconds)
Impact of Page Fault Rate
No page faults (p=0): EAT= memory access time.
With page faults: even a tiny p drastically slows performance because disk I/O is orders of magnitude slower than RAM.
For example, if memory access is 100 ns and page fault handling takes 8 ms (8,000,000 ns), then:
To keep slowdown under 10% (EAT < 110 ns), we need p<7,999,90010≈1.25×10−6, i.e., fewer than 1 page fault per 400,000 accesses.
Optimizations
Use swap space instead of the standard file system: swap space is allocated contiguously, making disk I/O faster.
Clustering (also called prepaging): when loading a page, also load several adjacent pages (e.g., pages 2–4 if page 1 is needed) because of locality.
Modified (dirty) bit: track whether a page has been written to. If a page is clean (not modified), it does not need to be written back when swapped out—the copy on disk is still valid. This reduces disk I/O.
Page Replacement
When a page fault occurs and no free frames are available, the operating system must replace an existing page in memory to make room for the new page. The page selected for removal is called the victim page. The replacement decision is critical to performance.
Dirty Pages and I/O Overhead
If the victim page has been modified (dirty bit set), it must be written back to swap space before the new page can be loaded. This adds a second disk I/O. If the page is clean, it can simply be discarded (its disk copy is current). Therefore, preferring clean pages as victims reduces overhead.
Page Replacement Algorithms
Several algorithms exist; the goal is to minimize page faults. They are evaluated using a page reference string – a sequence of page numbers requested by a process.
First-In, First-Out (FIFO)
Replace the page that was brought into memory longest ago (the oldest page).
Simple to implement (a queue).
Can suffer from Belady’s anomaly: increasing the number of frames can increase the number of page faults.
Example with reference string
7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1and 3 frames: produces 15 page faults.
Frames: [7] [0,7] [1,0,7] → fault on 2 replaces 7 → [2,1,0] → etc.Optimal Page Replacement (OPT or PR)
Replace the page that will not be used for the longest time in the future.
Provides the minimum possible page faults for a given reference string.
Not implementable in practice because the future is unknown.
Used as a benchmark to compare other algorithms.
For the same string with 3 frames: 9 page faults.
Frames: [7] [0,7] [1,0,7] → fault on 2, replace 7 (next use far ahead) → [2,0,1] → etc.Least Recently Used (LRU)
Replace the page that has not been used for the longest time in the past.
Exploits locality: pages used recently are likely to be used again soon.
Performs well in practice, close to optimal.
No Belady’s anomaly (it is a stack algorithm).
Implementation overhead: requires tracking the order of accesses. Can be approximated with a hardware counter (each page entry stores a timestamp) or a stack of page numbers (most recently used at top).
For the sample string with 3 frames: 12 page faults.
Frames: [7] [0,7] [1,0,7] → fault on 2, LRU is 7 → [2,1,0] → etc.Comparison Summary
Algorithm | Page Faults (3 frames) | Notes |
|---|---|---|
FIFO | 15 | Simple, Belady’s anomaly |
Optimal | 9 | Best possible, not realizable |
LRU | 12 | Good performance, costlier to implement |
Thrashing
Thrashing occurs when a system spends more time swapping pages in and out than executing processes. Symptoms: extremely high disk activity, CPU utilization drops, system becomes unresponsive.
Cause
The working set of each process (the set of pages currently being used) is larger than the available physical memory.
The operating system keeps introducing more processes, exceeding memory capacity.
As page faults increase, processes are suspended and resumed repeatedly, causing more page faults.
Working Set Model
The working set WS(Δ) for a process is the set of pages referenced in the last Δ time units (a window). Each process migrates through different localities (e.g., edit, compile, spell check). If the total working set of all active processes exceeds physical memory, thrashing results.
Prevention
Working set strategy: Monitor the working set size of each process. Only allow a process to run if its working set fits into available memory. Otherwise, suspend a process to free frames.
Page fault frequency (PFF) control: Set an upper threshold for acceptable page fault rate. If rate exceeds threshold, suspend or block processes; if too low, add more processes.
Thrashing Graph
As CPU utilization increases, multiprogramming level rises. At a certain point, thrashing begins: CPU utilization drops sharply because the system is busy paging instead of executing. This is visible in the classic “knee” curve.
Case Study: Windows Virtual Memory
Windows implements demand paging with clustering (prefetching several pages around the faulted page). It maintains a minimum and maximum working set per process:
Minimum working set: guaranteed number of pages a process can have (e.g., 50 pages).
Maximum working set: upper limit beyond which the process cannot grow.
When free memory is low, the working set manager trims (reduces) the working sets of processes that are not actively using all their pages.
This helps control memory pressure and reduces thrashing.
Virtual memory management is a cornerstone of modern operating systems, allowing efficient memory utilization, protection, and support for large applications. The choice of page replacement algorithm, careful handling of page faults, and detection/prevention of thrashing are critical to system performance.

Ask anything
We've read this whole study set, so questions about it get the best answers.