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);
| cppgc | Role | paragc counterpart |
|---|---|---|
GarbageCollected<T> | base class of heap objects | #[derive(Trace)] |
Trace(Visitor*) | written by hand, reports each pointer field | generated by the derive |
Member<T> | pointer field inside a heap object, with a write barrier | Slot<'gc, T> |
WeakMember<T> | weak field | Weak inside an object |
Persistent<T> | root held from outside the heap | Root<B> |
WeakPersistent<T> | weak root | Weak<B> |
raw T* on the stack | found by conservative stack scanning | Gc<'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
Memberstores, 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
Tracemethods 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
| cppgc | paragc | |
|---|---|---|
| Stack | scanned conservatively | not scanned; Gc handles are confined to mutate by Rust lifetimes |
| Stop-the-world | needs every thread at a known point | &mut Heap: the borrow checker proves nobody is inside |
| Moving | ordinary objects never move | evacuation and young copying, because no unknown raw pointers exist |
| Tracing code | hand-written Trace | derived; Drop on traced types rejected at compile time |
| Allocation threads | per-thread heaps | one heap, many threads bump-allocating at once |
| Young objects | optional minor GC | sticky-mark young collections and reference counting by default |
| Sharing between processes | none | cross-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.