---
id: wiki-2026-0508-write-barrier
title: Write Barrier
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [write barrier, store barrier, GC write barrier]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [garbage-collection, memory, runtime, gc, jvm]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
  language: C++/Rust/Java
  framework: GC runtimes
---

# Write Barrier

## 매 한 줄
> **"매 reference store 의 inline 직전 추가 코드, 매 GC가 매 inter-generational pointer 또는 concurrent invariant 를 track 하기 위함"**. 매 generational / incremental / concurrent GC의 핵심 mechanism으로, 2026 modern runtime (HotSpot ZGC, OpenJDK G1, .NET, V8 Orinoco, Go) 모두 형태가 다른 write barrier를 사용.

## 매 핵심

### 매 왜 필요
- Generational GC: old → young pointer 의 매 빠른 식별 (remembered set).
- Concurrent GC: mutator + collector 동시 동작 시 invariant 유지 (SATB, Incremental Update).
- 매 every reference store 마다 매 hooked code path.

### 매 종류
- **Card-marking** (G1, .NET): heap을 card (~512B) 로 분할, store 시 card byte를 dirty.
- **Snapshot-At-The-Beginning (SATB)** (G1, ZGC): 매 overwrite 직전 old value를 mark queue에.
- **Incremental Update** (CMS): 매 store 의 new value를 mark queue에.
- **Read barrier** (ZGC, Shenandoah): 매 load 시점 hook (대조).

### 매 응용
1. 매 generational collector의 minor GC가 old gen scan 안 해도 됨.
2. 매 concurrent marking의 lost-edge 방지.
3. 매 region-based collector의 inter-region reference 추적.

## 💻 패턴

### Card-marking (pseudo C)
```c
// every "obj.field = val"
void store_ref(Object* obj, int idx, Object* val) {
  obj->fields[idx] = val;
  // 매 barrier
  uintptr_t card = ((uintptr_t)obj) >> 9;  // 512B
  card_table[card] = DIRTY;
}
```

### SATB barrier
```c
void satb_store(Object* obj, int idx, Object* val) {
  Object* old = obj->fields[idx];
  if (concurrent_marking_active && old != NULL && !is_marked(old))
    enqueue_satb(old);  // preserve snapshot
  obj->fields[idx] = val;
}
```

### Incremental update barrier
```c
void inc_store(Object* obj, int idx, Object* val) {
  obj->fields[idx] = val;
  if (concurrent_marking_active && val != NULL && is_grey_or_white(val))
    enqueue_mark(val);  // re-scan
}
```

### Rust / unsafe 매 manual barrier (toy)
```rust
unsafe fn write_field(obj: *mut Obj, off: usize, val: *mut Obj) {
    let slot = (obj as *mut u8).add(off) as *mut *mut Obj;
    *slot = val;
    // card mark
    let card_idx = (obj as usize) >> 9;
    CARD_TABLE[card_idx].store(1, Ordering::Relaxed);
}
```

### V8 (incremental marking write barrier)
```cpp
// simplified
template<typename T>
void StoreInObject(HeapObject host, T value) {
  host.SetSlot(offset, value);
  if (incremental_marking_running) {
    if (IsWhite(value)) MarkObject(value);
  }
}
```

## 매 결정 기준
| 상황 | GC choice |
|---|---|
| 매 latency-critical (low pause) | ZGC / Shenandoah (read+write barriers) |
| 매 throughput-critical | Parallel GC (no concurrent barrier) |
| 매 balanced | G1 (SATB + card) |
| 매 manual memory | Rust / C++ — 매 barrier 의 X |

**기본값**: 매 modern JVM = G1 (Java 9+ default), 매 ultra-low pause = ZGC.

## 🔗 Graph
- 부모: [[Garbage-Collection]] · [[Memory-Management]]
- 변형: [[Read-Barrier]] · [[Card-Marking]] · [[SATB]]
- 응용: [[G1-GC]] · [[ZGC]] · [[V8-Orinoco]] · [[.NET-GC]]
- Adjacent: [[Generational-Hypothesis]] · [[Remembered-Set]] · [[Tri-Color-Marking]]

## 🤖 LLM 활용
**언제**: 매 GC tuning, 매 runtime 구현.
**언제 X**: 매 application-level code — 매 mostly invisible.

## ❌ 안티패턴
- **매 barrier 비활성화 시도**: 매 GC invariant 깨짐 → silent heap corruption.
- **매 barrier overhead 무시**: 매 hot loop의 매 store 마다 추가 instruction.

## 🧪 검증 / 중복
- Verified (Jones, Hosking, Moss — *The Garbage Collection Handbook* 2nd; OpenJDK ZGC docs 2026).
- 신뢰도 A.

## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — write barrier types + GC examples |
