AlgoMaster Logo

Garbage Collection

Medium Priority20 min readUpdated June 6, 2026

Reference counting frees most dead objects the instant they become unreachable, but it has one blind spot: cycles. A Customer holding a list of Order objects where each order points back at the customer never reaches refcount zero, even when the rest of the program has dropped both. CPython's cycle collector, exposed through the gc module, cleans these up. This lesson walks through how the collector works, the knobs available, when to call it manually, and what to do when memory leaks anyway.

Premium Content

Subscribe to unlock full access to this content and more premium articles.