Concepts and theorems · Intermediate

Demand paging: provide a page when it is needed

Separate virtual allocation from page preparation and distinguish anonymous, file-backed and invalid accesses.

Ask Logeia about this articleTry the three questionsReview your answers

Updated

On this page
  1. Allocation is not residency
  2. Trace a sparse first touch
  3. Identify the kind of backing
  4. Account for first-access latency
  5. Ask Logeia about this articleCheck your understanding

Allocation is not residency

Demand paging prepares a page in response to an access that needs it, rather than necessarily preparing every page when a virtual range is created. A large mapping can therefore exist without every backing page being resident. This postpones work for unused ranges but moves some cost into first access. A page fault can transfer control to the operating system to resolve that access. The existence of a fault alone does not tell you whether storage I/O occurred, whether a new anonymous page was allocated, or whether the access was valid in the first place.

Trace a sparse first touch

Assume 4 KiB pages and a 1 MiB anonymous mapping. There are 256 page-sized positions in the region. A loop that writes one byte on each page can require preparation at each first write under a demand-allocation policy. Repeatedly writing only the first page touches a much smaller footprint despite the same virtual allocation size. This is a simplified calculation of possible work, not a promise of exactly 256 faults on every machine. Huge pages, prefaulting, shared zero pages and implementation choices can change the observed sequence and counters.

Identify the kind of backing

A valid anonymous write may require allocation and initialization of a new page. A file-backed access depends on whether the needed data is already in the page cache or must be fetched from storage. A fault resolved using existing resident data has a different cost profile from one that waits for I/O. An invalid address or disallowed permission is another case entirely: the operating system may report an error rather than supply a new page. Always classify the mapping and permission before describing a fault as useful allocation or as an application failure.

Account for first-access latency

If first-access latency matters, a program can consider touching the required range before the critical phase. That shifts work earlier rather than making it free, and reclaimed pages may need preparation again. Touching the whole mapping can waste memory and initialization time when much of it is never used. Separate allocation, first pass and later passes when measuring. Keep input and placement conditions comparable. Reducing a fault counter is useful only in relation to the latency, footprint and failure behaviour the application actually requires; it is not a sufficient performance goal by itself.

Reading and practice are recorded separately in this browser, for each language.

Learn this in order

Build your CS foundations · STEP 16

Understand the systems behind the web · STEP 6

Prepare for AI systems · STEP 6

Choose a learning path