Skip to main content

Memory Leak

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/memory-leak

In short

A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.

What is a memory leak?

A memory leak happens when a program allocates memory and then loses track of it or keeps an unnecessary reference to it, so the memory is never released. One small leak may not matter, but in a long-running program such as a server, a mobile app, or a browser tab, leaked memory piles up until the program slows down, gets killed by the operating system, or crashes with an out-of-memory error.

In languages with manual memory management, such as C and C++, a leak usually means memory was allocated with malloc or new but never released with free or delete. In garbage-collected languages such as JavaScript, Java, and Python, the garbage collector frees only objects that nothing references anymore, so leaks come from references that stick around by accident. Typical causes are ever-growing caches or global arrays, event listeners and timers that are never removed, and closures that capture large objects.

A memory leak is like a restaurant where diners leave but their tables are never cleared. Each abandoned table seems harmless, yet after a busy evening there is no room left for new guests. Developers find leaks by watching memory usage over time and using heap snapshots in browser developer tools, memory profilers, or leak detectors such as Valgrind and AddressSanitizer.

A memory leak is not the same as high memory usage. A program that uses a lot of memory but stays at a stable level is not leaking, while a leaking program's memory keeps climbing with no upper limit as it runs. When a process exits, the operating system reclaims all of its memory, which is why leaks mainly hurt programs that run for a long time.

Key takeaways

  • A memory leak is memory that stays allocated even though the program no longer needs it.
  • Leaks make memory usage grow steadily over time.
  • Garbage collection does not prevent leaks caused by lingering references.
  • Common causes include unbounded caches, forgotten event listeners, and timers that are never cleared.
  • Heap snapshots and memory profilers help locate the objects that are leaking.

Example

Two common memory leaks in JavaScriptjavascript
// Leak: this cache grows forever because entries are never removed
const cache = new Map();

function getUser(id) {
  if (!cache.has(id)) cache.set(id, loadUser(id));
  return cache.get(id);
}

// Leak: each call adds a new listener that keeps panel alive
function openPanel(panel) {
  window.addEventListener("resize", () => panel.resize());
}

// Fix: cap the cache size and remove listeners when the panel closes

Readers ask

Can a memory leak happen in a garbage-collected language?

Yes. A garbage collector only frees objects that are no longer reachable, so if your code keeps an unneeded reference, for example in a global cache or an event listener, that memory is never reclaimed.

How do I find a memory leak?

First watch the program's memory usage over time to confirm it keeps growing. Then take heap snapshots or use a memory profiler to compare which objects accumulate between snapshots; those objects usually point to the leak.

Does a memory leak last after the program is closed?

No. When a process exits, the operating system reclaims all the memory it used. Leaks matter most in long-running programs, such as servers and apps that stay open for days.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings