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:
1 2 3 4 5 6 7 8 |
class User(object): def __del__(self): print("No reference left for {}".format(self)) user1 = User() user2 = user1 user3 = user1 |
In this example, we made a class and 3 reference variables pointing to the same object. Let’s visualise this:
Now we let the variables user1, user2, and user3 point to None instead of the User instance.
1 2 3 4 5 6 7 |
>>> user1 = None >>> user2 = None >>> user3 = None No reference left for <__main__.User object at 0x212bee9d9> <img class="alignnone wp-image-22818 size-full" src="http://all-learning.com/wp-content/uploads/2018/01/python-gc-example.png" alt="python gc example" width="1200" height="628" /> |
With above code, the references have changed to:
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:
1 2 3 4 5 6 7 8 9 10 11 |
class LinkedList(object): def __init__(self, name): self.name = name self.next = None def set_next(self, next): print('Linking nodes %s.next = %s' % (self, next)) self.next = next def __repr__(self): return '%s(%s)' % (self.__class__.__name__, self.name) |
Once that is done, we can start constructing their objects and trigger Garbage collection manually:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Constructing a circular LinkedList a = LinkedList('1') b = LinkedList('2') c = LinkedList('3') a.set_next(b) b.set_next(c) c.set_next(a) # Remove references to the LinkedList nodes in this module's namespace a = b = c = None # Show the effect of garbage collection for i in range(2): print('Collecting %d ...' % i) n = gc.collect() print('Unreachable objects:', n) print('Remaining Garbage:', pprint.pprint(gc.garbage)) print |
When we run this, the output will be:
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.