memory leak
A memory leak is a defect in which a program keeps holding memory that it no longer needs, so its memory use grows the longer it runs. The leaked memory stays allocated because the program lost every pointer to it or because lingering references prevent garbage collection from reclaiming it:
In C and other languages with manual memory management, a leak usually means code allocated a block and never freed it. Garbage-collected languages allow a subtler version. Objects that stay reachable through an unbounded cache, a global container, or an event listener that’s never unregistered can’t be collected, even though the program will never use them again.
Small leaks often go unnoticed, but in a long-running process such as a web server they accumulate until performance degrades or the program crashes with an out-of-memory error. Heap profilers and memory debuggers such as Valgrind locate leaks by tracking allocations over time.
In Python, reference counting and a cycle collector reclaim unreachable objects automatically, so leaks usually trace back to references kept alive by accident. The standard library’s tracemalloc module helps track down where the surviving memory was allocated.
Related Resources
Tutorial
Memory Management in Python
Get ready for a deep dive into the internals of Python to understand how it handles memory management. By the end of this article, you’ll know more about low-level computing, understand how Python abstracts lower-level operations, and find out about Python’s internal memory management algorithms.
For additional information on related topics, take a look at the following resources:
- Profiling in Python: How to Find Performance Bottlenecks (Tutorial)
- Profiling Performance in Python (Course)
- Caching in Python Using the LRU Cache Strategy (Tutorial)
- Caching in Python With lru_cache (Course)
- Python's del: Remove References From Scopes and Containers (Tutorial)
- How Python Manages Memory (Course)
- Memory Management in Python (Quiz)
By Martin Breuss • Updated Sept. 4, 2026