# Virtual Memory and Demand Paging

> Virtual memory is a technique that lets a process run with an address space larger than the installed physical RAM by keeping only the pages in use in memory and the rest on disk. Demand paging loads each page only when it is actually accessed, through a page fault, rather than loading the whole process upfront.

Canonical: https://devsunite.com/resources/core-subjects/operating-systems/virtual-memory-and-demand-paging
Last updated: 2026-11-22

Subject: Operating Systems · Category: Memory Management · 3 min read

Virtual memory is the technique that lets a process run with an address space larger than the physical RAM actually installed on the machine, and lets multiple processes' total memory needs exceed physical RAM at the same time. It works hand in hand with demand paging: pages are only loaded into physical memory when they're actually accessed, not all at once when the process starts.

## The core idea

Instead of loading a process's entire memory image into RAM before it can run, the OS loads only what's immediately needed and keeps the rest on disk. When the process accesses a page that isn't currently in physical RAM, a page fault occurs (its own dedicated topic covers the full handling sequence), and the OS loads that specific page in from disk at that moment, on demand, rather than upfront.

## Why this works well in practice

Real programs exhibit locality of reference: at any given moment, a running program tends to access a relatively small, concentrated subset of its total memory (the code it's currently executing, the data it's currently working with), not its entire address space uniformly. Because of this, keeping only the actively-used pages in RAM and leaving the rest on disk usually satisfies almost all real memory accesses without triggering constant page faults, even though the process's total virtual address space might be far larger than physical RAM.

## A worked walkthrough

A process has a 100-page virtual address space, but only 10 physical frames are currently allocated to it. When the process starts, none of its pages are loaded yet, marked invalid in the page table. It begins executing, accessing page 0: page fault, OS loads page 0 from disk into a free frame, marks it valid, execution resumes. The process keeps running, mostly touching pages 0 through 9 in a tight loop (locality of reference in action), so those 10 pages, once loaded, satisfy the vast majority of its memory accesses with no further faults. Only when it eventually jumps to code or data in page 50 does another fault occur, loading that page in turn.

## Where this matters in real systems

Virtual memory with demand paging is universal in modern general-purpose operating systems. It's exactly why you can run several large applications simultaneously on a machine with, say, 16GB of RAM, even if their combined virtual memory demands exceed that, because at any moment only a fraction of each application's total memory is actually resident in physical RAM. Linux's swap space and Windows' page file are the disk-backed storage this scheme relies on when physical RAM genuinely can't hold everything currently in active use.

## What interviewers actually ask about virtual memory and demand paging

- **"What is virtual memory?"** A technique letting a process's address space, and the combined memory demand of all running processes, exceed the physical RAM actually installed, by keeping only actively-used pages in RAM.
- **"What is demand paging specifically?"** Loading a page into physical memory only when it's actually accessed and triggers a page fault, rather than loading a process's entire memory image upfront.
- **"Why does demand paging work well given how real programs behave?"** Because of locality of reference, programs tend to concentrate their memory accesses on a small, actively-used subset of their total address space at any given time.
- **"What happens when a process accesses a page that isn't currently in RAM?"** A page fault occurs, and the OS loads that specific page in from disk (the full sequence is covered in the page fault handling topic).

## Key takeaways

- Virtual memory lets processes and the system as a whole use more memory than physical RAM actually provides.
- Demand paging loads pages into RAM only when they're actually accessed, not all upfront.
- Locality of reference is why this works well in practice, most accesses concentrate on a small active subset of memory.
- Linux's swap space and Windows' page file are the real disk-backed storage this scheme depends on.

## Related

- [Previous: Segmentation in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/segmentation-in-os)
- [Next: Page Fault Handling](https://devsunite.com/resources/core-subjects/operating-systems/page-fault-handling)
- [More in Memory Management: Logical vs Physical Address Space](https://devsunite.com/resources/core-subjects/operating-systems/logical-vs-physical-address-space)
- [More in Memory Management: Swapping and Contiguous Memory Allocation](https://devsunite.com/resources/core-subjects/operating-systems/swapping-and-contiguous-memory-allocation)
- [More in Memory Management: Fragmentation in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/fragmentation-in-os)
- [More in Memory Management: Paging in Operating Systems](https://devsunite.com/resources/core-subjects/operating-systems/paging-in-os)
- [More in Memory Management: Page Replacement Algorithms](https://devsunite.com/resources/core-subjects/operating-systems/page-replacement-algorithms)
- [All 205 OS interview questions](https://devsunite.com/resources/core-subjects/operating-systems)
