Menu

Earn Premium with Referrals

Invite your friends and earn Premium rewards through our referral program.

See how it works and start inviting friends.

Virtual Memory & Thrashing
OS

Virtual Memory & Thrashing

Run programs larger than your RAM using Virtual Memory and understand the dreaded 'Thrashing' phenomenon.

Virtual memory decouples the logical address space a process sees from the physical RAM installed in the machine. Each process gets its own contiguous virtual address space (e.g., 4GB on 32-bit systems), even if physical RAM is only 1GB. The OS maps virtual pages to physical frames on demand.

Why Virtual Memory

  • Run larger programs: a 3GB executable can run on a 1GB machine
  • Isolation: each process has its own address space — cannot see or corrupt other processes’ memory
  • Efficiency: only the actively used parts of a program are in RAM
  • Simplified linking: all programs link at the same virtual addresses

Demand Paging

Pages are loaded into RAM only when needed — not upfront. When a process accesses a virtual address whose page is not in RAM, a page fault occurs.

Page Fault Handling

  1. Hardware detects invalid page → traps to OS
  2. OS checks if the address is valid (vs segmentation fault)
  3. OS finds a free frame (or evicts a page)
  4. OS initiates disk I/O to read the page into the frame
  5. OS updates the page table entry
  6. OS restarts the faulting instruction

Page faults are expensive — disk I/O takes millions of CPU cycles. The goal of virtual memory management is to minimize page faults.

Thrashing

Thrashing occurs when a process spends more time paging (handling page faults) than executing. The disk is constantly busy, but no useful work gets done.

Cause: the sum of the working sets of all processes exceeds physical memory. Each process doesn’t have enough frames to hold its active pages, so it constantly faults.

Solution: reduce the degree of multiprogramming — swap out entire processes to free frames. This actually increases CPU utilization because the remaining processes stop thrashing.

Working Set Model

The working set is the set of pages a process is currently using (bounded by the locality of reference). If the OS keeps the sum of all working sets ≤ available frames, thrashing is avoided.

ConceptMeaning
Temporal localityRecently accessed pages will be accessed again soon
Spatial localityPages near recently accessed pages will be accessed
Working setPages in the current locality

Q: Why do we need virtual memory?

A: (1) Run programs larger than physical RAM. (2) Process isolation — one process can’t corrupt another. (3) Efficient memory use — only active pages are in RAM. (4) Simplified programming — no need to manage physical memory layout.

Q: What happens during a page fault?

A: The MMU triggers a trap. The OS validates the address, finds a free frame (or evicts one), reads the page from disk into the frame, updates the page table, and restarts the instruction. A page fault takes millions of cycles — disk I/O is orders of magnitude slower than RAM.

Q: What is thrashing and how is it solved?

A: Thrashing is when a system spends most of its time paging rather than executing. Caused by too many processes competing for too few frames. Solved by reducing the degree of multiprogramming — swapping out processes until the remaining ones fit comfortably in memory.

Q: What is demand paging?

A: A virtual memory strategy where pages are loaded into memory only when accessed (faulted in). This minimizes startup time and wastes no RAM on code paths that are never executed.

My Private Notes

Notes are auto-saved locally to this device.