Skip to content
Home » Garbage collection

Garbage collection

Here is a clear, simple, and exam-focused explanation of Garbage Collection in Python.


Garbage Collection in Python

Garbage Collection (GC) is the process of automatically freeing memory that is no longer being used by a program.

Python provides automatic memory management, which means:

✔ Programmers do not need to manually allocate or free memory
✔ Python automatically removes unused objects
✔ Helps prevent memory leaks

This memory cleanup process is called garbage collection.


Why Is Garbage Collection Needed?

When objects are created, memory is allocated.
After they become useless (not referenced anymore), memory should be released.

Without garbage collection:

❌ Memory leaks occur
❌ System performance decreases
❌ Program may crash

Python solves this automatically.


—————————————–

How Does Python Garbage Collection Work?

Python uses two techniques:

✔ 1. Reference Counting (Primary mechanism)

Every object has a reference count → number of variables referring to it.

Example:

a = [1, 2, 3]
b = a   # both ref the same list

Reference count increases.

When reference count becomes zero, the object is immediately deleted.

Example:

a = [1,2,3]
del a   # reference removed
# object deleted (if no other references)

✔ 2. Generational Garbage Collection (Secondary mechanism)

Used for removing circular references, e.g.:

a = [1]
b = [a]
a.append(b)

Here:

  • a refers to b
  • b refers to a

Reference count never becomes zero, so Python uses generational GC to remove such unreachable objects.

Python maintains three generations:

  • Generation 0 → young objects
  • Generation 1 → older objects
  • Generation 2 → long-lived objects

Younger generations are checked more frequently.


—————————————–

The gc Module (Garbage Collector)

Python allows manual control of garbage collection using the gc module.

✔ Importing gc module

import gc

✔ Manually triggering garbage collection

gc.collect()

✔ Disabling garbage collection

gc.disable()

✔ Enabling garbage collection

gc.enable()

✔ Checking if GC is enabled

print(gc.isenabled())

—————————————–

When Does Python Perform Garbage Collection Automatically?

Python automatically collects garbage:

✔ When an object’s reference count becomes zero
✔ When memory is low
✔ Periodically based on generation thresholds
✔ When gc.collect() is triggered


—————————————–

Example of Garbage Collection

import gc

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

p = Person("Ravi")
p = None   # object is no longer referenced

gc.collect()   # manually calling GC

—————————————–

Advantages of Garbage Collection

✔ No need for manual memory management
✔ Prevents memory leaks
✔ Better memory optimization
✔ Cleaner and safer code
✔ Handles circular references automatically


—————————————–

Exam-Ready Short Answer

Garbage collection in Python is an automatic memory management system that removes unused objects to free memory. Python mainly uses reference counting to track object usage and generational garbage collection to clean circular references. The gc module allows manual control of the garbage collector using functions like gc.enable(), gc.disable(), and gc.collect().