A memory leak happens when a program allocates memory and never releases it, or keeps references to data it will never use again, so memory consumption grows steadily over time.
Why it matters
Long-running processes such as web servers, browsers and background workers are the most exposed. A small leak per request becomes gigabytes after days, ending in slowdowns, swapping or an out-of-memory kill by the operating system. Restarting the process hides the symptom, which is why a service that "needs a restart every few days" often has a leak. In languages with manual memory management a leak means forgetting to free; in garbage-collected languages it usually means an unintended reference keeps objects alive.
Example
const listeners = [];
function onRequest(req) {
listeners.push(() => console.log(req.body)); // never removed
}
Each request adds a closure that holds its body forever. The array grows without limit and the garbage collector cannot free any of it.
Common mistakes
- Adding event listeners, timers or cache entries without ever removing them.
- Using an unbounded in-memory cache with no size limit or expiry.
- Blaming the garbage collector instead of looking for what still references the objects.
Related
Get notified when this term updates
Plus occasional practical tips on uptime and monitoring, nothing else. Unsubscribe anytime.
This page is general information written by our editorial team and updated when documentation changes. Servers and providers differ, so test changes safely first. See our editorial policy and disclaimer.