In programming, a memory leak happens when a program allocates a block of memory but then fails to release that memory when it’s no longer needed. The program “loses track” of the memory, or otherwise holds onto it, preventing the operating system from reclaiming it for other uses. This accumulated, unused memory can cause the application (and potentially the entire system) to run out of resources.
Python has an automatic memory management system called a garbage collector.
- Reference counting: Every object in Python has a reference count, which is the number of variables or data structures that are currently “pointing” to it. When an object’s reference count drops to zero, Python’s garbage collector automatically deallocates that object, freeing its memory.
- Generational Cyclic Garbage Collector: This secondary collector deals with a specific problem reference counting can’t solve: circular references.
Unintended Global References or Long-Lived Data Structures
Closures Capturing Large Objects (Subtle Leak)
(Global Interpreter Lock) GIL is the unique aspect of CPython (the most common implementation of Python), the GIL simplifies memory management in C Python API. When a Python thread wants to execute Python bytecode, it must acquire the GIL. It holds the GIL for a certain duration (e.g., a fixed number of bytecode instructions or until it performs an I/O operation). After that, it releases the GIL, allowing another thread to acquire it. This rapid switching creates the illusion of parallelism, but it’s actually just highly concurrent execution.
Memory leak is like a hoarder: they keep collecting stuff and never throw anything away. A memory leak is when heap memory is allocated but never released,
leaky_list = []
def handle_request(data):
leaky_list.append(data) # never remove old items!
Stack overflow is like walking down a spiral staircase forever — eventually, you fall.
def recurse():
return recurse()
recurse()
The stack and heap are not in the same physical location in memory — they’re typically in different regions of a process’s memory space, and they grow in opposite directions.