Files
2nd/10_Wiki/Topics/DevOps_and_Security/Scavenge.md
T
2026-05-10 22:08:15 +09:00

4.3 KiB
Raw Blame History

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-scavenge Scavenge 10_Wiki/Topics verified self
Minor GC
Scavenger
Young Generation GC
Cheney Scavenger
none A 0.95 applied
v8
garbage-collection
memory
runtime
2026-05-10 pending
language framework
cpp 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)

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)

void* alloc_young(size_t bytes) {
  if (alloc + bytes > to_end) trigger_scavenge();
  void *p = alloc;
  alloc += bytes;
  return p;
}

Promotion check

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)

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)

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

🤖 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