Files
2nd/10_Wiki/Topics/Programming & Language/메모리 누수(Memory Leaks).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

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 작성