Memory Leak
ELI5 — The Vibe Check
A memory leak is when your program keeps grabbing more memory but never gives it back, like filling a bathtub without a drain. Eventually you run out of water (RAM) and everything slows down or crashes. It is sneaky because things work fine at first, then gradually get worse.
Real Talk
A memory leak occurs when a program allocates memory that is never released back to the OS or runtime. In managed languages (JavaScript, Python), leaks happen through unintended references that prevent garbage collection — such as event listeners that are never removed or closures holding large objects. In C/C++, leaks occur from forgetting to call free(). Long-running processes are most affected.
Show Me The Code
// Classic JS memory leak: event listener never removed
function setup() {
const bigData = new Array(1000000).fill("data");
document.addEventListener("click", () => {
console.log(bigData[0]); // bigData can NEVER be GC'd
});
}
// Fix: store the listener and removeEventListener when done
When You'll Hear This
"The server runs fine for an hour then crashes — probably a memory leak." / "Profile it with Chrome DevTools to find the memory leak."
Related Terms
Debug
Debugging is the process of finding and fixing the gremlins in your code. Something is broken, and you need to play detective — adding clues (console.
Garbage Collection (GC)
Garbage collection is your programming language's automatic cleanup crew. When you create variables and objects, they take up memory.
Runtime
Runtime is the environment where your code actually runs.