CPython Memory Management and the GIL
Intro
CPython manages memory with reference counting and a generational garbage collector (GC). It also has the Global Interpreter Lock (GIL), which lets only one thread run Python bytecode at a time. In this post, I look at how each of them works and why CPython uses them together.
The Life Cycle of a Python Object
In Python, specifically the CPython implementation, managing memory is less about “deleting files” and more about managing labels. A common mistake is to think that the del statement destroys an object. In fact, del only removes the binding between a name and an object. Look at this classic example:
a = [1, 2, 3]
b = a
del a
In this case, the name a is gone. But the list [1, 2, 3] is still fully intact because b still refers to it. Whether the object survives depends entirely on its Reference Count. CPython keeps an internal counter for every object. When you assign b = a, the count goes up. When you call del a, it goes down. Only when that count reaches zero is the object destroyed, and this happens immediately.
This “immediate destruction” is a characteristic of CPython. As soon as the last reference is gone, the memory is reclaimed. Right before the object is destroyed, CPython calls the __del__ method if it’s defined. It is very important to remember that __del__ is a hook. It is not a destructor you should call yourself. It’s meant for cleaning up external resources like open files or network sockets. Relying on it for core logic is risky because, as we’ll see, Circular References can cause problems.
class A:
pass
a = A()
b = A()
a.other = b # a references b
b.other = a # b references a
del a
del b
Here, we have a kind of “deadlock”. Even after del a and del b, the two objects still point at each other. Their reference counts will never reach zero. This creates an “island” in your memory that cannot be reached. To solve this, CPython uses a second mechanism: the Generational Garbage Collector (GC). The GC periodically scans object graphs to find these isolated islands of circular references and clears them out. It organizes objects into three “generations” (0, 1, and 2). It checks younger objects more often than older objects that have lived a long time.
Note that this specific behavior, especially the “immediate” destruction, is particular to CPython. Other implementations do not guarantee that an object will be destroyed the second it becomes unreachable. Examples are PyPy (which uses a tracing GC) and Jython (which relies on the JVM’s garbage collector).
This dual system uses Reference Counting for speed and Generational GC for thoroughness. If you understand it, you can write more efficient code. You can also avoid the common problems of memory leaks and unpredictable __del__ behavior.
The Global Interpreter Lock (GIL): CPython’s Necessary Shield
At its core, the Global Interpreter Lock (GIL) is a mutex. It allows only one thread to control the Python interpreter at any given time. This means that a single CPython process will only execute one line of Python bytecode at a time. This is true even on a machine with 64 CPU cores. This design choice comes directly from CPython’s use of reference counting for memory management. The internal object model is not “thread-safe”. This means two threads could try to increment or decrement an object’s reference count at the same time. Because of this, the GIL was implemented to prevent race conditions and memory corruption. It was a trade-off. CPython gave up multi-core parallelism to get a simple, fast, and robust implementation for single-threaded tasks.
In practice, the impact of the GIL depends entirely on what your code is doing.
// Only one lock -> Only one CPU calculates!
CPU1: [ Calc ] ---> [ Idle ]
CPU2: [ Idle ] ---> [ Calc ] ---> [ Idle ] ---> [ Calc ]
CPU3: [ Idle ] -------------------> [ Calc ] ---> [ Idle ]
For CPU-bound workloads, the GIL is a significant bottleneck. Examples are heavy mathematical computations or image processing entirely in Python. The threads constantly compete for the lock. So adding more threads can actually slow down your program because of the overhead of context switching.
CPU1: [ CPU ] ---> [R/W (GIL Released)]
CPU2: [ Idle ] ---> [ CPU ] ---> [R/W (GIL Released)]
CPU3: [ Idle ] ---------------> [ CPU ] ---> [R/W (GIL Released)]
However, for I/O-bound workloads, the GIL is much less of a problem. Examples are web scraping, handling network requests, or reading from a disk. CPython is smart enough to release the GIL while a thread is waiting for data to arrive from the outside world. This allows other threads to run in the meantime. As a result, multithreading is highly effective for concurrent tasks that spend most of their time waiting.
It is a common mistake to think the GIL is a fundamental limitation of the Python language. In fact, it is strictly an implementation detail of CPython. Other versions of Python, such as Jython (running on the JVM) or IronPython (running on .NET), do not have a GIL. This is because their underlying runtimes provide their own memory management and thread-safety mechanisms. Even within the CPython ecosystem, developers have found clever ways to bypass the lock. Powerful libraries like NumPy or Pandas run their heaviest work in C extensions. These extensions can use Py_BEGIN_ALLOW_THREADS to release the GIL. They can then perform massive parallel calculations on multiple cores. They can take the lock again only when they need to talk back to Python objects.
Py_BEGIN_ALLOW_THREADS
// long computations
Py_END_ALLOW_THREADS
In the end, the GIL remains because removing it is known to be very difficult. The hard part is to avoid making single-threaded code significantly slower. Single-threaded code makes up the vast majority of Python scripts. The GIL represents a practical design philosophy. It puts simple C extension integration and single-thread speed ahead of theoretical multi-core perfection. If you truly need to use every core of your CPU in pure Python, the standard recommendation is not to fight the GIL with threads. Instead, it is to use the multiprocessing module, which avoids the lock by starting entirely separate interpreter processes.
The CPython Synergy: Balancing Simplicity and Performance
Reference Counting and the GIL are linked by necessity. CPython uses RC as its primary memory management tool. Because of this, every single variable assignment or function call changes an object’s reference counter. If the GIL did not exist, CPython would need “fine-grained locking”. This essentially means putting a tiny lock on every single object. These locks would make sure two threads don’t update the same counter at once. This would create massive overhead and make single-threaded Python significantly slower. CPython uses one “Global” lock instead of millions of “local” locks. In this way, it remains extremely fast and efficient for the vast majority of Python applications, which are single-threaded.
#define Py_DECREF(op) \\
do { \\
if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \\
--((PyObject*)(op))->ob_refcnt != 0) \\
_Py_CHECK_REFCNT(op) \\
else \\
_Py_Dealloc((PyObject *)(op)); \\
} while (0)
The Advantages: Why the Duo Wins
The main advantage of this combination is simplicity and stability. Developers rarely have to worry about memory corruption or complex thread-safety issues when writing standard Python code. This design is also the reason the Python ecosystem has so many C extensions. Writing a C library for Python is relatively simple. This is because the programmer can rely on the GIL to protect the state of the interpreter. This “ease of integration” is exactly why major libraries like NumPy, TensorFlow, and PyTorch chose Python. They can do the heavy work in parallel C code. At the same time, they rely on the GIL to keep the Python interface stable and easy to use.
The Disadvantages: The Cost of the Shield
However, this architectural choice has a clear trade-off: the Multi-core Bottleneck. Today, even smartphones have octa-core processors. But CPython cannot run pure Python code in parallel across multiple CPUs, and this can feel outdated. Some tasks are CPU-intensive, like calculating prime numbers or complex data processing in pure Python. For these tasks, the GIL lets only one thread execute at a time, even with multithreading. This leads to wasted hardware potential. In addition, the Generational GC handles circular references, but it does so in a stop-the-world way. Combined with the GIL, this can occasionally cause minor “stutters” in high-performance, real-time applications.
Conclusion
In the end, the combination of Reference Counting and the GIL represents CPython’s pragmatic philosophy. It puts the “common case” ahead of the “edge case”. The common case is fast single-threaded execution and easy integration with C. The edge case is multi-core parallelism in pure Python. Modern projects like Free-threaded Python (PEP 703) are currently working to make the GIL optional. But for decades, this combination has provided stability. That stability allowed Python to become the world’s most popular language for data science and web development. The GIL exists to protect the speed of Reference Counting. If you understand this, you can better decide when to use threads for I/O. You can also better decide when to switch to multiprocessing or C-extensions for raw power.