"매 unreachable memory를 자동으로 reclaim한다". 매 GC는 manual malloc/free의 cognitive burden을 제거 — McCarthy의 Lisp(1959)에서 시작, modern V8/HotSpot/Go의 generational + concurrent collector로 evolution. 2026 trade-off는 매 throughput vs latency vs memory overhead.
매 핵심
매 Algorithm 분류
Mark-and-sweep: 매 reachable mark → unreachable sweep (fragmentation)
Copying (semi-space): 매 live obj 새 space로 copy (50% memory waste)
Mark-compact: 매 mark 후 live obj 한쪽 끝으로 compact
Reference counting: 매 ref count 0 즉시 free (cycle 문제)
매 Generational Hypothesis
매 most objects die young: 매 short-lived 95%+
Young gen (eden + survivor): 매 frequent, fast minor GC
Old gen (tenured): 매 infrequent, expensive major GC
import"runtime/debug"funcinit(){// 매 GOGC=100 default — 100% growth 시 triggerdebug.SetGCPercent(50)// 매 lower = more frequent, less memory// 매 hard memory limit (Go 1.19+)debug.SetMemoryLimit(8<<30)// 매 8GB}// 매 manual hint after large allocruntime.GC()
V8 Heap snapshot (Node.js)
constv8=require('v8');constfs=require('fs');// 매 heap snapshot for leak analysis
constsnapshot=v8.writeHeapSnapshot();console.log(`Heap snapshot: ${snapshot}`);// 매 heap statistics
console.log(v8.getHeapStatistics());// { total_heap_size, used_heap_size, heap_size_limit, ... }
Reference cycle break (Python)
importweakrefclassParent:def__init__(self):self.children=[]classChild:def__init__(self,parent):# 매 strong cycle: parent ↔ child → leak# self.parent = parent# 매 fix: weakrefself.parent=weakref.ref(parent)defget_parent(self):returnself.parent()# 매 None if collected
Object pooling (avoid GC pressure)
publicclassBulletPool{privatereadonlyStack<Bullet>pool=new();publicBulletRent(){if(pool.Count>0)returnpool.Pop();returnnewBullet();}publicvoidReturn(Bulletb){b.Reset();pool.Push(b);}}// 매 hot path에서 alloc 회피 → minor GC 감소
Tricolor marking (concept, Go-style)
// 매 white = unscanned, gray = in queue, black = scanned// invariant: 매 black은 white를 직접 reference 안 함// write barrier로 enforce:funcwriteBarrier(slot**Object,ptr*Object){ifisBlack(slot)&&isWhite(ptr){markGray(ptr)// 매 promote to gray}*slot=ptr}
.NET Span (stack alloc, no GC)
publicintSumDigits(intn){Span<int>digits=stackallocint[16];inti=0;while(n>0){digits[i++]=n%10;n/=10;}intsum=0;for(intj=0;j<i;j++)sum+=digits[j];returnsum;// 매 zero heap allocation}
매 결정 기준
상황
GC choice
Low-latency trading (JVM)
ZGC / Shenandoah (sub-ms pause)
Throughput batch (JVM)
Parallel GC
Game engine (managed)
Object pooling + GC.Collect at safe points
Real-time embedded
Manual memory or Rust (no GC)
Server-side Go
Default tricolor + GOGC tuning
기본값: 매 modern runtime의 default GC. 매 measure first (allocation profiler), tune later.