javascriptcore garbage collector

Other Community

Skill notes

The mental model

The heap is a graph. GC does a breadth-first search from roots → marks everything it reaches → everything unmarked is freed (lazily, on next allocation from that block). It does NOT compact or move objects — pointers stay stable for an object's lifetime. Two collection modes: Eden GC: only scans newly-allocated objects + remembered set. Fast, frequent. Full GC: scans everything. Slower, rarer. It runs concurrently. Marking happens on background threads while JS is executing; the mutator only stops at brief safepoints. visitChildren runs off the main thread, racing with your code.

How the VM gathers roots

Roots are not a hardcoded list — they are marking constraints registered with Heap::addMarkingConstraint() and run to fixpoint. The built-in set lives in Heap::addCoreConstraints() (vendor/WebKit/Source/JavaScriptCore/heap/Heap.cpp:2970): Tag · Name · What it marks Cs · Conservative Scan · Native stack + registers of every JS thread, scanned word-by-word (gatherStackRoots → ConservativeRoots). Also JIT stub routines. World is stopped for this.

Object layout: the 8-byte JSCell header

Every GC-managed object inherits JSCell (runtime/JSCell.h): StructureID — compressed hidden-class pointer indexingTypeAndMisc — 2 bits are an embedded WTF::Lock (the cell lock); always CAS this byte cellState — inlined GC color, used by the write barrier Out-of-line, in the MarkedBlock footer (or PreciseAllocation header for objects >~8KB):

CellState and the write barrier

vendor/WebKit/Source/JavaScriptCore/heap/CellState.h: Generational + concurrent GC share one retreating-wavefront barrier: You almost never write this by hand. Use WriteBarrier<T> as the field type and call .set(vm, owner, value) — it stores then barriers. A raw JSCell* / JSValue member without a WriteBarrier wrapper is a bug: eden GC will free the target out from under you. LazyProperty<Owner, T>, LazyClassStructure, and WriteBarrierStructureID are barrier-aware variants for lazily-initialized fields and structures.

Allocation: where objects live

bmalloc/libpas provides pages; JSC carves them up: MarkedBlock — 16KB block, fixed cell size (segregated free list). Footer holds bitvectors. 16-byte minimum cell alignment. addr & ~(16KB-1) → block, so liveness checks are O(1). PreciseAllocation — large objects (>~8KB), individually malloc'd, 96-byte GC header. Always returns addresses with addr % 16 == 8 so ptr & 8 distinguishes them from MarkedBlock cells.

Fluxly profile Runtime and permissions are inferred by Fluxly static checks, not author declarations or runtime test results.
Domain
Other
Author-declared agents
No explicit declaration found; this is not inferred or tested compatibility
Static check
88 / 100 · heuristic scan, not runtime safety proof
Runtime evidence
Not run; no isolated runtime test evidence is currently recorded
Author / version / license
@oven-sh · no license declared
Token usage
Moderate
Setup complexity
Guided setup
External API key
Not required
Operating systems
macOS · Linux · Windows
Runtime requirements
Node.js
Detected file/system behavior
  • Read-only
  • Write / modify
  • Shell exec
Network behavior
Local-only
Install commands
None (reference only)

Heads up: Tool permissions are not declared; actual access depends on the runtime.

Output preview javascriptcore-garbage-collector.preview
# visitAdditionalChildren and output constraints

// Custom hook called from BOTH places:
template<typename Visitor>
void JSFoo::visitAdditionalChildren(Visitor& visitor) {
    wrapped().listeners().visitJSEventListeners(visitor);
    visitor.addOpaqueRoot(&wrapped());
}

// 1) From visitChildren (normal marking):
DEFINE_VISIT_CHILDREN_WITH_MODIFIER(..., JSFoo) {
    ...
    thisObject->visitAdditionalChildren(visitor);
}

// 2) From visitOutputConstraints (constraint fixpoint re-scan):
template<typename Visitor>
void JSFoo::visitOutputConstraints(JSCell* cell, Visitor& visitor) {
    auto* thisObject = jsCast<JSFoo*>(cell);
    Base::visitOutputConstraints(thisObject, visitor);
    thisObject->visitAdditionalChildren(visitor);
}

Discussion

Powered by GitHub Discussions. Sign in with GitHub to comment, react, or subscribe.