Files
2nd/10_Wiki/Topics/DevOps_and_Security/Cheneys Algorithm.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

4.6 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-cheneys-algorithm Cheney's Algorithm 10_Wiki/Topics verified self
Cheney GC
Semi-space Collector
Copying GC
none A 0.9 applied
gc
memory
algorithm
runtime
2026-05-10 pending
language framework
C/Rust runtime/GC

Cheney's Algorithm

매 한 줄

"매 stop-and-copy GC 의 BFS-style two-finger traversal". 1970년 C.J. Cheney 가 제시한 copying garbage collector 의 표준 algorithm — recursion 없이 queue-style 로 live object 를 from-space 에서 to-space 로 evacuate. 매 modern V8/SpiderMonkey young generation, OCaml minor heap, MLton 의 baseline.

매 핵심

매 semi-space 구조

  • Heap 을 두 개의 equal-sized region 으로 split: from-space, to-space.
  • Allocation 은 from-space 의 bump pointer 만 증가.
  • GC 시 live object 를 to-space 로 copy 후 role swap.

매 two pointers

  • scan: to-space 에서 아직 children 추적 안 한 boundary.
  • free: to-space 의 next allocation slot.
  • scan == free 이면 traversal 종료.

매 응용

  1. V8 young-gen scavenger (Node.js, Chrome).
  2. OCaml minor heap collection.
  3. SBCL, MLton 의 default GC.

💻 패턴

Core Cheney loop (C)

void* to_space; size_t scan, free_;

void* copy(void* obj) {
    if (is_forwarded(obj)) return forward_addr(obj);
    size_t sz = size_of(obj);
    void* dst = (char*)to_space + free_;
    memcpy(dst, obj, sz);
    set_forward(obj, dst);
    free_ += sz;
    return dst;
}

void cheney_gc(void** roots, size_t n) {
    free_ = scan = 0;
    for (size_t i = 0; i < n; i++) roots[i] = copy(roots[i]);
    while (scan < free_) {
        void* obj = (char*)to_space + scan;
        for_each_pointer_field(obj, p) { *p = copy(*p); }
        scan += size_of(obj);
    }
    swap(from_space, to_space);
}

Forwarding pointer trick

// Object header overlap: live header OR forwarding pointer.
struct header { uintptr_t tag_or_fwd; };
#define IS_FWD(h) ((h)->tag_or_fwd & 1)
#define FWD_PTR(h) ((void*)((h)->tag_or_fwd & ~1))
#define SET_FWD(h, dst) ((h)->tag_or_fwd = (uintptr_t)(dst) | 1)

Allocation (post-GC)

void* alloc(size_t sz) {
    if (free_ + sz > SEMI_SIZE) cheney_gc(roots, n_roots);
    if (free_ + sz > SEMI_SIZE) abort(); // OOM
    void* p = (char*)to_space + free_;
    free_ += sz;
    return p;
}

V8-style scavenger (simplified)

void Scavenger::Process() {
    while (!worklist_.empty()) {
        HeapObject obj = worklist_.Pop();
        obj->IterateBody(this);  // visits each pointer field
    }
}
void Scavenger::VisitPointer(Object** slot) {
    HeapObject obj = HeapObject::cast(*slot);
    if (Heap::InFromSpace(obj)) {
        HeapObject target = EvacuateObject(obj);
        *slot = target;
    }
}

Generational tweak

// Young gen uses Cheney; old gen uses mark-sweep.
// Promotion: if object survives N scavenges, copy to old-gen instead of to-space.
if (age(obj) >= PROMOTION_THRESHOLD) dst = old_gen_alloc(sz);
else dst = (char*)to_space + free_;

매 결정 기준

상황 Approach
Short-lived allocation 多 Cheney (semi-space) — fast bump alloc
Large heap, low live ratio Cheney 우수 (cost ∝ live, not heap)
Mostly-live mature data Mark-sweep / mark-compact
Real-time constraints Incremental / concurrent GC (Shenandoah, ZGC)
Memory tight (mobile) Mark-sweep (no 2× overhead)

기본값: Young generation 에 Cheney, old generation 에 mark-compact (generational hypothesis).

🔗 Graph

🤖 LLM 활용

언제: GC 설명, runtime internals 분석, language implementation 설계 시. 언제 X: Application-level memory tuning (use language-specific profiler 대신).

안티패턴

  • Naive recursive copy: stack overflow 가능 — Cheney 의 queue 방식 사용.
  • Forgetting forward check: 동일 object 두 번 copy → 데이터 corrupt.
  • Pointer 누락: stack/register/global root scan 빠짐 → dangling pointer.
  • Pinning ignored: native pointer 가 from-space object 가리키는 동안 GC → crash.

🧪 검증 / 중복

  • Verified (Cheney 1970 CACM paper, V8/SpiderMonkey source).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — Cheney GC algorithm 의 BFS copy + V8 scavenger 패턴