Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

cppgc: V8’s C++ heap

A JVM controls the language, the compiler and the runtime, so it can put barriers and stack maps everywhere. A garbage collector for C++ cannot. The code is written by people, pointers are raw, and the stack is whatever the C++ compiler made of it. cppgc, also known as Oilpan, is the best-known modern answer, and the closest relative of paragc.

Where it comes from

Chrome’s rendering engine, Blink, used reference counting for its DOM objects, and cycles between C++ DOM nodes and JavaScript wrappers leaked. Around 2013 the Blink team built Oilpan, a tracing garbage collector for Blink’s C++ objects. In 2020 it moved into V8 as the standalone library cppgc, so other embedders could use it too. Today Blink’s DOM, the V8 JavaScript heap and cppgc’s C++ heap form one unified heap, traced together.

The programming model

class Node final : public cppgc::GarbageCollected<Node> {
 public:
  void Trace(cppgc::Visitor* visitor) const {
    visitor->Trace(next_);
  }
 private:
  cppgc::Member<Node> next_;
};

auto* node = cppgc::MakeGarbageCollected<Node>(heap->GetAllocationHandle());
cppgc::Persistent<Node> root(node);
cppgcRoleparagc counterpart
GarbageCollected<T>base class of heap objects#[derive(Trace)]
Trace(Visitor*)written by hand, reports each pointer fieldgenerated by the derive
Member<T>pointer field inside a heap object, with a write barrierSlot<'gc, T>
WeakMember<T>weak fieldWeak inside an object
Persistent<T>root held from outside the heapRoot<B>
WeakPersistent<T>weak rootWeak<B>
raw T* on the stackfound by conservative stack scanningGc<'gc, T>, safe only inside mutate

How it collects

  • Mark-sweep with free-list allocation in size-class pages, plus bump allocation in linear allocation buffers. Ordinary objects do not move.
  • Conservative stack scanning: any word on a thread’s stack that points into the heap keeps that object alive. This is what lets C++ code hold raw pointers in local variables safely, and it is also why objects cannot move freely.
  • Incremental and concurrent marking with write barriers on Member stores, and concurrent sweeping.
  • Compaction only for backing stores: the buffers behind Blink’s vectors and hash tables can be compacted, because cppgc knows every slot that points at them.
  • Pre-finalizers run before sweeping, while every object is still intact, for cleanup that must touch other heap objects. Ordinary destructors run during sweeping, in no particular order.
  • A young generation (minor collections with a remembered set) exists behind a build flag and needs the caged heap: all objects in one large reservation, so a pointer can be checked against the heap with a mask.

What cppgc teaches

  • A C++ collector has to tolerate raw pointers on the stack, so it scans conservatively, and that limits moving.
  • Hand-written Trace methods are a frequent source of bugs: forgetting a field means a live object gets freed.
  • One unified heap across languages (C++ and JS) removes the leaks that two separate collectors with cross-references produce.

Where paragc differs

cppgcparagc
Stackscanned conservativelynot scanned; Gc handles are confined to mutate by Rust lifetimes
Stop-the-worldneeds every thread at a known point&mut Heap: the borrow checker proves nobody is inside
Movingordinary objects never moveevacuation and young copying, because no unknown raw pointers exist
Tracing codehand-written Tracederived; Drop on traced types rejected at compile time
Allocation threadsper-thread heapsone heap, many threads bump-allocating at once
Young objectsoptional minor GCsticky-mark young collections and reference counting by default
Sharing between processesnonecross-heaps on shared memory (Linux)

Rust gives paragc what cppgc has to buy with conservative scanning: the compiler knows when no pointer can be outside the collector’s view.