Files
2nd/10_Wiki/Topics/Domain_Programming/Programming & Language/메모리 누수(Memory Leaks).md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

5.5 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-메모리-누수-memory-leaks 메모리 누수(Memory Leaks) 10_Wiki/Topics verified self
Memory Leak
Heap Leak
Leak Detection
none A 0.9 applied
memory
debugging
performance
gc
profiling
2026-05-10 pending
language framework
C/C++/Java/JS/Python Valgrind/ASan/Heaptrack/Chrome DevTools

메모리 누수(Memory Leaks)

매 한 줄

"매 alive 인데 매 unused, 매 unused 인데 매 free X". 매 manual mem (C/C++) 의 free 누락, 매 GC mem (Java/JS) 의 unintended retention. 매 long-running process 의 매 가장 흔한 incident, 매 2026 도구는 매 ASan/heaptrack/V8 heap snapshot/JFR + AI-assisted root cause.

매 핵심

매 두 의미

  • 매 Manual leak: 매 free X — 매 OS 가 매 process 종료 시 매 회수.
  • 매 GC leak: 매 root 에서 매 reachable 이지만 매 logically dead — 매 unintended retention.

매 흔한 원인

  1. 매 Cache 의 unbounded growth.
  2. 매 Listener / observer 의 unsubscribe 누락.
  3. 매 Closure 의 unintended capture (JS).
  4. 매 Static collection (HashMap) 의 누적.
  5. 매 ThreadLocal 의 cleanup 누락 (JVM/web).
  6. 매 Native handle (file/socket) 의 close 누락.
  7. 매 Circular ref + RC (Python before GC, Swift, ObjC ARC).

매 detection 의 axes

  • 매 Symptom: 매 RSS 증가, 매 OOM, 매 GC 빈도 ↑, 매 latency drift.
  • 매 Tool: 매 sampling profiler, 매 heap snapshot, 매 allocation tracking.

패턴

C++ Valgrind

g++ -g -O0 main.cpp -o app
valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./app
# 매 "definitely lost" 의 우선 fix

C++ AddressSanitizer + LeakSanitizer

g++ -fsanitize=address,leak -g main.cpp -o app
ASAN_OPTIONS=detect_leaks=1 ./app

C++ RAII (matter of design)

class FileHandle {
    FILE* f;
public:
    explicit FileHandle(const char* p) : f(fopen(p, "r")) {}
    ~FileHandle() { if (f) fclose(f); }
    FileHandle(const FileHandle&) = delete;
    FileHandle& operator=(const FileHandle&) = delete;
};
// 매 std::unique_ptr / std::shared_ptr 가 매 default

JS — listener 누락

// 매 BAD — node 가 떠도 listener 가 retain
function attach(el) {
  const handler = () => doStuff(el);
  el.addEventListener("click", handler);
}

// 매 GOOD
function attach(el) {
  const handler = () => doStuff(el);
  el.addEventListener("click", handler);
  return () => el.removeEventListener("click", handler);
}

JS — closure 의 unintended capture

// 매 BAD — 매 huge 가 매 outer scope 에 retained
function setup() {
  const huge = new Array(1e7).fill(0);
  const small = huge[0];
  return () => small;
}

// 매 GOOD
function setup() {
  const small = (() => {
    const huge = new Array(1e7).fill(0);
    return huge[0];
  })();
  return () => small;
}

Java — static map leak

// 매 BAD
public class Cache {
    static final Map<Key, Value> M = new HashMap<>();
}

// 매 GOOD — Caffeine size cap + eviction
import com.github.benmanes.caffeine.cache.*;
Cache<Key, Value> c = Caffeine.newBuilder()
    .maximumSize(10_000).expireAfterWrite(Duration.ofMinutes(10)).build();

Java heap dump + MAT

jcmd <pid> GC.heap_dump /tmp/heap.hprof
# 매 Eclipse MAT 에서 dominator tree / leak suspect

Chrome DevTools — heap snapshot diff

1. baseline snapshot
2. suspect action 반복 (e.g. open/close modal x10)
3. second snapshot
4. "Comparison" mode, 매 retained 증가 식별
5. retainer chain 의 root 추적

Python tracemalloc

import tracemalloc
tracemalloc.start(25)
snap1 = tracemalloc.take_snapshot()
# ... 매 action 반복 ...
snap2 = tracemalloc.take_snapshot()
for stat in snap2.compare_to(snap1, 'lineno')[:10]:
    print(stat)

Linux heaptrack (C/C++ low-overhead)

heaptrack ./app
heaptrack_gui heaptrack.app.<pid>.gz

매 결정 기준

Stack Tool
C/C++ dev ASan + LeakSanitizer
C/C++ prod heaptrack / jemalloc profiler
Java JFR + heap dump + MAT
Node.js --inspect + Chrome heap snapshot
Browser DevTools Memory tab
Python tracemalloc / objgraph / memray
Go pprof (heap profile)

기본값: 매 first symptom = RSS chart, 매 second = heap snapshot diff, 매 third = retainer/dominator analysis.

Graph

  • 부모: Memory Management · Performance
  • 응용: Cache Eviction · WeakRef
  • Adjacent: Garbage Collection · Reference Counting · RAII · OOM

LLM 활용

언제: 매 leak 패턴 식별, 매 fix 제안 (RAII, weak ref, eviction policy), 매 heap dump 분석 hint. 언제 X: 매 production heap 의 직접 access, 매 sensitive customer data dump.

안티패턴

  • 매 OOM 시 -Xmx ↑: 매 leak 을 매 늦출 뿐.
  • 매 try/finally 없는 close: 매 try-with-resources / RAII / context manager 사용.
  • 매 Strong cache without eviction: Caffeine / LRU.
  • 매 Listener pattern 의 no unsubscribe: weakRef / dispose.
  • 매 GC tuning 으로 leak 감추기: root cause X.

검증 / 중복

  • Verified: Valgrind manual, ASan docs, V8 blog, JFR docs, tracemalloc PEP, heaptrack docs.
  • 신뢰도 A.

Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — leak detection per-stack/anti-patterns/RAII 작성