Garbage Collection in CPython01:45 PM - 02:10 PM on August 15, 2015, Room 701
- Audience level:
This talk is an overview of CPython's approach to memory management - how the life cycle of an object progresses, how objects are flagged for collection, and the tools that Python provides for manipulating the garbage collector's behavior. Along the way, we'll explore how to learn about Python language features through experimentation.
First, we'll explore what the role of a garbage collector is. We'll touch on the two major types of garbage collectors, tracing vs. reference counting, and convince ourselves that Python uses the latter through code that behaves differently under the two models.
We'll explore how the del statement works, and when it might not have the behavior one expects. (Hint: if objects are deleted when their reference counts hit 0, what happens if you try to delete an object with more than one reference?) In exploring this, we'll encounter __del__ and its ability to provide a window into the behavior of objects when they're being garbage collected.
We'll then look at the gc module, when manual memory management might be required and how it works in Python, and which aspects of Python garbage collection are implementation specific. We'll take a digression into why CPython's garbage collection features led to the development of the global interpreter lock. Along the way, we'll see an interesting interaction between the depth of the call stack and CPython's specific garbage collection implementation, and construct a simple fractal whose maximum size actually elucidates the value of a constant in the CPython source code.
Finally, we'll use the garbage collector's behavior to implement depth-first search, and solve the eight queens problem using the Python finalizer.