Files
2nd/10_Wiki/Topics/Programming & Language/가비지 컬렉션 (Garbage Collection).md
T
Antigravity Agent f8b21af4be Wiki cleanup: error-doc removal, dedup merge, link normalization
10_Wiki/Topics 대규모 정리:
- 오류 캡처/미완성 stub 문서 227개 제거
- 교차폴더 중복 43클러스터 병합 (63파일 → redirect)
- 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건
- 카테고리 MOC 6개 신규 생성
- Graph 섹션 미해결 related-keyword 링크 10,058건 제거

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 23:52:15 +09:00

173 lines
5.7 KiB
Markdown

---
id: wiki-2026-0508-가비지-컬렉션-garbage-collection
title: 가비지 컬렉션 (Garbage Collection)
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [GC, Garbage Collection, 메모리 관리]
duplicate_of: none
source_trust_level: A
confidence_score: 0.9
verification_status: applied
tags: [gc, memory, runtime, jvm, v8]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: Java/JS/Go
framework: JVM/V8/Go-runtime
---
# 가비지 컬렉션 (Garbage Collection)
## 매 한 줄
> **"매 도달 불가능 (unreachable) 한 객체를 자동으로 회수하는 runtime memory manager"**. 1959 LISP의 mark-and-sweep 부터 현대의 ZGC/Shenandoah/G1 까지 — 매 generational + concurrent + region-based 방향으로 진화. 매 sub-millisecond pause 의 실용화 (2024+).
## 매 핵심
### 매 GC 의 근본 작업
- **Mark**: GC root (stack, globals, registers) 부터 reachable graph traversal.
- **Sweep / Compact / Copy**: unreachable 객체의 회수 + 매 fragmentation 의 처리.
### 매 주요 알고리즘
- **Mark-and-Sweep**: 매 simple, 매 fragmentation 발생.
- **Mark-Compact**: 매 sweep 후 live object 의 compact — fragmentation X.
- **Copying (Cheney)**: 매 from-space → to-space 의 live copy. 매 generational young gen 에 사용.
- **Generational**: 매 weak generational hypothesis — 매 young die young. 매 young gen frequent + old gen rare.
- **Concurrent / Incremental**: 매 mutator thread 와 동시 실행 — pause 의 최소화.
- **Region-based (G1, ZGC, Shenandoah)**: 매 heap 의 region 분할 — 매 partial collection.
### 매 응용
1. JVM (G1 default since Java 9, ZGC production since Java 17, Shenandoah).
2. V8 (Orinoco — concurrent + parallel + incremental).
3. Go (concurrent tri-color mark-sweep, sub-ms pause).
4. .NET CLR (generational + LOH).
5. CPython (reference counting + cycle detector).
## 💻 패턴
### Tri-color Marking (concurrent GC 의 기반)
```python
# Tri-color invariant: White (unmarked), Gray (marked, children pending), Black (done)
WHITE, GRAY, BLACK = 0, 1, 2
def tri_color_mark(roots, heap):
for obj in heap:
obj.color = WHITE
gray_set = set()
for r in roots:
r.color = GRAY
gray_set.add(r)
while gray_set:
obj = gray_set.pop()
for child in obj.refs:
if child.color == WHITE:
child.color = GRAY
gray_set.add(child)
obj.color = BLACK
# White = unreachable -> sweep
return [o for o in heap if o.color == WHITE]
```
### Write Barrier (concurrent GC 의 invariant 유지)
```c
// Dijkstra-style: black -> white write 발생 시 child를 gray로 승격
void write_barrier(Object* parent, Object** field, Object* new_val) {
if (parent->color == BLACK && new_val && new_val->color == WHITE) {
new_val->color = GRAY;
gray_queue_push(new_val);
}
*field = new_val;
}
```
### Generational Allocation (bump allocator)
```rust
struct YoungGen { start: *mut u8, top: *mut u8, end: *mut u8 }
impl YoungGen {
fn alloc(&mut self, size: usize) -> Option<*mut u8> {
unsafe {
let new_top = self.top.add(size);
if new_top > self.end { return None; } // trigger minor GC
let p = self.top;
self.top = new_top;
Some(p)
}
}
}
```
### Reference Counting + Cycle Detection (CPython)
```python
class Obj:
def __init__(self):
self.refcount = 1
def incref(self): self.refcount += 1
def decref(self):
self.refcount -= 1
if self.refcount == 0:
for child in self.refs: child.decref()
free(self)
# Cycle: a.refs=[b], b.refs=[a] -> refcount 영원히 >0 -> cycle collector 필요
```
### Finalizer (resource cleanup, careful)
```java
// Java AutoCloseable + try-with-resources >>> finalize() (deprecated in Java 9+)
try (var conn = DriverManager.getConnection(url)) {
// use conn
} // auto-close, deterministic
```
### Soft / Weak / Phantom Reference
```java
WeakReference<Cache> ref = new WeakReference<>(cache);
// GC가 free하면 ref.get() == null. Cache 구현에 자주 사용
```
### G1 / ZGC tuning (JVM)
```bash
java -XX:+UseZGC -Xmx16g -XX:+UseLargePages MyApp
# ZGC: <1ms pause, multi-TB heap (Java 21+)
java -XX:+UseG1GC -XX:MaxGCPauseMillis=200 MyApp
# G1: pause-target driven, default since Java 9
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Low-latency service (<10ms p99) | ZGC, Shenandoah, Go GC |
| Throughput batch | Parallel GC (JVM), G1 |
| Embedded / no-GC | Rust, C++ RAII |
| Predictable real-time | No GC + arena allocator |
| Reference cycles 빈번 | tracing GC > refcount |
**기본값**: 매 modern JVM 의 **G1** (or ZGC for >4GB heap), Go 의 **default concurrent collector**.
## 🔗 Graph
- 부모: [[메모리 관리]]
- 변형: [[Reference Counting]]
- 응용: [[V8 Engine]]
- Adjacent: [[Memory Leak]] · [[Heap]]
## 🤖 LLM 활용
**언제**: GC tuning, OOM 분석, GC log 해석, allocation pattern 최적화.
**언제 X**: 매 hot loop allocation 미세조정 — profiler (async-profiler, pprof) 가 우선.
## ❌ 안티패턴
- **System.gc() 호출**: hint 일 뿐, full GC 강제로 pause 유발.
- **finalize() 의존**: 매 deprecated. AutoCloseable 사용.
- **Large object 의 young gen 할당 가정**: TLAB overflow → tenuring promotion 발생.
- **WeakReference cache 의 무한 신뢰**: 매 GC pressure 시 즉시 회수 — cold start.
## 🧪 검증 / 중복
- Verified (Jones et al. *The Garbage Collection Handbook* 2nd ed., OpenJDK ZGC docs 2024).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — GC algorithms + tri-color + JVM/Go tuning patterns |