What Is a Memory Leak?
A memory leak happens when a program keeps allocating memory it no longer needs without releasing it, causing memory usage to grow continuously over time.
Published October 7, 2026
A memory leak is when a program keeps memory it no longer needs, so usage grows over time until it slows down or crashes. In garbage-collected languages it happens when objects stay reachable by mistake.
The concept
Typical causes: ever-growing caches, event listeners never removed, timers that are never cleared, global arrays and closures capturing large objects.
The symptom is steadily rising memory in monitoring that drops only on restart.
Memory over time
healthy: ▁▃▅▃▁▃▅▃▁ (rises and falls)
leak: ▁▂▃▄▅▆▇█ ✗ (only climbs)
Practical examples
Leaking listener
function mount() {
window.addEventListener('resize', onResize) // never removed
}Fix: clean up
useEffect(() => {
window.addEventListener('resize', onResize)
return () => window.removeEventListener('resize', onResize)
}, [])Unbounded cache
const cache = {}
function get(k) { return cache[k] ??= load(k) } // grows foreverFind it
node --inspect app.js # DevTools → Memory → heap snapshots, compareCommon mistakes
- Forgetting clearInterval and removeEventListener.
- Caches without size limits or expiry.
- Ignoring the slow climb until production crashes.
Key takeaways
- Leak = memory kept but unused.
- Always clean up listeners and timers.
- Compare heap snapshots to locate it.
FAQ
Do garbage-collected languages leak?
Yes, through references that are still reachable.
How can I prevent cache leaks?
Use LRU limits, TTLs or WeakMap/WeakRef.