Skip to content
Latchkey

What Is a Memory Leak? Memory That Never Comes Back

A memory leak happens when a program holds onto memory it no longer needs, so its memory usage grows over time until it runs out.

A memory leak is not a dramatic crash at first; it is a slow climb. The program keeps allocating memory and never lets some of it go, so usage creeps upward. Eventually the process exhausts available memory and crashes or gets killed. Leaks happen in both manual-memory languages (you forget to free) and garbage-collected ones (you keep a reference alive).

What a leak really is

A leak is memory that is still allocated but will never be used or freed again. In manual languages it is an allocation with no matching free; in garbage-collected languages it is an object the collector cannot reclaim because something still references it.

Common causes

  • Growing caches or lists that are never trimmed.
  • Event listeners or subscriptions that are added but never removed.
  • Closures that capture and retain large objects.
  • Global registries that accumulate entries over the program lifetime.

Why leaks are sneaky

A leak rarely breaks anything immediately. A short-lived program may finish before the leak matters. A long-running server, though, will slowly degrade and eventually fall over, often hours after the offending code ran.

Finding leaks

Heap snapshots and memory profilers show which objects keep growing and what holds references to them. Tracking memory over a long run, then looking for a steady upward trend, is the usual way to confirm a leak exists.

A quick example

Pushing items into a module-level array on every request, without ever removing them, leaks memory: the array grows forever and nothing it holds can be collected, even though those items are never read again.

Memory leaks in CI

A leaky test suite can cross the runner memory limit and get OOM-killed, often only on large runs, which looks like a flaky failure. Larger runners (Latchkey) delay the symptom and auto-retry transient OOM kills, but a persistent leak still needs fixing, since more memory only postpones the crash.

Key takeaways

  • A memory leak is allocated memory that is never released, so usage grows over time.
  • Leaks happen in both manual and garbage-collected languages, often via lingering references.
  • In CI they surface as OOM kills, frequently mistaken for flaky failures.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →