"매 GC root 부터 reference graph 의 traverse — reachable object 의 keep, unreachable 의 collect". 매 modern GC 의 fundamental algorithm — V8, JVM, .NET, Go 의 동일 원리. 2026 의 incremental + concurrent variant 의 mainstream.
매 핵심
매 GC Root 종류
Stack root: 매 active call stack 의 local variable / parameter.
Global root: 매 globalThis / window — 매 module-level binding.
Register root: 매 CPU register 의 held reference.
Compilation root: 매 V8 의 compiled code 의 reference table.
Handle root: 매 native binding 의 Handle / Persistent reference.
매 Traversal algorithm
Tri-color marking: White (unvisited) → Gray (queued) → Black (done).
Worklist-based: 매 root 의 enqueue → pop → mark → push referents.
Incremental: 매 small chunks 의 분할 — 매 main thread pause 의 단축.
Concurrent: 매 background thread 의 동시 mark — write barrier 의 sync.
매 응용
V8 의 Mark-Sweep-Compact GC 의 base.
Memory leak 의 발견 — DevTools heap snapshot 의 retainer path.
Cycle detection — refcount 의 약점 의 보완.
💻 패턴
매 Reachability 의 tri-color 의 idea
// 매 conceptual mark phase
functionmark(roots){constgray=newSet(roots);// 매 worklist
constblack=newSet();// 매 marked
while(gray.size>0){constobj=gray.values().next().value;gray.delete(obj);black.add(obj);for(constrefofgetReferences(obj)){if(!black.has(ref))gray.add(ref);}}// 매 black = reachable, 매 white (heap - black) = collect
}
매 DevTools Heap snapshot 의 root 의 확인
// Chrome DevTools → Memory → Heap snapshot
// 매 retainer path 의 (GC root) 까지 추적
// 매 closure / detached DOM / event listener 의 leak 의 발견
// 매 typical leak pattern
letleakedRef;window.addEventListener('click',()=>{leakedRef=bigData;// 매 GC root (global) 까지 retained
});
매 WeakRef 의 reachability 의 우회
// 매 weak reference — 매 reachability 의 contribute X
constcache=newMap();functionset(key,val){cache.set(key,newWeakRef(val));// 매 GC 의 collect 가능
}functionget(key){returncache.get(key)?.deref();// 매 collected 시 undefined
}
매 FinalizationRegistry 의 cleanup hook
constregistry=newFinalizationRegistry((heldValue)=>{console.log('Object collected:',heldValue);externalResources.delete(heldValue);});functiontrackResource(obj,id){registry.register(obj,id);}// 매 obj 의 unreachable 시 cleanup 의 trigger
매 v8 의 heap snapshot 의 programmatic
importv8from'node:v8';importfsfrom'node:fs';constsnapshot=v8.getHeapSnapshot();snapshot.pipe(fs.createWriteStream('heap.heapsnapshot'));// 매 Chrome DevTools 의 import → reachability graph 의 분석
매 Cycle 의 detection
// 매 Refcount 의 약점 — 매 cycle 의 leak
leta={},b={};a.ref=b;b.ref=a;// 매 V8 의 reachability-based GC 의 cycle 의 collect (root 의 unreachable 시)
매 결정 기준
상황
Approach
일반 object
implicit reachability — 매 default
Cache
WeakRef + FinalizationRegistry
External resource
FinalizationRegistry 의 cleanup
Cycle 의심
heap snapshot 의 retainer path
Memory leak debug
DevTools Memory tab
기본값: 매 explicit ref 의 nullify 보다 매 scope 의 의도된 사용 의 reachability 의 자연스러운 종료.
🔗 Graph
부모: Garbage Collection · Mark-Sweep-Compact 알고리즘
변형: Incremental Marking · Tri-color Marking
응용: GC Root · Memory Leak Debugging
Adjacent: Generational Hypothesis · Write Barrier
🤖 LLM 활용
언제: GC 동작 의 explain, memory leak 의 diagnose, WeakRef vs Map 의 권장.
언제 X: 매 application 의 GC 의 strict timing 의 의존 의 답변 — 매 non-deterministic 임 의 인지.
❌ 안티패턴
Global cache: 매 unbounded Map 의 GC root 까지 retained — 매 WeakMap 의 사용.
Closure leak: 매 outer function 의 large var 의 inner closure 의 capture.
Detached DOM: 매 removed node 의 JS reference 의 retained — 매 listener cleanup.
🧪 검증 / 중복
Verified (V8 docs, Garbage Collection Handbook by Jones).