advanced

Garbage Collection in Python

By DataPlayArena Technical Review BoardUpdated 2026-07-209 min readRuntime Verified

Overview

Memory management in Python (specifically the reference CPython implementation) is automated. Developers rarely need to manually allocate or deallocate memory buffers. However, understanding how CPython’s dual memory management system works—combining Reference Counting with a Generational Cyclic Garbage Collector—is essential for avoiding severe memory leaks in long-running data pipelines, preventing memory fragmentation, and optimizing throughput.

What You Will Learn

In this lesson, you will master:
  • Reference Counting Mechanics: How PyObject_HEAD tracks live references and deallocates memory instantly when count reaches zero.
  • The Reference Cycle Problem: Why reference counting alone fails for self-referential structures (e.g., graphs, doubly linked lists).
  • Generational Garbage Collection: How Python’s cyclic GC divides objects into Generation 0, 1, and 2 based on survival heuristics.
  • The gc Module: Tuning GC thresholds, diagnosing uncollectable objects, and running manual collection cycles.
  • Weak References (weakref): Referencing cached objects without artificially extending their lifetime.

Core Concepts: Two-Tier Memory Management

Python uses two complementary systems to reclaim memory:

Tier 1: Reference Counting (Primary Deallocator)

Every Python object (PyObject in C) contains a header field named ob_refcnt. Whenever an object is referenced (assigned to a variable, passed to a function, or added to a list), its reference count increments by 1. Whenever a reference falls out of scope or is deleted with del, its count decrements by 1.

python
import sys

# Reference count starts at 1
data = ["payload_1", "payload_2"]
print(sys.getsizeof(data))

# Assigning another reference increments count
alias = data
print(f"References: {sys.getrefcount(data) - 1}")  # (getrefcount adds 1 temporary reference)

# Deleting the alias decrements count
del alias

# Deleting the final reference drops count to 0 -> Deallocated IMMEDIATELY!
del data

Advantage: Deterministic, instant deallocation the moment the object is no longer needed. No "stop-the-world" pause required for standard variables.


Tier 2: Generational Cyclic Garbage Collector (Cyclic Reclaimer)

Reference counting has one fatal flaw: reference cycles. If Object A references Object B, and Object B references Object A, neither reference count can ever reach zero on its own, even if both objects become completely unreachable from the root program scope:

python
class Node:
    def __init__(self, name):
        self.name = name
        self.partner = None

node1 = Node("Node A")
node2 = Node("Node B")

# Create a cyclic reference
node1.partner = node2
node2.partner = node1

# Sever root references
del node1
del node2

# Both nodes are now unreachable by your code!
# However, node1.partner keeps node2 alive (refcnt=1),
# and node2.partner keeps node1 alive (refcnt=1).
# Reference counting CANNOT free them.

To solve this, Python runs a Cyclic Garbage Collector in the background that periodically scans container objects (lists, dicts, instances, tuples) using graph-traversal algorithms to identify and isolate unreferenced cyclic islands.


Generational Garbage Collection: Generations 0, 1, and 2

CPython's cyclic GC is based on the weak generational hypothesis: most objects die young. Objects are sorted into three distinct generations:

| Generation | Purpose | Collection Frequency | | :--- | :--- | :--- | | Generation 0 | Newly allocated objects | Most frequent (triggers when allocations exceed threshold) | | Generation 1 | Survivors of Gen 0 collection | Moderate frequency | | Generation 2 | Long-lived survivors of Gen 1 (e.g., globals, caches) | Infrequent |

python
import gc

# Inspect GC thresholds: (Gen0 threshold, Gen1 threshold, Gen2 threshold)
print(gc.get_threshold())
# Default output: (700, 10, 10)
# Meaning: When (allocations - deallocations) > 700 in Gen 0, run Gen 0 collection.
# For every 10 Gen 0 collections, run 1 Gen 1 collection.
# For every 10 Gen 1 collections, run 1 Gen 2 collection.

The weakref Module: Breaking Cycles in Caches

A weak reference allows you to reference an object without incrementing its ob_refcnt. If only weak references to an object remain, Python deallocates the object immediately:

python
import weakref

class LargeDataFrameCache:
    def __init__(self, name):
        self.name = name

# Create object
raw_df = LargeDataFrameCache("Q3_Financials")

# Create a weak reference
weak_handle = weakref.ref(raw_df)

print(weak_handle())  # Accessible: <LargeDataFrameCache object at ...>

# Delete strong reference
del raw_df

# The object is deallocated instantly!
print(weak_handle())  # Output: None

Weak references are widely used in LRU caches, event listeners, and parent-child tree graphs.


Common Mistakes & Gotchas

  • Disabling GC Without Measuring: In ultra-high-performance distributed workers, some developers call gc.disable() to avoid GC latency spikes during heavy batch processing. If you do this without cyclic hygiene or manually calling gc.collect(), your process will steadily leak memory until killed by Linux OOM killer (SIGKILL).
  • Global Data Structures that Accumulate: Appending log items or request payloads to module-level global lists or dictionaries prevents reference counts from ever dropping, leading to silent memory consumption over days.
  • Old __del__ Gotcha (Pre-Python 3.4): In historical Python versions, objects with custom __del__ destructors in a cyclic reference could not be collected. Since PEP 442 (Python 3.4+), CPython safely breaks cycles containing __del__, but relying heavily on __del__ is still discouraged.

Performance & Best Practices

  • Explicit Cleanup in Long Loops: When processing millions of large records, delete intermediate objects and periodically trigger collection if memory pressure is severe:
    python
    import gc
    
    for batch in massive_dataset:
        processed = process_large_batch(batch)
        upload_to_s3(processed)
        del processed
    
    # Explicit cleanup after processing completes
    gc.collect()
    
  • Use Slots for Million-Object Datasets: If you instantiate millions of small objects, define __slots__ in your classes. This prevents Python from creating a dynamic __dict__ for each instance, cutting memory consumption by up to 60%.

Interview Perspective

Python Interview Question: How does Python manage memory, and why does it need both reference counting and a generational GC?

Key Talking Points for Candidates:

  1. Dual System: Reference counting handles immediate, zero-latency deallocation for 95%+ of objects as soon as their scope ends.
  2. Cyclic Limitation: Reference counting cannot resolve circular references (A points to B, B points to A).
  3. Generational Collector: Python uses a tri-generational (Gen 0, 1, 2) graph-traversal collector that detects isolated cyclic graphs and reclaims them in batches, balancing memory reclamation with CPU throughput.

Interactive Practice Challenge

Challenge: Identify and Force Collection of a Memory Cycle

Construct a cyclical reference between two objects, delete their strong variable pointers, and verify that gc.collect() detects and frees the unreachable cyclic objects.

DPA

DataPlayArena Technical Editorial Board

Curated & maintained by enterprise data platform engineers with 10+ years in distributed systems.

Runtime Verification Standards: Every code example, query, and architecture diagram in this tutorial is compiled and verified against real runtimes (Apache Spark 3.5, Python 3.11, PostgreSQL, and DuckDB). If you spot any errata or want to suggest improvements, please submit feedback.