Python Garbage Collection (Python GC) With Examples

Python garbage collection is the memory management mechanism in python. Let’s look into different aspects of garbage collection and how python garbage collection works.

What is Garbage Collection?

Garbage collection is the process of cleaning shared computer memory which is currently being put to use by a running program when that program no longer needs that memory. With Garbage collection, that chunk of memory is cleaned so that other programs (or same program) can use it again.

Garbage collection is a memory management feature in many programming languages. In this lesson, we will study how this mechanism works in Python.

Python Garbage Collection

The process of memory management in Python is straightforward. Python handles its objects by keeping a count to the references each object have in the program, which means, each object stores how many times it is referenced in the program. This count is updated with the program runtime and when it reaches zero, this means it is not reachable from the program anymore. Hence, the memory for this object can be reclaimed and be freed by the interpreter.

Let’s study python garbage collection with the help of an example:

In this example, we made a class and 3 reference variables pointing to the same object. Let’s visualise this:

python gc example

Now we let the variables user1, user2, and user3 point to None instead of the User instance.

With above code, the references have changed to:

Python Garbage Collection (Python GC) With Examples

After we assigned the last variable user3 to None, the object is garbage collected and this calls the __del__ function.

How Garbage collection varies with implementation

Garbage collection is a mechanism which varies with Python implementations like CPython, Jython or IronPython.

  • C Implementation of Python uses reference counting to track unreachable objects. It doesn’t track the objects at each line of execution instead, it periodically executes a cycle detection algorithm which looks for inaccessible objects and cleans them.
  • Jython uses the JVM’s garbage collector. The same applies to IronPython which uses the CLR garbage collector

If you want to study about the gc interface, do have a look at the Python docs.

Python Force Garbage Collection

As we studied above, the Garbage collection runs automatically as the program is under execution, sometimes, we might want to run the Garbage collection at a specific time. We can do this by calling collect() function. Let’s try to define a LinkedList class to demonstrate this:

Once that is done, we can start constructing their objects and trigger Garbage collection manually:

When we run this, the output will be:

Python Garbage Collection (Python GC) With Examples

In this example, the cyclic LinkedList objects are cleared as soon as garbrge collection runs the first time.

Python Garbage Collection Summary

Here, let’s provide some final ways through which we can optimise the use of Garbage collection:

  • Do not force collect garbage too many times. This is because that even if are freeing the memory, it still takes time to evaluate an object’s eligibility for being Garbage collected.
  • If you want to manually manage the Garbage Collection in your application, start doing it only after the app has completely started and then continue doing so in steady operations.

Garbage collection is a tricky mechanism if managed manually. To get it right, study the contract closely.

By admin

Leave a Reply