Python gc Module Internals — ELI5

Imagine you are building with blocks. Every time you need a new block, you pull one from a big box. When you are done with a block, you should put it back so others can use it. But sometimes you forget.

Python has a tiny helper that watches which blocks you are using. Every block has a counter that says “2 people are using me” or “1 person is using me.” When the counter hits zero — nobody needs it — the helper puts the block back in the box immediately. This counting system is called reference counting, and it handles most of the cleanup.

But there is a tricky situation. What if block A says “I need block B” and block B says “I need block A”? Both counters stay at 1 forever, even though nobody else cares about either block. They are like two friends holding hands in a circle, keeping each other from being put away. This is called a reference cycle.

That is where the gc module comes in. It is a second helper that periodically looks for these hand-holding circles. It checks: “Can anyone outside the circle still reach these blocks?” If not, it breaks the circle and puts all those blocks back.

You can talk to this second helper through the gc module. You can ask it to clean up right now instead of waiting. You can ask it how many forgotten blocks it found. You can even tell it to be more or less aggressive about checking.

Most of the time, you never think about it — it just works. But when your program uses a lot of memory and you cannot figure out why, the gc module helps you investigate.

The one thing to remember: Python’s gc module is the cycle-detecting garbage collector that catches memory leaks caused by objects referencing each other in circles.

pythonmemory-managementinternals

See Also

  • Python Ast Module Code Analysis How Python's ast module reads your code like a grammar teacher diagrams sentences — turning source text into a tree you can inspect and change.
  • Python Dis Module Bytecode How Python's dis module lets you peek at the secret instructions your computer actually runs when it executes your Python code.
  • Python Importlib Custom Loaders How Python's importlib lets you teach Python to load code from anywhere — databases, zip files, the internet, or even generated on the fly.
  • Python Site Customization How Python's site module sets up your environment before your code even starts running — the invisible first step of every Python program.
  • Python Startup Optimization Why Python takes a moment to start and what you can do to make your scripts and tools launch faster.