Virtual Memory & Demand Paging in OS: Page Faults, Performance & Examples
Virtual Memory is one of those Operating System topics that sounds more complicated than it really is. Students often think that virtual memory means extra physical RAM. It does not.
Virtual memory creates the illusion that a process has a large, continuous memory space even when only some of its pages are currently present in physical RAM.
Demand Paging takes this idea one step further: a page is brought into main memory only when the process actually needs it.
Virtual Memory is a memory-management technique that allows a process to use a virtual address space that can be larger than the currently available physical memory. Only the required pages need to remain in RAM. Other pages may remain on secondary storage until needed.
Demand Paging loads a page only when that page is referenced. If the required page is not currently present in RAM, a Page Fault occurs. The operating system then loads the page into a free frame, updates the page table and restarts the interrupted instruction.
Why Does Virtual Memory Feel Difficult?
Virtual Memory becomes confusing mainly because students try to imagine that the entire program must always remain inside RAM.
Demand paging works differently. Only the pages that are currently needed may be kept in physical memory.
Virtual Memory = Large logical view
RAM = Pages currently needed
Secondary Storage = Pages not currently resident
Demand Paging = Load page only when demanded
What Is Virtual Memory in Operating System?
A process works with addresses generated inside its own logical or virtual address space.
These addresses do not directly mean that every byte of the process must already exist in physical RAM.
The operating system and memory-management hardware translate virtual addresses into physical memory addresses.
Virtual Memory is a memory-management technique that gives a process a virtual address space and allows only the required parts of that process to remain in physical memory at a given time.
The Big Idea
A program may contain many pages. But the program may currently be using only a small number of them.
Instead of loading everything immediately, the operating system can keep only the active pages in RAM.
Virtual Address Space vs Physical RAM
Why Do We Need Virtual Memory?
Where Are Non-Resident Pages Kept?
Pages that are not currently in RAM may have backing data on secondary storage.
When such a page is needed, the operating system can bring it into a physical frame.
Virtual Pages
Active Pages
Backing Data
What Is Demand Paging?
Demand Paging follows a simple rule:
Suppose a process has 20 pages. The operating system does not necessarily need to load all 20 pages before execution begins.
It may load only the pages that the process currently references.
What Is Pure Demand Paging?
In the strict idea of pure demand paging, a page is brought into memory only after the process actually attempts to reference it.
Do not load it.
Page referenced?
Bring it into memory if necessary.
How Does the System Know Whether a Page Is in RAM?
A page-table entry can contain status information indicating whether the required mapping is currently resident and usable.
How to Read This
Page 0 is currently stored in Frame 2.
Page 3 is currently stored in Frame 0.
Page 1 and Page 2 are valid process pages in this example, but they are not currently resident in RAM.
What Is a Page Fault?
Suppose the CPU generates an address belonging to Page 2.
The page table indicates that Page 2 is not currently present in physical memory.
The processor cannot complete the memory access normally.
A Page Fault is an exception that occurs when a process references a virtual-memory page that is not currently resident in physical memory and the operating system must handle the situation.
Valid Page Fault vs Invalid Memory Reference
When a page fault occurs, the operating system must determine whether the requested virtual address is actually valid for the process.
Valid Reference
The page belongs to the process but is currently not resident.
The operating system can load the required page and continue execution.
Invalid Reference
The process is trying to access an address that is not part of its valid address space or violates protection.
The operating system should not treat this as a normal page-in operation.
How Does the Operating System Handle a Page Fault?
From Missing Page to Restarted Instruction
What If No Free Frame Is Available?
If a free frame exists, the operating system can use it for the incoming page.
But physical memory may already be full.
In that situation, the operating system may need to choose an existing page as a victim page and reuse its frame.
What If the Victim Page Was Modified?
A page that has been modified while in RAM may need to be written back to its backing storage before its frame can be reused, depending on the page and system design.
A page-table entry may contain a Dirty or Modified Bit that helps indicate whether the page has changed.
Why Are Page Faults Expensive?
A normal RAM access is extremely fast compared with loading data from secondary storage.
Therefore, demand paging performs well only when page faults remain relatively infrequent.
Effective Access Time in Demand Paging
Demand-paging performance is commonly studied using Effective Access Time.
Let:
- p = Page-fault probability
- m = Normal memory-access time
- F = Time for a page-fault access path
Page Fault Performance Solved Example
Question
Suppose:
Step 1: Convert Milliseconds to Nanoseconds
Step 2: Apply the Formula
Step 3: Calculate
Effective Access Time ≈ 180 ns
Why Even a Small Page-Fault Rate Matters
Look at the previous example carefully.
The page-fault probability was only 0.00001.
That means page faults were very rare.
Even then, the average access time increased from about 100 ns to about 180 ns because a page fault is extremely expensive compared with RAM access.
Demand Paging Performance Calculator
Use this calculator to see how page-fault probability affects Effective Access Time.
Locality of Reference and Demand Paging
Demand paging works well because programs normally do not access every page equally at every moment.
Programs often repeatedly access a relatively small group of instructions and data for some period of time.
This behavior is called Locality of Reference.
In this simple illustration, the process is currently working mainly with Pages 2, 3 and 4.
Temporal and Spatial Locality
Temporal Locality
If a memory location has been used recently, it may be used again soon.
Example: instructions inside a loop.
Spatial Locality
If one memory location is accessed, nearby locations may also be accessed soon.
Example: reading sequential elements of an array.
Basic Idea of a Working Set
At a particular time, a process may be actively using only a subset of its pages.
This frequently used group is commonly discussed using the idea of a Working Set.
If enough of these important pages remain in memory, the process can execute with relatively few page faults.
What Is Thrashing?
Demand paging becomes harmful when the system spends too much time moving pages between storage and RAM instead of doing useful CPU work.
Thrashing is a condition in which a system experiences excessive paging activity and spends a large amount of time servicing page faults rather than executing useful process instructions.
How Can Thrashing Happen?
- Too many processes compete for a limited number of frames.
- A process receives too few frames for its active pages.
- The process generates frequent page faults.
- Pages are repeatedly loaded and removed.
- Storage activity increases.
- Useful CPU progress may fall.
Page Fault Rate and System Performance
| Page Fault Rate | What It Means | Possible Performance |
|---|---|---|
| Very Low | Most required pages are already resident | Good demand-paging performance |
| Moderate | Pages are occasionally fetched | Additional overhead becomes visible |
| Very High | Frequent page movement | Severe slowdown and possible thrashing |
Advantages and Disadvantages of Demand Paging
Advantages
- Pages are loaded only when needed.
- Unused pages do not immediately occupy RAM.
- More processes may share physical memory.
- Large virtual address spaces are possible.
- Program startup may avoid loading every page immediately.
- Works naturally with virtual-memory systems.
Disadvantages
- Page faults are expensive.
- Secondary-storage I/O can be slow.
- Page tables require management.
- Page replacement may be necessary.
- Too many page faults can cause thrashing.
- Hardware and OS support are required.
Virtual Memory vs Physical Memory
| Basis | Virtual Memory | Physical Memory |
|---|---|---|
| Meaning | Address-space abstraction seen by processes | Actual RAM hardware |
| Address | Virtual or logical address | Physical address |
| Size | Can exceed the amount of currently available RAM | Limited by installed physical memory |
| Translation | Requires virtual-to-physical mapping | Represents actual memory locations |
| Demand Paging | Pages can be loaded when needed | Holds currently resident pages |
Paging vs Demand Paging
| Basis | Paging | Demand Paging |
|---|---|---|
| Main Idea | Divide logical memory into pages and physical memory into frames | Load a page into RAM only when required |
| Focus | Address translation and non-contiguous allocation | On-demand page loading |
| Page Fault | Paging alone explains mapping | Page faults are central to on-demand loading |
| Secondary Storage | Not the main concept in basic paging explanation | Non-resident page data may need to be fetched from storage |
Simple Demand Paging Example
Process Pages
Suppose RAM currently contains:
CPU Requests Page 3
Page 3 is already present.
CPU Requests Page 1
Page 1 is a valid page of the process, but it is not currently resident.
Common Student Confusions
Is Virtual Memory the Same as RAM?
No.
RAM is physical memory hardware. Virtual memory is an address-space and memory-management abstraction.
Does a Page Fault Mean the Program Is Wrong?
Not necessarily.
In demand paging, a valid page may simply not be resident yet.
Does Every Page Fault Require Page Replacement?
No.
If a free physical frame is available, the operating system can use that frame. Replacement becomes necessary when a suitable free frame is not available.
Does Demand Paging Load the Complete Process?
No.
The key idea is to load pages when they are required.
Is a Higher Degree of Multiprogramming Always Better?
No.
If too many processes compete for too few frames, page-fault activity can increase and lead to thrashing.
Does Virtual Memory Make Storage as Fast as RAM?
No.
Storage-backed page transfers are much slower than normal RAM accesses.
One-Minute Revision
Virtual Memory and Demand Paging Exam Preparation
2-Mark Virtual Memory Definition
Virtual Memory is a memory-management technique that provides a process with a virtual address space and allows only the required parts of the process to be present in physical memory at a time.
2-Mark Demand Paging Definition
Demand Paging is a virtual-memory technique in which a page is loaded into physical memory only when it is actually referenced.
Page Fault Short Answer
A page fault occurs when a process references a virtual page that is not currently resident in physical memory and the operating system must handle the missing-page condition.
Page Fault Handling Steps
- Detect the page fault.
- Check whether the reference is valid.
- Find a free frame or select a victim page.
- Load the required page.
- Update page-table information.
- Restart the interrupted instruction.
Effective Access Time Formula
For a 5-Mark Question
- Define Virtual Memory.
- Explain Demand Paging.
- Define Page Fault.
- Explain Page Fault Handling.
- Write advantages and limitations.
For a 10-Mark Question
- Meaning of Virtual Memory.
- Need for Virtual Memory.
- Virtual Address Space and Physical Memory.
- Demand Paging.
- Page-table resident information.
- Page Fault.
- Page Fault Handling Steps.
- Free Frame and Page Replacement.
- Dirty Page Concept.
- Demand-Paging Performance.
- Effective Access Time.
- Locality of Reference.
- Working Set.
- Thrashing.
- Advantages and Disadvantages.
Frequently Asked Questions
What is Virtual Memory in Operating System?
Virtual Memory is a memory-management technique that gives a process a virtual address space and allows only required pages to be present in physical memory at a given time.
What is Demand Paging?
Demand Paging is a technique in which a page is brought into physical memory only when the process actually references that page.
What is a Page Fault?
A Page Fault occurs when a process references a virtual page that is not currently resident in physical memory and the operating system must handle the condition.
What Happens After a Page Fault?
The operating system checks the memory reference, obtains a frame, loads the required page if the reference is valid, updates the mapping and restarts the interrupted instruction.
Does Every Page Fault Require Page Replacement?
No. If a free frame is available, the required page can be loaded into that frame. Page replacement is needed when a usable free frame is not available.
Why Is a Page Fault Slow?
A page fault may require operating-system processing and secondary storage access, which is much slower than a normal RAM access.
What is Effective Access Time in Demand Paging?
Effective Access Time is the average memory-access time after considering both normal memory accesses and the probability and cost of page faults.
What is Locality of Reference?
Locality of Reference is the tendency of programs to repeatedly access a relatively small group of memory locations or nearby locations during a period of execution.
What is Thrashing?
Thrashing is a condition in which excessive page faults cause the system to spend a large amount of time transferring pages instead of performing useful computation.
What is the Difference Between Paging and Demand Paging?
Paging divides logical memory into pages and physical memory into frames, while Demand Paging adds the rule that pages are brought into physical memory when they are actually needed.
Conclusion
Virtual Memory allows a process to work with a virtual address space without requiring every page of the process to remain in physical RAM at the same time.
Demand Paging improves memory use by loading a page only when it is actually required.
When a valid required page is not currently resident, a Page Fault occurs. The operating system then checks the reference, finds or creates an available frame, loads the page, updates the page table and restarts execution.
The idea is powerful, but page faults are expensive because storage operations are much slower than normal RAM accesses.
Demand paging therefore works best when programs show good Locality of Reference and when their actively used pages remain in memory.
If the system produces too many page faults, it can enter a condition called Thrashing, where paging activity dominates useful processing.
Virtual Memory = Large Virtual Address Space
Demand Paging = Load When Needed
Page Present = Normal Access
Page Missing = Page Fault
Free Frame = Load Page
No Free Frame = Page Replacement May Be Needed
Low Page Fault Rate = Better Performance
Too Many Page Faults = Thrashing
