Skip to content
IT glossary
ProgrammingUpdated December 3, 2026

Memory leak

Утечка памяти

A program has a memory leak when it keeps memory that it no longer needs instead of making it available for reuse. Repeated leaks can eventually cause the program to run out of memory.

What counts as a memory leak?

A temporary spike while processing a large file isn't necessarily a leak. The key question is what happens when the work is done. If a server finishes a request but keeps its request data indefinitely by mistake, that memory is leaking. A single allocation can leak without making a visible dent in a graph; leaks that repeat with every request or screen transition are easier to spot and more likely to cause outages.

Two ways memory gets left behind

In C, a common failure is allocating a block with malloc and returning from a function without calling free or handing ownership to another part of the program. If that return loses the last pointer to the block, the program no longer has a way to free it.

With a tracing garbage collector, a common failure is keeping a reference you meant to remove. The collector can reclaim unreachable objects, but it doesn't know which reachable objects the application still needs. A long-lived object such as an event emitter can keep a callback alive after the feature that registered it has shut down. In turn, that callback can keep everything it closes over alive.

Example: a listener outlives a view

function watchSize(panel) {
  const update = () => {
    panel.textContent = String(window.innerWidth);
  };
  window.addEventListener('resize', update);
  return () => window.removeEventListener('resize', update);
}

watchSize returns a cleanup function. The code that closes the view should call it as part of teardown. Removing panel from the DOM isn't enough: window still holds the listener, and the listener's closure still holds the panel. If each new view creates a fresh panel and skips cleanup, old panels can pile up.

How to investigate

Pick a repeatable action and compare memory at equivalent points, such as just after closing a view and letting background work finish. If heap snapshots show an increasing number of objects that should have gone away, inspect their retaining paths: the chains of references that keep them reachable. If process memory grows while the managed heap stays stable, look at native allocations too.

Process memory alone is not proof of a leak. An allocator can keep freed memory for future allocations rather than immediately returning it to the operating system. A bounded cache can also grow during warm-up and then level off. Conversely, an unbounded cache with no useful reason to keep old entries can behave like a leak even though the entries are reachable by design.

What interviewers look for

When walking through a suspected leak, identify what should end the object's lifetime and what actually keeps it alive. For the listener example, propose cleanup at teardown, then explain how you'd verify the fix: repeat the open-and-close cycle and check that old panels no longer accumulate.