
Understand it
Virtual memory maps a process’s addresses to backing storage and physical pages under the operating system’s rules. Reservation, commitment and residency differ. Lazy allocation and shared file pages are common mechanisms, not guarantees of every Rust allocation or a fixed physical cost.
A process uses virtual addresses. The operating system and hardware map regions of that address space to physical memory or backing storage as needed, with page tables mediating access. Reserving an address range is not the same as filling every page with committed physical memory. Many systems map pages lazily, so the first access can trigger a page fault that the kernel resolves. But allocation behavior varies by allocator, operating system, and mapping flags; do not promise that every large allocation is instant.
This model explains several useful observations. Resident set size counts memory currently resident, while virtual size can include reserved or mapped regions not resident. A shared library may be mapped into several processes without every page being physically duplicated. Copy-on-write lets processes share pages until one writes and needs a private copy. A page fault is not always an error: it can be normal lazy materialization. An invalid access, however, may produce a protection fault.
For an interview, walk through one address from a program pointer to a page-table translation and a physical frame. Then ask what happens on the first read, a write to a copy-on-write page, and an access outside a valid mapping. Distinguish address-space reservation, committed memory, and actual residency. This matters for profiling memory: a process can have a huge virtual range and modest physical use, or many small allocations that fragment memory and hurt locality.
A mapped file can expose bytes through virtual memory without copying all of them into a separate user buffer at once, but access can still fault and I/O can still occur. Mapping is not a free read. Mention protection bits too: a virtual page can be readable, writable, executable, or inaccessible according to mapping policy.
Interview follow-up: Contrast virtual address reservation, resident pages and private copy-on-write pages. Which measurement answers your memory-budget question?
Read the code
// Conceptual OS-level sequence; no allocation is performed here.
// 1. Reserve or map a virtual address range.
// 2. Access pages under the mapping’s protection rules.
// 3. Inspect resident/private/shared memory on that OS.
// File-backed pages may be shared; mappings still have overhead.This is a conceptual mapping trace, not a promise that Vec::with_capacity reserves virtual pages lazily.
Practice starter
// OS-level scenario, not a portable Rust guarantee.
// Two read-only mappings reference the same file.
// Residency, mapping flags and allocator behaviour are not specified.Try it
Explain why a huge allocation can succeed and the first write still stall.
What success looks like: You mention pages, faults, and the difference between virtual and resident.
Check your understanding
What can be concluded from mapping one file read-only into two processes on an OS with shared file-backed pages?
Reveal the answer
- Each mapping must duplicate every file byte in physical RAM immediately.
- The OS may share resident file pages, while page tables and other costs remain per mapping.
- The mappings guarantee exactly one resident copy of every byte at all times.
Mapping permits sharing; it does not fix RSS, residency, accounting, page-fault behaviour or overhead across operating systems.
Go deeper
Common misconception: The mappings guarantee exactly one resident copy of every byte at all times.
Sharing is permitted, not guaranteed. A page may be absent until first touched or evicted later, so some bytes may have no resident copy at all.
- Identify the contract: Every pointer you see is virtual, and the kernel decides what it means.
- Trace the concrete example: This is a conceptual mapping trace, not a promise that Vec::with_capacity reserves virtual pages lazily.
- Test the changed constraint: Contrast virtual address reservation, resident pages and private copy-on-write pages. Which measurement answers your memory-budget question?
Transfer challenge: Contrast virtual address reservation, resident pages and private copy-on-write pages. Which measurement answers your memory-budget question?
Connections
- Stack and Heap: Where values live and what a move copies
- Syscalls and Buffering: Where the cost of IO actually is
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Virtual Memory (courseId "interview", lessonId "q-virtual-memory") Link: https://crabrix.com/learn/interview/q-virtual-memory/ Goal: Virtual size, committed memory and residency are different measurements; inspect the actual platform. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=interview/q-virtual-memory) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.