15.2 Should GC Be Per Interpreter?
question-status: Tentative.
15.2.1 Context
GNU R runs one garbage collector over one heap, and every object lives in that heap. The collector is generational: it marks reachable objects, sweeps unreachable ones, and never moves an object.
In a multi-interpreter design, one collector over one heap becomes a bottleneck. R’s collector stops the world: nothing else may run R code while it marks. A single collector stopping several independent interpreters serializes their progress. Per-interpreter collectors could let each interpreter manage its own memory.
The multi-interpreter goal assumes per-interpreter heaps, and getting there means breaking every link between objects that belong to different interpreters.
15.2.2 Stakes
If per-interpreter heaps are not feasible, multi-interpreter parallelism collapses. With one global collector, every stop-the-world pause stops every interpreter, and one interpreter’s long collection blocks all the others.
Per-interpreter collectors would let each interpreter collect on its own schedule: one collects while another computes, and a pause stops only the interpreter whose heap is being collected.
Some objects have to be shared anyway. Compiled packages compare against R_NilValue and a few other constants by address, so every interpreter has to see the same object. Everything else can live in a private heap.
15.2.3 Known constraints
Every R object is a SEXPREC structure. Inside each SEXPREC live generation list pointers and mark bits. These fields tie the object to a specific collector. When an object crosses from one interpreter’s heap to another, the collector in the destination interpreter does not own the mark bits.
A collector that reaches an object it does not own cannot safely mutate that object’s GC metadata.
The moment a SEXP crosses a boundary, the source interpreter’s collector and the destination interpreter’s collector both reach that object. But only one of them owns the GC bits. The other cannot write to them without risking corruption.
threaded-r’s churn gate failed for exactly that reason while its interpreters still shared their roots: a collector reached objects it did not own.
threaded-r now gives each interpreter its own heap and collector. A collection in one interpreter leaves the other’s live count alone, and a node allocated in one lies inside that heap’s pages and outside the other’s (docs/p4/two-heaps.md). The churn gate, which counts nodes on one interpreter’s generation rings that live in the main heap, has read zero on every run since 2026-08-11, against 814 to 6,432 per run before each interpreter had its own symbol table, string cache and environments (docs/p4/churn-gate.md). The collector’s tuning parameters, such as the heap growth fractions, stay process-wide (docs/p4/heap-ownership.md).
r-in-rust was built with per-interpreter heaps from its first phase. Each interpreter owns its heap, its collector, its symbol table and its string cache. Values that cross between interpreters, as in mclapply, are serialized to bytes; handles never cross (docs/p15/status.md).
15.2.4 Candidate approaches
Fully isolated heaps. Each interpreter has its own heap and collector, no object crosses a boundary, and any sharing is a copy. BEAM, Erlang’s virtual machine, works this way: each Erlang process has its own heap, and a message is copied into the receiver’s heap (large binaries are the exception; they live off-heap and are shared by reference count). The cost is copying, and large data cannot be shared without it. This is what r-in-rust does.
Shared immortal arena plus isolated heaps. A small set of objects is immortal and shared, and every other object lives in an interpreter-private heap. This is what threaded-r built. Its arena holds the eleven constants, among them R_NilValue, NA_STRING and R_UnboundValue, plus R_EmptyEnv and the 38 shared symbols with their print names. They sit on no page and no generation list, and their mark bit stays permanently set, so no collector ever writes to them. Each interpreter collects its own heap without touching anyone else’s, and a new interpreter only has to reference the arena.
One shared heap with stop-the-world collection. All interpreters use one heap and one collector, and every pause stops all of them. This is GNU R’s design extended to several interpreters. It is simple but serializes progress, and a full collection takes long because it marks everything reachable from every interpreter. Julia takes this shape for multithreaded code: one heap, and every thread stops for a collection.
The accented region is what the interpreters hold in common. It grows from nothing, to a set of constants nobody writes, to everything.
15.2.5 Evidence so far
Before each interpreter had its own symbol table, string cache and environments, threaded-r’s churn gate counted 814 to 6,432 nodes per run that one interpreter’s collector had pulled from the main heap onto its own generation rings, even though every interpreter already had a separate heap. That is the constraint above happening, through shared roots. Once the roots were partitioned it counted zero.
r-in-rust has had per-interpreter heaps from the start. In its P10 baseline, fixed work split across interpreters on threads ran at 93 to 94 percent efficiency at four threads on fib(24), which triggers about 50 collections per iteration, with no lock and no shared heap (docs/p10/status.md).
What does independent collection buy in practice? Neither project has measured pause times or throughput with several interpreters collecting, against a shared collector on the same workload.
15.2.6 Experiments needed
threaded-r has settled the arena’s scope: design amendment A8 keeps base R per interpreter, with the arena holding only the objects the ABI forces to be shared. The open experiments are about what the split buys.
First, measure collection pauses in threaded-r as the interpreter count grows. Does a full collection in one interpreter change the others’ throughput?
Second, measure what crossing costs: time and memory for large values sent through threaded-r’s channels, which serialize.
Third, compare r-in-rust’s per-interpreter collection with threaded-r’s on the same workload, controlling for other differences such as native code.
15.2.7 Current status
Both projects have answered the question with a yes in their own code. threaded-r gives each interpreter its own heap and collector, with the eleven shared constants, R_EmptyEnv and the 38 shared symbols in an immortal arena and the collector’s tuning state process-wide. r-in-rust had per-interpreter heaps from the start. What is missing is a measurement of what independent collection buys, so the question stays tentative.
Where this appears
- threaded-r Each interpreter now owns its heap and collector; eleven shared constants, R_EmptyEnv and the 38 shared symbols live in an immortal arena, and the collector’s tuning state stays process-wide.
- r-in-rust (rrs) Built with per-interpreter heaps from its first phase.