Files
2nd/10_Wiki/Topic_Programming/Architecture/Mark-Sweep.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

5.2 KiB

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-mark-sweep Mark-Sweep GC 10_Wiki/Topics verified self
Mark and Sweep
Tracing GC
M&S
none A 0.9 applied
gc
memory
runtime
algorithm
2026-05-10 pending
language framework
multi jvm-v8-cpython

Mark-Sweep GC

매 한 줄

"매 reachable 만 살리고 — 매 나머지는 sweep.". Mark-Sweep 은 매 1960년 McCarthy 가 Lisp 위해 발명한 매 tracing GC algorithm. 매 root 부터 reachable object 를 mark 하고 매 unmarked 를 sweep. 매 reference counting 의 cycle 문제 해결 — 매 modern GC (V8, JVM, CPython generational) 의 base.

매 핵심

매 2-phase

  1. Mark: 매 GC root (stack, globals, registers) 부터 매 graph traverse, 매 reachable 에 mark bit 1.
  2. Sweep: 매 heap 전체 scan, 매 unmarked object 를 free list 에 추가.

매 GC root

  • 매 stack 의 local variable.
  • 매 global / static variable.
  • 매 CPU register.
  • 매 JNI / native handle.

매 변형

  • Mark-Compact: sweep 대신 매 살아있는 object 를 압축 → 매 fragmentation 해결.
  • Tri-color: white / grey / black — 매 incremental / concurrent 가능.
  • Generational: young (Eden + Survivor) / old — 매 most objects die young 가설.

매 stop-the-world (STW)

  • 매 mark phase 동안 매 mutator (app thread) 정지 — 매 reference graph 일관성.
  • 매 concurrent / incremental GC 는 매 write barrier 로 STW 최소화.

💻 패턴

Pseudocode mark

def mark(obj):
    if obj is None or obj.marked:
        return
    obj.marked = True
    for ref in obj.references:
        mark(ref)

def gc():
    # 1. Mark
    for root in get_roots():
        mark(root)
    # 2. Sweep
    for obj in heap:
        if obj.marked:
            obj.marked = False  # reset for next cycle
        else:
            free(obj)

Tri-color iterative mark

WHITE, GREY, BLACK = 0, 1, 2

def mark_iterative():
    # Initially all WHITE except roots → GREY
    grey = list(get_roots())
    for r in grey:
        r.color = GREY

    while grey:
        obj = grey.pop()
        for ref in obj.references:
            if ref.color == WHITE:
                ref.color = GREY
                grey.append(ref)
        obj.color = BLACK

    # Sweep: WHITE → free, BLACK → reset to WHITE
    for obj in heap:
        if obj.color == WHITE:
            free(obj)
        else:
            obj.color = WHITE

V8 (JavaScript) GC config

// Inspect V8 GC behavior
node --trace-gc app.js
// → [GC] Scavenge ... Mark-sweep ...

// Tune heap size
node --max-old-space-size=4096 app.js  // 4GB

// Trigger GC manually (testing only)
node --expose-gc -e "global.gc()"

JVM G1GC vs ZGC

# G1GC (default since Java 9): regional mark-sweep with compaction
java -XX:+UseG1GC -Xmx8g -XX:MaxGCPauseMillis=200 App

# ZGC (Java 21+): concurrent, sub-ms pause
java -XX:+UseZGC -Xmx32g App
# → most work concurrent with mutator, STW phases <1ms

CPython gc module

import gc

# CPython uses refcount + mark-sweep (for cycles only)
gc.collect()  # force cycle collection
gc.get_count()  # (gen0, gen1, gen2) — generational
gc.set_threshold(700, 10, 10)  # tune trigger

# Detect cycles
import objgraph
objgraph.show_most_common_types(limit=20)

Write barrier (concurrent GC)

// When mutator writes a pointer, GC must know
// (so concurrent mark doesn't miss new references)
void write_field(Object* obj, int idx, Object* val) {
    if (gc_is_marking && obj->color == BLACK && val->color == WHITE) {
        val->color = GREY;
        grey_queue_push(val);
    }
    obj->fields[idx] = val;
}

매 결정 기준

상황 GC choice
Throughput 중요, latency 무관 Parallel GC (JVM)
균형 (default) G1GC (JVM), V8 default
매 sub-ms pause ZGC (JVM 21+), Shenandoah
Embedded / RT Manual / arena allocator
Functional language Generational copying (OCaml, Erlang per-process)

기본값: 매 generational + concurrent mark-sweep (G1, V8 Orinoco) — 매 modern runtime 의 standard.

🔗 Graph

🤖 LLM 활용

언제: GC algorithm 설계, runtime 선택, GC tuning 의 이론적 basis 가 필요할 때. 언제 X: application-level memory leak 추적 — 매 Memory_Leaks 가 더 직접적.

안티패턴

  • "GC = no leaks": 매 reachable leak 이 매 GC lang 의 가장 흔한 issue.
  • 잦은 manual gc(): 매 runtime 의 heuristic 보다 못함, 매 throughput 떨어뜨림.
  • 거대 heap: 매 mark phase 가 heap size 에 비례 — 매 큰 heap = 큰 pause (non-concurrent GC).
  • Finalizer 의존: 매 unpredictable timing — 매 RAII / try-with-resources 가 정답.

🧪 검증 / 중복

  • Verified (McCarthy 1960, "GC Handbook" Jones et al, V8 blog, OpenJDK docs).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — 2-phase + tri-color + V8/JVM/CPython 패턴