Virtual Memory, Demand Paging & Performance of Demand Paging
1. What is Virtual Memory?
Have you ever wondered how your laptop with only 8 GB of physical RAM can smoothly run a massive 60 GB video game alongside Chrome and VS Code? The secret is Virtual Memory!
Observation: In real programs, an entire program is rarely needed in RAM all at once! (For example, error-handling code, rare menu options, and unused arrays may never even be executed during a run).
Definition of Virtual Memory: Virtual Memory is a memory management technique that separates user logical memory from physical RAM, allowing the execution of processes that are not completely in Main Memory (RAM).
- Only the currently needed pages of a program are kept in Physical RAM, while the rest of the program stays on the Hard Disk / SSD (Backing Store / Swap Space) and is swapped in automatically when needed!
Major Benefit: Programs can be much larger than total physical RAM, and many more programs can run simultaneously (higher Degree of Multiprogramming)!
2. Demand Paging (How Virtual Memory is Implemented)
Virtual Memory is most commonly implemented using Demand Paging.
What is Demand Paging? Instead of loading all pages of a process into RAM when the program starts, a Demand Paging system uses a “Lazy Swapper” (Pager):
- Golden Rule of Demand Paging: Never load a page into Main Memory (RAM) unless the CPU actually demands (references) it during execution!
The Valid-Invalid Bit in the Page Table:
How does the MMU hardware know which pages of a process are currently in RAM and which pages are still sitting on the Hard Disk? Every entry in the Page Table has an extra hardware bit called the Valid-Invalid Bit:
v(Valid =1): The page is legal AND currently in Main Memory (RAM).i(Invalid =0): The page is either not valid or is currently sitting on the Hard Disk (Backing Store)!
3. What is a Page Fault & How is it Handled? (6 Steps)
When the CPU tries to access a page whose Page Table entry is marked i (Invalid — not currently in RAM), the MMU hardware triggers a trap to the Operating System called a Page Fault!
(Note: A “Page Fault” is not an error—it is simply a signal to the OS saying: “Hey OS, the CPU needs Page X, please fetch it from the Hard Disk into RAM!”)
i (Invalid)i → v (Valid)What is Pure Demand Paging?
In Pure Demand Paging, the OS starts executing a process with ZERO pages loaded in RAM! As soon as the CPU tries to fetch the very first instruction, an immediate Page Fault occurs to bring in Page 0, and subsequent pages are brought in strictly on demand.
4. Performance of Demand Paging (Effective Access Time — EAT)
Your DAVV Unit 4 syllabus explicitly mentions “Performance of Demand Paging”! Let us derive the formula and solve a numerical example.
- Let ma = Normal Memory Access Time of RAM (typically
100 nsto200 ns). - Let p = Page Fault Rate (0 ≤ p ≤ 1, the probability that a memory access causes a Page Fault):
- If p = 0: No page faults occur at all.
- If p = 1: Every single access is a page fault.
- If no page fault occurs (probability 1 - p), the access takes just ma.
- If a page fault occurs (probability p), the OS must read the page from the slow Hard Disk (Page Fault Service Time, typically
8 ms = 8,000,000 ns!).
Formula for Effective Access Time (EAT):
Formula: Effective Access Time (EAT) = (1 − p) × Memory Access Time (ma) + p × Page Fault Service Time
Solved Numerical Example:
Suppose normal RAM access time ma = 200 ns, and average Page Fault Service Time = 8 ms = 8,000,000 ns. If the Page Fault Rate is 1 out of every 1,000 accesses (p = 0.001), find the Effective Access Time:
Formula: EAT = (1 - 0.001) × 200 ns + (0.001) × 8,000,000 ns Formula: EAT = 199.8 ns + 8,000 ns = 8,199.8 ns ≈ 8.2\ μs
- Critical Conclusion on Demand Paging Performance:
Even a tiny page fault rate of 1 in 1,000 (p=0.001) slowed down memory by 40 times (from
200 nsto8,200 ns)! Therefore, to keep Demand Paging fast (less than 10% slowdown), the Page Fault Rate (p) MUST be kept extremely close to zero using good Page Replacement Algorithms and Locality of Reference!
5. Thrashing (When Demand Paging Collapses)
What is Thrashing?
If a process does not have enough RAM frames allocated to hold the active pages it is currently using (its Working Set), it will page-fault every few instructions. To bring in the needed page, it replaces another page that it will immediately need again!
Definition of Thrashing: A system is Thrashing when a process is spending more time paging (swapping pages in and out of disk) than actually executing instructions on the CPU!
- During Thrashing, Disk I/O goes to
100%while CPU Utilization plunges to near0%, making the computer freeze completely. - Solution to Thrashing: Reduce the Degree of Multiprogramming (suspend/swap out some processes) or give each active process enough frames using the Working-Set Model.
💡 Exam Preparation Tips for DAVV BCA
- Always write the EAT (Effective Access Time) formula in a box when answering any question on Demand Paging or Performance of Demand Paging!
- Memorize the 6 steps of Page Fault Handling (Check
ibit → Trap to OS → Find on Disk → Load into Free Frame → Update Page Table tov→ Restart Instruction).