16.1 Fork, Copy-on-Write, and the First GC

EXPERIMENTSSTATUS: STABLELEVEL: INTERMEDIATEREVIEWED: 2026-11-18
Before this page Garbage Collection

16.1.1 Question

Does fork’s copy-on-write save worker memory for R? A forked child shares its parent’s memory pages until it writes to one, and then the kernel copies that page. Workers forked from a template that has already loaded its packages should therefore share one copy of those packages. R’s garbage collector writes into every reachable object when it runs a full collection, though, and the question is how much sharing survives that.

16.1.2 Hypothesis

Workers forked from a prewarmed template would share one physical copy of the loaded packages, so a forked pool would use far less memory than the same number of separately started workers. My plan for sunlight aimed at 10 to 25 times less aggregate memory at 32 workers, and set a 5x reduction against the best incumbent as the kill criterion.

16.1.3 Setup

The measurements come from sunlight’s benchmark notes of 2026-08-01 (bench/results/ in the sunlight repository).

  • The pool comparison ran sunlight against parallel (PSOCK), future multisession, mirai and crew, at two configurations: 8 workers with the core tidyverse preloaded, and 32 workers with Seurat. Aggregate memory is the sum of each worker’s VmRSS from /proc, and for sunlight’s workers also PSS, which splits a shared page among the processes sharing it. Every worker was fully initialized and had run a task before its memory was read.
  • To see where the sharing went, I measured four workers forked from a Seurat-class template (449 MB resident) at each stage of their startup. I also measured one forked child of a plain R parent with Seurat loaded (390 MB resident), with no sunlight code involved.

16.1.4 Results

Time to first result at 32 workers with Seurat was 4.82 seconds for sunlight against 14.87 seconds for the best incumbent, mirai: 3.1 times faster.

Memory was the miss. sunlight’s aggregate PSS was 1.10 times lower than the best incumbent’s aggregate RSS at 32 workers (11,667.7 MB against 12,808.0 MB) and 1.28 times lower at 8 workers (819.6 MB against 1,048.1 MB). The hypothesis needed 5x.

The stage measurements showed where the sharing went. Right after the fork, the four workers held 2.6 MB of private memory between them. After each had run its post-fork initialization, before any task, they held 1,449.3 MB, roughly a private copy of the template’s heap each. The plain R child held 2.3 MB of private memory at birth, 2.9 MB after a tiny allocation, and 308.8 MB after one gc(), about 79 percent of its parent’s heap.

From sunlight’s runs of 2026-08-01: 4.82 s against 14.87 s to first result at 32 workers with Seurat; sunlight’s aggregate PSS 1.28x below the best incumbent’s aggregate RSS at 8 workers and 1.10x below at 32; a forked child’s private memory going from 2.3 MB to 308.8 MB across one gc(). Three caveats travel with the pool numbers. All five frameworks ran in one process in a fixed order with sunlight last, against a warm filesystem cache. The host was WSL2 on Windows 11, and a bare-metal Linux reproduction is still owed. crew launched only 5 of 8 and 8 of 32 configured workers, so it is left out of the memory comparison.

16.1.5 Interpretation

The child’s garbage collector is the culprit. R’s collector is generational: a minor collection marks only the youngest generations, and a full collection (level 2 in memory.c) walks every reachable object and sets a mark bit in its header. Setting a mark bit is a write, and a write to an object header dirties the page that holds the object, so the kernel copies that page from the parent. Almost every page the child inherited holds some reachable object, so one full collection dirties almost every page and the kernel privatizes them all. The plain R measurement used gc(), which runs a full collection by default. The notes do not record which collection level fired during the workers’ post-fork initialization; they record only that nearly the whole heap was private afterwards.

The floor comes from R’s garbage collection design, not from fork. Changes on the fork side did not move it: overlapping worker startup made a pool 5.4x faster to start and saved no memory, and a full gc() in the template before forking saved 11 percent. Deferring the child’s first collection did not work either. The collection that fires is driven by the node heap, which loading packages fills, and R_NSIZE, the setting that raises it, only moves the trigger, so the template’s node heap grows toward the higher bound before its first collection: it tripled the template’s own footprint and still did not prevent privatization. A fix would have to go into the collector, for example an equivalent of Python’s gc.freeze().

The startup gain is unaffected, because a forked worker still skips R startup and library(). The memory gain is small, and what remains of it comes from pages the collector never writes, such as mapped shared libraries.

16.1.6 Limitations

Both preload stacks, tidyverse and Seurat, are heavy. A template with a small heap has less to privatize, and I did not measure one. The child does not get to run long before it collects: a template that has just loaded a heavy package sits close to its node-heap trigger, so the first allocation in the child is enough. I did not record which collection level ran then. A minor collection alone would leave the old generations’ headers untouched, so a run that logs collection levels in the child would show how much of the privatization each level causes.

The host was WSL2 on Windows 11, a Linux kernel in a virtual machine. A bare-metal Linux run is still owed, and NUMA machines may behave differently. The mechanism, the mark phase writing to object headers, does not depend on the operating system.

I did not separate the collector’s own metadata, such as its generation lists, from the objects it marks. Both get dirtied, and I have no measure of their share.

I read RSS, PSS and private memory from /proc, not page-fault counts, so I know how much was privatized but not the order in which the pages went.

16.1.7 Gaps in this record

The sunlight repository records the host: WSL2 with Ubuntu 24.04 (kernel 5.15.167.4-microsoft-standard-WSL2) on an AMD Ryzen 7 7700X, with 6 logical cores and 23 GB of RAM visible to WSL2, R 4.5.1, and package versions pinned in bench/env/. Both configurations ask for more workers than those 6 cores, which the harness treats as part of the startup and memory cost being measured. What the record lacks is repetition. In the 2026-08-01 vignette run each pool configuration ran once, and the only second reading is the earlier cold-cache run on older code, so there is no variance to report.

16.1.8 Next experiment

Quantify page-dirtying per garbage collection generation. Instrument the collector to record which object types and sizes trigger the most writes. Does the mark phase alone dirty the most pages, or is most dirtying done in the sweep phase?

R’s settings cannot defer the first collection, as above, but a patched R could skip the objects a child inherited from its template, the way gc.freeze() does in Python. How much memory stays shared if it does?

Explore mark state that does not live in the object header. Some collectors keep mark bits in a separate bitmap. R’s mark phase also unlinks each object from its generation list and relinks it, which writes pointers into the object and its neighbors, so a bitmap alone would not keep the pages clean. Measure what moving both out of the object costs against the memory saved.

Compare fork performance against other startup methods. How much of the speedup comes from shared pages, and how much comes from reusing the loaded runtime? If packages are preloaded without forking, how close does startup time get to fork’s startup time?

Where this appears

  • sunlight Core finding that shaped the forkserver design and memory expectations.

Where to go next

Back to top