Demand Paging
Demand paging is a key concept in modern memory management that makes virtual memory systems efficient and scalable. It allows programs to run without loading the entire process into memory at once. Instead, only the parts (pages) that are actually needed are brought into physical memory on demand—hence the name.
What Is Demand Paging?
Demand paging is a technique where pages of a program are loaded into RAM only when they are required during execution.
Â
- At the start, most pages are not loaded into memory.
- When a program accesses a page that is not in memory, a page fault occurs.
- The operating system then loads that page from secondary storage (usually a hard disk or SSD) into physical memory.
- The program is resumed once the required page is available.
Â
This approach conserves memory and allows large programs to run even with limited RAM.
How Demand Paging Works
- A program starts running with only a small number of pages in memory.
- When it tries to access a missing page, the Memory Management Unit (MMU) triggers a page fault.
- The operating system pauses the program and identifies the location of the page on disk.
- It loads the page into RAM, possibly removing another page if memory is full.
- The page table is updated with the new frame location.
- The instruction that caused the fault is retried, and the program continues.
Page Fault Handling
A page fault is not an error—it’s an expected event in demand paging. However, frequent page faults slow down the system. This phenomenon is called thrashing.
Â
To reduce page faults, operating systems use:
Â
- Page replacement algorithms (e.g., LRU, FIFO)
- Working set models (keeping most-used pages in memory)
Benefits of Demand Paging
Benefit | Explanation |
---|---|
Efficient memory use | Loads only the pages that are needed, reducing memory waste. |
Faster startup | Programs can begin execution without waiting for the full load. |
Runs larger programs | Virtual memory allows programs larger than RAM size to run. |
Improved multitasking | Frees memory for multiple active processes by loading only active pages. |
Challenges in Demand Paging
Challenge | Description |
---|---|
Page fault overhead | Handling page faults adds execution delay. |
Thrashing | Too many page faults can cause the system to stall. |
Disk I/O dependency | Loading from disk is much slower than RAM access. |
Complex OS management | Requires sophisticated tracking and replacement strategies. |
Demand Paging vs. Pre-paging
Feature | Demand Paging | Pre-paging |
---|---|---|
When pages are loaded | Only when accessed | Loaded in advance, based on prediction |
Memory usage | Lower initially | Higher at start |
Performance | Can be slower due to initial page faults | Can be faster if predictions are accurate |
Risk | Page faults if not predicted correctly | Wasted memory if unused pages are loaded |