Hunt the Circular Reference: Resolving gc.garbage
Python refcounting frees objects immediately when reference counts hit zero. Cyclic references defeat refcounting, forcing objects to linger until the cyclic garbage collector runs. Replacing parent references with weakref breaks cycles and eliminates memory bloat.
import weakref
class Node:
def __init__(self, name):
self.name = name
self.parent = None
def set_parent(self, p):
self.parent = weakref.ref(p)
# Hunt the Circular Reference: Resolving gc.garbage Standard CPython memory management relies primarily on **reference counting**. When you write `node.child = child` and `child.parent = node`, both objects have a reference count of at least 1 forever, even after you `del root`. 💀🔄 While Python's generational cyclic garbage collector (`gc`) eventually sweeps simple cycles, complex cycles in high-throughput engines cause severe memory bloat and latency spikes. The production fix is `weakref.ref`, which allows backward links without incrementing reference counts. ```python import weakref class Parent: def __init__(self): self.child = None class Child: def __init__(self, parent): self.parent = weakref.ref(parent) # Does NOT increment parent refcount ``` ## Pop Quiz Why does `weakref` prevent memory leaks in bidirectional tree or graph data structures? - [ ] It creates a non-owning pointer that does not increment the target object's reference counter. - [ ] It compresses circular nodes into bytecode stored on the operating system swap partition. - [ ] It forces Python to execute generational garbage collection on every attribute lookup. - [ ] It converts Python dictionaries into immutable static C-level structs. ## Real-World Challenge Implement a `TreeNode` class. Each node holds `name`, `children` (a list of strong references), and `parent` (a `weakref` pointer to its parent node). Verify that when a parent is deleted and garbage collected, invoking `child.get_parent()` returns `None`. <details> <summary>Solution</summary> ```python import weakref import gc class TreeNode: def __init__(self, name: str): self.name = name self.children = [] self._parent_ref = None def add_child(self, child: "TreeNode"): self.children.append(child) child._parent_ref = weakref.ref(self) def get_parent(self): return self._parent_ref() if self._parent_ref else None # Verification root = TreeNode("root") leaf = TreeNode("leaf") root.add_child(leaf) assert leaf.get_parent() is root root_weak = weakref.ref(root) del root gc.collect() # Root should be completely deallocated from memory assert root_weak() is None assert leaf.get_parent() is None ``` </details>