Skip to content

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:

Flowchart where a 'References still held?' diamond branches on No to a green 'Reclaimed and reused' box, and on Yes through a yellow reference box to a red 'Memory stays allocated' box.
A leak happens when a lingering reference keeps unneeded memory from being reclaimed.

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.

Memory Management in Python

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.

intermediate python

For additional information on related topics, take a look at the following resources:


By Martin Breuss • Updated Sept. 4, 2026