Files
2nd/10_Wiki/Topic_Programming/Programming & Language/가비지 컬렉션 (Garbage Collection).md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +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 |