Skip to main content
CSArchitecture & RuntimeDiagramIntermediate

Memory Leak

KO: 메모리 누수 (Memory Leak)·EN: Memory Leak·JA: メモリリーク

A defect where unneeded memory is never released or remains unintentionally referenced, steadily inflating heap usage until OOM failure

Table of Contents

Structure Diagram

Memory Leak Steady Growth Toward OOM vs Healthy GC Sawtooth
OOM Crash!Uptime (Time) →Heap U
(20, 22)Healthy (GC Sawtooth) #3
Quick Answer

What is Memory Leak?

A defect where unneeded memory is never released or remains unintentionally referenced, steadily inflating heap usage until OOM failure. It is a core concept in Architecture & Runtime within CS.

Core Principles

A Memory Leak occurs when memory that is no longer needed is never freed—either due to missing free() in manual languages or unintended lingering references in garbage-collected runtimes.

  • Spotting a Leak: Healthy apps display a flat sawtooth heap graph after each GC; leaking apps show a steadily climbing post-GC baseline culminating in an OOM kill (Exit 137).

Practical Takeaways

  • Top 4 causes in GC languages:
    1. Unbounded global maps/caches without max size or TTL (always use bounded LRU caches).
    2. Unremoved event listeners, timers, or subscriptions after component unmount.
    3. Closures capturing large outer objects unintentionally.
    4. Blocked goroutines/threads holding stacks and references forever.
  • Use the 3-Snapshot Method: Capture heap snapshots at T1, T2, and T3, then inspect objects allocated between T1 and T2 that are still retained at T3.

Notable Examples

  • Chrome DevTools & Node.js Heap Snapshots: Finding Detached DOM trees and leaked listeners.
  • JVM Eclipse MAT, Go pprof, and C/C++ AddressSanitizer (ASan): Pinpointing retainer chains in production.