--- id: wiki-2026-0508-쓰기-장벽-write-barrier title: 쓰기 장벽(Write Barrier) category: 10_Wiki/Topics status: verified canonical_id: self aliases: [Write Barrier, GC Write Barrier, Generational Barrier, Card Marking] duplicate_of: none source_trust_level: A confidence_score: 0.95 verification_status: applied tags: [garbage-collection, runtime, jvm, v8, memory-management] raw_sources: [] last_reinforced: 2026-05-10 github_commit: pending tech_stack: language: runtime-internals framework: HotSpot/V8/Go-runtime --- # 쓰기 장벽(Write Barrier) ## 매 한 줄 > **"매 reference store 시 GC 가 끼어드는 hook"**. 매 generational / concurrent GC 의 핵심 mechanism — 매 mutator 가 pointer 를 쓸 때마다 매 GC metadata 를 업데이트하여 매 cross-generation reference 추적 + concurrent marking 의 invariant 유지. 2026 년 모든 modern GC (G1, ZGC, Shenandoah, V8 Orinoco, Go GC) 매 필수. ## 매 핵심 ### 매 정의 - 매 `obj.field = ref` instruction 시 매 runtime-injected code. - 매 compiler 가 매 store 직전/직후 hook 삽입. - 매 cost: 매 native pointer write 의 1.05x ~ 1.3x. ### 매 종류 - **Card marking**: 매 heap 을 card (보통 512B) 단위로 분할 — 매 dirty bit 표시. - **Remembered set (RSet)**: 매 region 별 incoming reference list. - **SATB (Snapshot-At-The-Beginning)**: 매 G1/Shenandoah — 매 마킹 시작 시 graph snapshot. - **Incremental update**: 매 CMS 류 — 매 새로 추가된 reference 추적. - **Dijkstra-style**: 매 black-to-white reference 차단 (Go). ### 매 응용 1. Generational GC — 매 old → young reference 추적. 2. Concurrent marking — 매 mutator-collector race 방지. 3. Region-based GC (G1, ZGC) — 매 cross-region reference RSet. ## 💻 패턴 ### Card marking (HotSpot CMS/G1) ```cpp // 매 conceptual code — 매 HotSpot 의 oop_store inline void oop_store(oop* field, oop new_val) { *field = new_val; // 매 actual write // 매 post-write barrier uintptr_t card_index = ((uintptr_t)field) >> CARD_SHIFT; // 매 9 = 512B card_table[card_index] = DIRTY; // 매 매 single byte write } // 매 minor GC 시 매 dirty card 만 scan — 매 old gen 전체 X void scan_dirty_cards_for_young_refs() { for (auto i = 0; i < card_count; i++) { if (card_table[i] == DIRTY) { scan_card_for_young_pointers(i); card_table[i] = CLEAN; } } } ``` ### G1 RSet (remembered set) ```cpp // 매 region 별 incoming reference 추적 class HeapRegion { PerRegionTable rset; // 매 다른 region 의 어디가 매 나를 가리키는지 void update_rset(oop* from_field, oop to_obj) { if (region_of(from_field) != this) { rset.add(card_of(from_field)); } } }; // 매 evacuation 시 RSet scan — 매 외부 reference rewrite ``` ### SATB write barrier (Shenandoah/G1 concurrent mark) ```cpp // 매 pre-write barrier — 매 old value 를 매 marking queue 에 push inline void satb_oop_store(oop* field, oop new_val) { if (concurrent_marking_active) { oop old_val = *field; if (old_val != nullptr && !is_marked(old_val)) { satb_queue.push(old_val); // 매 snapshot 보존 } } *field = new_val; } // 매 invariant: 매 marking 시작 시점의 graph 가 매 모두 visit ``` ### V8 incremental marking (Dijkstra-style) ```cpp // 매 black-to-white reference 차단 inline void v8_write_barrier(HeapObject* host, Object** slot, Object* value) { if (!is_incremental_marking) { *slot = value; return; } if (Marking::IsBlack(host) && Marking::IsWhite(value)) { // 매 violation — 매 white 를 grey 로 marking_worklist.push(value); Marking::WhiteToGrey(value); } *slot = value; } ``` ### Go GC hybrid barrier (Yuasa + Dijkstra) ```go // 매 runtime/mbarrier.go 의 conceptual //go:nosplit func writebarrierptr(slot **byte, val *byte) { // 매 hybrid: 매 deletion + insertion barrier 결합 if writeBarrier.enabled { old := *slot if old != nil { shade(old) } // 매 Yuasa (deletion) if val != nil { shade(val) } // 매 Dijkstra (insertion) } *slot = val } func shade(p *byte) { obj := findObject(p) if obj != nil && !marked(obj) { markGrey(obj) gcWork.push(obj) } } ``` ### Compiler intrinsic (LLVM/HotSpot) ```cpp // 매 JIT 가 매 oop_store 호출 매 inline expand // 매 "object.field = ref" 매 compile 결과: // 1. mov [rax+offset], rbx ; 매 actual store // 2. shr rax, 9 ; 매 card index // 3. mov byte [card_table + rax], 0 ; 매 mark dirty // 매 cost: 매 3 instructions, 매 ~1ns ``` ## 매 결정 기준 | GC type | Barrier 종류 | |---|---| | Generational (young/old) | Card marking + RSet | | Concurrent marking (G1, Shenandoah) | SATB pre-barrier | | Incremental (V8 Orinoco) | Dijkstra post-barrier | | Region-based evacuation (G1, ZGC) | Card marking + RSet + SATB | | Go GC | Hybrid (Yuasa + Dijkstra) | | Reference counting | Increment/decrement barrier | **기본값**: 매 modern multi-threaded GC 매 hybrid barrier — 매 SATB + card marking 결합. ## 🔗 Graph - 부모: [[Garbage Collection]] · [[Memory Management]] - 변형: [[Read Barrier]] · [[Load Barrier]] · [[Reference Counting Barrier]] - 응용: [[G1 GC]] · [[ZGC]] · [[Shenandoah]] · [[Go GC]] · [[V8 Orinoco]] - Adjacent: [[Tri-color Marking]] · [[Card Table]] · [[Remembered Set]] · [[Concurrent Marking]] ## 🤖 LLM 활용 **언제**: 매 GC 동작 분석, 매 GC overhead 측정, 매 custom runtime 설계, 매 JIT compiler 최적화. **언제 X**: 매 application-level memory tuning (매 heap size 조정 만 — 매 barrier internal 무관). ## ❌ 안티패턴 - **Manual barrier elision**: 매 "이 store 는 안전" 자체 판단 매 elision — 매 GC invariant 파괴. - **Card size 무관 micro-tune**: 매 application 매 card size 변경 — 매 runtime config 영역 X. - **Barrier ignore in JNI**: 매 native code 가 매 barrier bypass 하여 oop write — 매 dangling reference. - **Concurrent marking 중 atomic operation 무시**: 매 race condition. ## 🧪 검증 / 중복 - Verified (Jones et al. "The Garbage Collection Handbook" 2nd ed, HotSpot source code, V8 design docs, Go runtime source). - 신뢰도 A. ## 🕓 Changelog | 날짜 | 변경 | |---|---| | 2026-05-08 | Phase 1 | | 2026-05-10 | Manual cleanup — barrier 종류별 패턴 + runtime examples |