Memory Leak
Bug in which a program keeps holding memory it no longer needs, so its memory use grows over time and can degrade or crash the system.
A memory leak occurs when a program allocates memory and then fails to release it after the data is no longer needed. Each individual leak may be small, but in a long-running program such as a server, a browser or a background service, leaked memory accumulates. The program's footprint grows steadily until performance suffers as the system starts swapping to disk, the operating system terminates the process or it crashes outright. Restarting the program reclaims the memory, which is why leaks often show up as problems that appear only after hours or days of uptime.
In languages with manual memory management, such as C and C++, the classic leak is memory obtained with malloc or new that is never freed, often because an error path returns early or the last pointer to the memory is overwritten. Garbage-collected languages prevent that kind of leak, but programs can still leak by keeping references to objects they will never use again: an ever-growing cache, event listeners that are never removed or closures that capture large structures. Leaks can involve other resources too, such as file handles, network connections and threads.
Leaks are found with profilers and specialized tools. Valgrind and AddressSanitizer detect unreleased allocations in native programs, while heap snapshots in browser developer tools and Java profilers show which objects are accumulating and what keeps them alive. Preventive techniques include using bounded caches and weak references, testing long-running workloads and tying resources to object lifetimes, a C++ idiom known as RAII. The ownership model of Rust frees memory automatically when its owner goes out of scope, although even Rust does not guarantee freedom from leaks in every case.
Key points
- A memory leak is memory a program no longer needs but never releases, causing usage to grow over time.
- Leaks matter most in long-running software such as servers, browsers and embedded systems.
- Garbage collection prevents forgotten frees but not leaks caused by lingering references to unused objects.
- Tools such as Valgrind, AddressSanitizer and heap profilers help locate leaking allocations.
Related entries
Garbage Collection
Automatic memory management in which a language runtime finds and reclaims memory occupied by objects a program can no longer use.
Pointer
Variable that stores the memory address of another value, letting programs refer to data indirectly and build linked structures.
Debugging
Process of finding, diagnosing and fixing defects in software or hardware that cause it to behave incorrectly or unexpectedly.
Closure
Function bundled with references to variables from the scope where it was created, letting it keep using them after that scope has finished.
Rust
Systems programming language that guarantees memory safety without a garbage collector through its ownership model, first stable in 2015.
C
General-purpose programming language created by Dennis Ritchie at Bell Labs in the early 1970s, foundational for operating systems and embedded software.
C++
General-purpose programming language created by Bjarne Stroustrup as an extension of C, combining low-level control with high-level abstractions.
Random-Access Memory
Fast, volatile computer memory that holds the data and instructions programs are actively using, readable and writable in any order.
Referenced by
Other entries that discuss or point to Memory Leak.