Memory Leak
- In Turkish
- bellek sızıntısı
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
// 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 closesReaders 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
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- Virtual MemoryOperating Systems, p. 37Virtual memory is an operating system technique that gives each process its own private address space and maps it to physical RAM or disk behind the scenes.
- ProcessOperating Systems, p. 23A process is a running instance of a program, with its own memory space, resources, and at least one thread of execution managed by the operating system.
- ClosureProgramming Fundamentals, p. 10A closure is a function that remembers the variables from the scope where it was created, so it can keep using them even after the outer function has returned.
- ObservabilityDevOps & Cloud, p. 38Observability is the ability to understand what is happening inside a running software system by collecting and analyzing its logs, metrics, and traces.
- Heap MemoryOperating Systems, p. 14Heap memory is the region for data a program allocates at runtime, whose size or lifetime isn't known in advance and can outlive the function that made it.
Spotted a mistake or something missing on this page?Suggest an edit