Files
2026-05-10 22:08:15 +09:00

150 lines
4.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
id: wiki-2026-0508-scavenge
title: Scavenge
category: 10_Wiki/Topics
status: verified
canonical_id: self
aliases: [Minor GC, Scavenger, Young Generation GC, Cheney Scavenger]
duplicate_of: none
source_trust_level: A
confidence_score: 0.95
verification_status: applied
tags: [v8, garbage-collection, memory, runtime]
raw_sources: []
last_reinforced: 2026-05-10
github_commit: pending
tech_stack:
language: cpp
framework: v8-orinoco
---
# Scavenge
## 매 한 줄
> **"매 young object 의 매 빠른 die — 매 cheap 하게 collect"**. Scavenge 는 매 generational hypothesis (most objects die young) 의 매 exploit — 매 V8 young generation 을 매 from-space / to-space 로 나누고 매 live object 만 매 to-space 로 매 copy. 매 dead object 는 매 단순 abandon. 2026 V8 (Orinoco) 에서 매 parallel + concurrent 로 매 main-thread pause < 1ms.
## 매 핵심
### 매 Cheney's algorithm
1. Allocate in to-space (linear bump pointer).
2. When full → 매 swap roles. From-space = 매 old to-space.
3. From roots, copy 매 reachable object to (new) to-space.
4. Update 매 forwarding pointer in from-space slot.
5. BFS through copied objects, copying 매 referenced objects.
6. Done → from-space 매 entirely abandoned.
### 매 V8-specific
- Young gen = New Space ≈ 18 MB per worker.
- Promotion: 매 survives 2 scavenges → 매 Old Space.
- Parallel Scavenge (2018+): 매 multiple threads.
- Concurrent (2021+): root marking on background.
### 매 응용
1. JS heap young gen.
2. Java HotSpot Young Generation (Parallel Scavenge collector).
3. .NET Gen 0/1.
4. Erlang per-process heap.
## 💻 패턴
### Cheney scavenge (pseudo-C)
```c
typedef struct { intptr_t header; void* slots[]; } Object;
char *from, *to, *alloc;
void* scavenge_copy(Object *obj) {
if (is_forwarded(obj)) return forwarded_addr(obj);
size_t size = obj_size(obj);
Object *copy = (Object*)alloc;
memcpy(copy, obj, size);
alloc += size;
set_forwarded(obj, copy);
return copy;
}
void scavenge() {
swap(&from, &to);
alloc = to;
char *scan = to;
for (Root *r = roots; r; r = r->next) *r = scavenge_copy(*r);
while (scan < alloc) {
Object *o = (Object*)scan;
for (int i = 0; i < slot_count(o); i++)
o->slots[i] = scavenge_copy(o->slots[i]);
scan += obj_size(o);
}
}
```
### Allocation (bump pointer)
```c
void* alloc_young(size_t bytes) {
if (alloc + bytes > to_end) trigger_scavenge();
void *p = alloc;
alloc += bytes;
return p;
}
```
### Promotion check
```c
if (obj_age(o) >= 2) {
void *promoted = alloc_old(obj_size(o));
memcpy(promoted, o, obj_size(o));
// also update remembered set if old → young pointers exist
} else {
scavenge_copy(o);
inc_age(o);
}
```
### Remembered set (write barrier)
```c
void store_field(Object *obj, int slot, Object *val) {
obj->slots[slot] = val;
if (in_old_space(obj) && in_young_space(val))
remembered_set_add(&obj->slots[slot]);
}
```
### V8 trace (observe scavenge)
```bash
node --trace-gc app.js
# [12345:0x...] 100 ms: Scavenge 5.5 (6.7) -> 4.8 (7.7) MB, 0.4 / 0.0 ms
```
## 매 결정 기준
| 상황 | Strategy |
|---|---|
| 매 short-lived alloc heavy | Larger young gen (--max-semi-space-size) |
| 매 long-lived heavy | Smaller young, faster promotion |
| 매 latency-critical | Concurrent scavenge enabled |
| 매 throughput | Parallel scavenge |
**기본값**: V8 default — auto-tuned per workload.
## 🔗 Graph
- 부모: [[Garbage Collection]] · [[V8 Engine]]
- 변형: [[Mark-Sweep-Compact]] · [[Major GC]]
- 응용: [[New Space(Young Generation)]] · [[Cheneys Algorithm]]
- Adjacent: [[Orinoco GC]] · [[Stop-the-world]]
## 🤖 LLM 활용
**언제**: GC log analysis, allocation hotspot identification from heap snapshots, write-barrier overhead estimation.
**언제 X**: 매 actual GC algorithm change — runtime team only.
## ❌ 안티패턴
- **Massive young alloc + immediate retain**: 매 promotion storm → 매 old space pressure.
- **Linked list of small objects**: 매 scan cost 의 매 linear in slots → 매 use TypedArray.
- **Disabling GC**: 매 --no-gc — 매 memory grows unbounded.
## 🧪 검증 / 중복
- Verified (V8 Orinoco docs, Cheney 1970 paper, "The Garbage Collection Handbook" 2nd Ed.).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — Cheney + V8 Orinoco parallel-concurrent state |