"매 C++ object 매 trace-based GC". 매 2014 Blink (Chromium renderer) 의 DOM tree memory bug 해결 위해 도입 된 매 C++ GC. 매 2021 V8 의 매 cppgc 로 generalize 되어 매 Node.js native module / Dart VM 의 사용. 매 raw pointer 의 cycle leak 매 fundamental 해결.
매 핵심
매 motivation
매 DOM tree 매 cyclic reference (parent ↔ child) 매 매우 흔함.
매 RefCounted (smart pointer) 의 cycle 매 leak.
매 manual delete 매 use-after-free / double-free 폭발.
매 Blink 매 2010-2014 매 매 brutal memory bug 매 routine.
매 Oilpan 동작
매 GarbageCollected<T> base class 매 inherit → 매 GC 의 manage.
매 Member<T> smart pointer 매 GC-tracked field 매 declare.
매 Trace(Visitor*) virtual method 매 reachability 의 manual report.
매 incremental marking + concurrent sweeping → 매 main thread pause < 1ms.
매 응용
Blink DOM (Element, Node, Document) 매 모든 lifecycle.
auto*node=cppgc::MakeGarbageCollected<Node>(heap.GetAllocationHandle());// 매 delete 의 X — GC 가 reclaim
3. Persistent (off-heap reference)
classNonGcOwner{cppgc::Persistent<Node>root_;// 매 strong root
cppgc::WeakPersistent<Node>observer_;// 매 weak (clear 시 nullptr)
};
4. Pre-finalizer (cleanup hook)
classResource:publiccppgc::GarbageCollected<Resource>{USING_PRE_FINALIZER(Resource,Dispose);voidDispose(){// 매 GC 직전 호출 — 매 file handle close 등
if(fd_>=0)close(fd_);}voidTrace(cppgc::Visitor*)const{}private:intfd_=-1;};
5. Cross-thread safety
// 매 GC heap 매 single thread (renderer main).
// 매 worker → main thread post 매 cppgc::CrossThreadPersistent.
cppgc::CrossThreadPersistent<Node>handle(node);PostTaskToMain([handle](){handle->DoSomething();});
언제: 매 C++ project 에서 매 cyclic object graph 매 unavoidable. 매 V8 embedder 매 native object 와 JS object 의 unified GC.
언제 X: 매 simple resource ownership (RAII 매 충분). 매 hard real-time. 매 embedded (memory budget tight).
❌ 안티패턴
Raw pointer 매 GC heap object 매 hold: 매 GC 가 collect → use-after-free. 매 항상 Member/Persistent.
Trace 매 incomplete: 매 missed field 매 premature collection. 매 Clang plugin 매 lint check 활용.
Pre-finalizer 매 heavy work: 매 GC 의 pause 증가. 매 light cleanup 만.
Cross-thread 매 raw Member: 매 data race + 매 GC 의 oblivious. 매 CrossThreadPersistent 사용.
Stack 의 conservative scan 의 abuse: 매 false retention. 매 kNoHeapPointers state 매 가능 한 사용.