New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL
See the Tutorial List

Garbage Collection in Python

In any program, garbage is a collection of memory that is not being used by the program or was being used at a given point but not anymore. As this memory space remains allocated to the program but is not being used by it hence it is wasted memory which needs to be cleaned.

Garbage collection is a process of freeing up memory that is not in use by a specific program at a given instance of time.

This process is helpful in memory management and minimizes the wastage of memory. The memory freed by garbage collection can be used for storing other important data or variables for the same program or by other programs.


Python: Automatic Garbage collection

Garbage collection in Python works automatically. When no reference is left to a variable/data/value or object then the memory occupied by that object is freed up by garbage collection mechanism of python. Thus, python provides with good memory management and prevents the wastage of memory.

The below example shows how the python garbage collection works:

class SomeObj:

    def __init__(self):
        print('The object is created.')

    def __del__(self):
        print('The object is destroyed.')

obj1 = SomeObj()
obj2 = obj1
obj3 = obj1
print("Set obj1 to None...")
obj1 = None
print("Set obj2 to None...")
obj2 = None
print("Set obj3 to None...")
obj3 = None

The object is created. Set obj1 to None... Set obj2 to None... Set obj3 to None... The object is destroyed.

In the above example, an object of class SomeObj is created which is referenced by obj1. Then, obj2 and obj3 also refer to the same memory location as obj1.

After creation of the object the __init__(init) method is called and when the object is destroyed, due to garbage collection __del__(del) method is called.

In the example, first, we created an object of SomeObj class and passed its reference to obj1 and then to obj2 and obj3, which makes the reference count of that object 3. But when all these variables are assigned to None then the references from the object are removed.

At last, when there is no reference left to the object then it is automatically destroyed by the Garbage collector of python and __del__() method is executed.


Python: Forced Garbage Collection

Sometimes, a user may need to do garbage collection for memory management explicitly so as to free up some memory space. So, python also allows explicit garbage collection which can be done using the gc module. Garbage collection can be done forcibly by using the collect() function of the gc module as shown below:

 import gc

class SomeObj:

    def __del__(self):
        print('The object is destroyed.')

obj1 = SomeObj()
obj2 = obj1
obj3 = obj1
obj1 = None
obj2 = None
obj3 = None

for i in range(10):
    dic = {}
    dic[0] = dic

print('Collecting...')
n = gc.collect()
print('Unreachable objects:', n)

The object is destroyed. Collecting... Unreachable objects: 9

In the above example, the simple object created (of SomeObj class) is destroyed by python's implicit garbage collector. But the case is not same for object dic which is a dictionary pointing itself again and again forming a cycle. The objects that are formed in this cycle cannot be destroyed by python garbage collector implicitly.

So, for destroying the objects created by the cycle (that is run by the for loop), we have used collect() method of the gc module.

The collect() method runs garbage collection and destroys the unused objects that are the objects with reference count 0. The collect method returns the number of unreachable objects, here unreachable objects means those objects which have reference count 0.