11.1 Virtual Memory, Pages, and Page Faults
Every process sees one large private range of addresses. Most of that range maps to nothing. The parts in use map to physical pages scattered through RAM, and some of those pages may sit on disk in swap. The operating system keeps this mapping between the addresses a program uses and the physical bytes behind them, and calls it virtual memory. The mapping explains why touching a bad address crashes a program, and how copy-on-write lets a forked child share its parent’s memory.
11.1.1 Mental model
Physical addresses name actual bytes in RAM. If every process used them directly, two programs built to use the same addresses would collide, and a bug in one program could overwrite another program’s memory.
The kernel solves this with virtual addressing. Each process has its own address space. Virtual address 0x1000 in one process and virtual address 0x1000 in another are unrelated. For each process the kernel keeps a page table that maps the process’s virtual addresses to physical ones. Two processes can map the same physical page, which is how shared libraries and copy-on-write work, but only where the kernel sets that up.
Virtual addresses give isolation. Process A and process B both use address 0x1000, the two addresses map to different physical bytes, and neither process has any address that reaches the other’s memory.
Each process’s page table maps its virtual addresses to its own physical pages, so the same address 0x1000 reaches different bytes in A and in B.
11.1.2 Mechanism
Pages and the page table
Virtual memory divides into fixed size blocks called pages, typically 4096 bytes on x86-64. Physical memory also divides into pages. The page table maps virtual page numbers to physical page numbers. A virtual address splits into two parts: the high bits name the page, the low bits name the offset within the page.
The page table is a data structure the kernel maintains for each process. On modern CPUs, the page table is too large to fit in registers. The CPU has a small cache called the TLB, the Translation Lookaside Buffer. The TLB holds recent translations. On a memory access, the CPU first checks the TLB. If the translation is cached, the access is fast. If the TLB misses, the CPU walks the page tables, reading one entry from each level of the table. Each of those reads is a memory access, so a walk costs tens of cycles when the entries are in cache and more when they are not.
Virtual memory uses indirection. Every memory access becomes a two-step process. First, translate the virtual address to a physical address. Second, read or write the physical location. The indirection costs are hidden by the TLB and by caches, but they are real.
The page fault
When the CPU touches a virtual address that has no valid mapping, or writes to a page mapped read-only, the hardware raises an exception: a page fault. The kernel’s page fault handler runs and decides what to do. If the address lies in a region the process has mapped, the kernel can allocate a physical page and add an entry to the page table. If it does not, the kernel sends the process a segmentation fault signal, SIGSEGV.
Page faults are not always errors. The kernel uses page faults to implement features like copy-on-write, demand paging, and memory protection.
Resident versus virtual memory
Virtual memory allows a process to use more memory than exists in physical RAM. The kernel swaps pages to disk. When code accesses a page that is on disk, the page fault handler loads it back into RAM, possibly evicting another page to disk. The resident set is the memory currently in physical RAM. The virtual size is everything the process has mapped. The resident set is almost always the smaller of the two, even with no swapping: pages the process reserved but never touched, and parts of mapped files it never read, count toward the virtual size and not the resident set.
The resident set size (RSS) counts a shared page in full in every process that maps it. Adding up the RSS of forked workers therefore overstates their total. Linux also reports a proportional set size (PSS), which divides each shared page among the processes sharing it.
In modern systems with plenty of RAM, swapping is rare. The OS still tracks resident sets to measure memory pressure across processes.
Copy-on-write
Fork uses page faults to avoid copying memory. When fork creates a child, the kernel does not copy the parent’s pages. It copies the page tables instead, so parent and child point to the same physical pages, and it marks the private writable pages read-only in both processes.
When either process writes to a page, the hardware raises a write fault. The page fault handler recognizes the copy-on-write situation. It allocates a new physical page, copies the old page to the new page, and remaps the writer to the new page. The copy happens lazily, only when needed.
Pages that neither process writes stay shared for as long as both processes live. A forked child that only reads a large data structure shares its pages with the parent, and the memory savings can be large.
sunlight measured much less sharing than that. In its Day 7 benchmark (2026-08-01), the summed PSS of workers forked from a preloaded template was 1.28 times lower than the summed RSS of the best separately started pool at 8 workers, and 1.10 times lower at 32 workers. That was one run, on WSL2, and crew was left out because it did not start all its workers. The experiment page traces the gap to garbage collection in the child, which writes to object headers and so takes a copy-on-write fault on every page it touches. See fork, copy-on-write, and the first GC.
11.1.3 Consequences for R
A full garbage collection in R writes to the header of every old-generation object: it clears each object’s mark bit and sets it again on every object still reachable (src/main/memory.c). In a forked child, each of those writes to a shared page takes a copy-on-write fault, so the child’s first full collection privatizes every page that holds an object header.
R also writes object headers while R code only reads data. Binding a value to a name, or passing it as an argument, can update its reference count, and the reference count lives in the header.
How much this costs depends on the layout. A large numeric vector keeps its data in the same allocation, straight after its header, so marking it dirties one page and the rest of the vector stays shared until something writes to it. Package code is the opposite case: closures, environments, pairlists and symbols are many small objects packed close together, so a collection dirties nearly every page they occupy. Thirty-two workers forked from a template with heavy packages loaded each end up holding a private copy of most of that package memory in RAM.
11.1.4 Tradeoffs
Copy-on-write fork saves memory only if the child writes little. If the child writes every page, it pays for a full copy after all, plus a page fault and a TLB update for each page.
If the child only reads, the shared pages cost nothing extra. Some memory stays shared whatever the child does: the machine code of R and of compiled package libraries is mapped read-only from the same files in every process.
11.1.5 Other systems
Modern operating systems all use virtual memory. Linux, Windows, macOS, and BSD all maintain page tables and handle page faults.
Copy-on-write is a standard technique. Databases use it for snapshots, filesystems such as Btrfs and ZFS for storage, and hypervisors for disk snapshots and cloned virtual machines. The idea is the same each time: share until someone writes, then copy only what was written.
The Go runtime leans on demand paging. Its allocator reserves address space in large arenas, and the kernel supplies a physical page only when the program first touches it.
Rust leaves these decisions to the OS. No Rust-specific virtual memory tricks exist. Rust code runs in the same virtual memory system as C code.
11.1.6 R’s approach
GNU R gets its heap from malloc, and its heap code makes no virtual memory calls of its own: src/main/memory.c calls neither mmap nor madvise. Copy-on-write after fork is entirely the kernel’s work.
sunlight relies on fork and copy-on-write to inherit a loaded template, and for the reasons above its forked workers keep little of that memory shared.
Further reading
- Remzi H. Arpaci-Dusseau and Andrea C. Arpaci-Dusseau, Operating Systems: Three Easy Pieces <https://pages.cs.wisc.edu/~remzi/OSTEP/>. Free online. See the chapters on address spaces, paging and TLBs.
- Linux kernel documentation, Concepts overview <https://docs.kernel.org/admin-guide/mm/concepts.html>, in the memory management admin guide.
fork(2)manual page <https://man7.org/linux/man-pages/man2/fork.2.html>.proc_pid_smaps(5)manual page <https://man7.org/linux/man-pages/man5/proc_pid_smaps.5.html>, which defines the Rss and Pss fields.- Linux kernel documentation, Transparent Hugepage Support <https://docs.kernel.org/admin-guide/mm/transhuge.html>.
Where this appears
- sunlight Forked workers lose most of their copy-on-write sharing when a full garbage collection in the child writes mark bits. This page covers the paging mechanism behind that.