What is virtual memory, and what is demand paging?
Interview preparation resource from Gate Smashers.
Virtual memory gives each process a private logical address space that is mapped to physical memory. Demand paging loads a page into RAM only when the process first tries to access it.

Virtual addresses let the OS relocate processes, isolate their memory and selectively share pages without requiring each program to know where physical RAM is located. Page tables store the mappings and access permissions used by the processor's memory-management unit.
With demand paging, executable and data pages can remain on storage until needed. An access to a valid but non-resident page causes a page fault; the OS loads the page, updates the page table and restarts the instruction. This reduces initial memory use but excessive faults can severely reduce performance.
Demand paging works because programs usually use only a portion of their address space at a given moment. The OS can therefore keep active pages in RAM and leave inactive pages on storage, giving each process the illusion of more memory while paying page-fault cost only when needed.
