Skip to content
Chethan Yadav_
Operating Systems

OS/Lesson 8 of 9/6 min read

Virtual Memory

Virtual memory, demand paging, page faults, copy-on-write, page replacement algorithms (FIFO, Optimal, LRU, Clock), Belady's anomaly, thrashing and the working set model.


On this page

What is Virtual Memory

Virtual memory separates the logical memory a program sees from the physical memory installed. A process can be larger than RAM because only the parts in active use need to be in memory; the rest stays on disk.

Benefits:

  • Programs are not limited by physical RAM size.
  • More processes fit in memory, so higher multiprogramming.
  • Less I/O to load or swap a program (only needed pages).
  • Easy sharing (shared libraries mapped into many processes) and isolation.

It is implemented mainly with demand paging.

Locality of Reference

Virtual memory works because programs show locality of reference:

  • Temporal locality: recently used data is likely to be used again soon (loop counters).
  • Spatial locality: data near recently used data is likely to be used soon (array traversal).

The same principle makes caches and the TLB effective (see memory hierarchy).

Demand Paging

With demand paging, a page is loaded into memory only when it is first accessed. A lazy swapper (pager) never brings in a page unless it is needed.

Pure demand paging: a process starts with zero pages in memory; the very first instruction faults.

Page Fault

A page fault is a trap raised by the MMU when a process accesses a page whose valid bit is not set.

Handling steps:

  1. Check the access is legal (otherwise kill the process: segmentation fault).
  2. Find a free frame (or pick a victim with page replacement).
  3. Schedule a disk read of the page into the frame.
  4. Update the page table, set the valid bit.
  5. Restart the instruction that faulted.

Effective access time with page fault rate p:

EAT = (1 - p) * memory_access + p * page_fault_service_time

Example: memory access 200 ns, fault service 8 ms. Even p = 1/1000 gives EAT ~ 8.2 us, a 40x slowdown. Page fault rate must be extremely low.

Minor (soft) fault: page is in memory but not mapped (e.g. shared library already loaded). Major (hard) fault: page must be read from disk.

Copy-on-Write

Copy-on-write (COW): after fork(), parent and child share the same pages marked read-only. When either writes, a page fault occurs and the kernel copies only that page. This makes fork() followed by exec() cheap.

Page Replacement

When no frame is free on a page fault, the OS picks a victim page to evict. If the victim is dirty (modified), it must be written to disk first; the dirty bit avoids writing clean pages.

Goal: lowest page fault rate. Algorithms are compared using a reference string. We use this one with 3 frames:

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1

FIFO Page Replacement

Evict the page that has been in memory the longest.

Result: 15 page faults. Simple, but may evict heavily used pages.

Optimal Page Replacement

Evict the page that will not be used for the longest time in the future (OPT / MIN).

Result: 9 page faults, the minimum possible. Not implementable (needs the future); used as a benchmark.

LRU

Least Recently Used: evict the page that has not been used for the longest time. It approximates OPT by looking at the past.

Result: 12 page faults.

Implementation:

  • Counters: timestamp each access, evict the smallest. Searching is slow.
  • Stack / doubly linked list + hash map: move a page to the top on access, evict from the bottom. This is also the classic LRU cache interview problem, O(1) per operation.

Exact LRU in hardware is too expensive, so OSes approximate it.

Clock Algorithm

The Clock (second chance) algorithm approximates LRU using the reference bit:

  • Frames are arranged in a circle with a pointer (the clock hand).
  • On replacement: if the page's reference bit is 1, clear it and move on (second chance). If 0, evict it.

Enhanced versions consider both reference and dirty bits, preferring clean, unreferenced pages.

Other algorithms

  • LFU (least frequently used) and MFU (most frequently used): counting based, rarely used alone.

Belady's Anomaly

Belady's anomaly: with FIFO, adding more frames can increase the number of page faults.

Reference string 1 2 3 4 1 2 5 1 2 3 4 5:

  • 3 frames: 9 faults
  • 4 frames: 10 faults

Stack algorithms (LRU, Optimal) never suffer from it, because the set of pages in memory with n frames is always a subset of the set with n + 1 frames.

Frame allocation

  • Equal allocation: each process gets the same number of frames.
  • Proportional allocation: frames proportional to process size.
  • Global replacement: a victim can be any process's frame (better throughput, less predictable).
  • Local replacement: only from the faulting process's own frames.

Thrashing

Thrashing happens when a process does not have enough frames for its active pages, so it page faults constantly and spends more time swapping than executing.

The vicious cycle: CPU utilization drops, the OS thinks it should increase multiprogramming, adds more processes, they steal frames, more faults, utilization drops further.

CPU utilization
  ^        ___
  |      /     \
  |    /         \   <- thrashing
  |  /             \
  +------------------> degree of multiprogramming

Fixes: reduce the degree of multiprogramming (suspend processes), use local replacement, and give each process its working set.

Working Set

The working set of a process is the set of pages referenced in the most recent D memory references (the working set window).

  • If the sum of all working set sizes exceeds available frames, suspend a process.
  • Page fault frequency (PFF): a simpler control. If a process's fault rate is above an upper bound, give it more frames; below a lower bound, take frames away.

Quick revision

  • Virtual memory = demand paging + page replacement.
  • Page fault: trap, find frame, read from disk, update table, restart instruction.
  • On the classic string with 3 frames: FIFO 15, LRU 12, Optimal 9.
  • Belady's anomaly affects FIFO, not LRU/Optimal.
  • Thrashing = too little memory per process; fix with working set or PFF.