Python garbage collector

Share Your Love

Python garbage collector, is responsible for automatically reclaiming memory occupied by objects that are no longer in use. It helps manage memory efficiently by freeing up resources that are no longer needed by the program.

The garbage collector in Python uses a combination of reference counting and a cyclic garbage collector (known as the “gc” module) to perform this task.

Reference counting:

Python’s reference counting mechanism is a simple and efficient way to track the lifetime of objects.

Each object in Python has a reference count associated with it, which is incremented when a new reference to the object is created and decremented when a reference goes out of scope or is deleted.

When the reference count of an object reaches zero, it means that the object is no longer accessible and can be safely deallocated.

Cyclic garbage collector:

In addition to reference counting, Python also employs a cyclic garbage collector to handle cases where objects form reference cycles or circular dependencies.

A reference cycle occurs when a group of objects reference each other in a way that no external references exist to the group. In such cases, the reference counting mechanism alone cannot reclaim the memory because the reference counts of the objects involved will never reach zero.

The cyclic garbage collector periodically identifies and collects cyclically referenced objects that are unreachable from the program.

It traverses the object graph, starting from known roots (such as global variables or objects on the stack), and marks objects as reachable.

Objects that are not marked during the traversal are considered garbage and can be safely deallocated.

Garbage collector module:

Python provides the gc module, which allows you to control and fine-tune the behavior of the garbage collector. It provides functions to manually trigger garbage collection, disable or enable the collector, set collection thresholds, and inspect the state of the garbage collector.

Here’s an example of using the gc module to manually trigger garbage collection:

import gc

# Create some objects
class MyClass:
    def __init__(self, name): = name

# Create a reference cycle
obj1 = MyClass("Object 1")
obj2 = MyClass("Object 2")
obj1.other = obj2
obj2.other = obj1

# Manually trigger garbage collection

# Output:
# 1 objects found, 0 objects unreachable

# The reference cycle has been collected

In the example above, a reference cycle is created between obj1 and obj2 by assigning each object to the other attribute of the other. After creating the cycle, we manually trigger garbage collection using gc.collect().

The output shows that one object was found and no objects were unreachable, indicating that the cyclically referenced objects were successfully collected.


It’s important to note that in most cases, you don’t need to manually trigger garbage collection in Python. The garbage collector runs automatically and efficiently manages memory behind the scenes.

Manual intervention is typically not required unless you’re dealing with specific memory management scenarios or debugging memory-related issues.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 411

Newsletter Updates

Enter your email address below to subscribe to our newsletter