Weak References in Python
In Python, we never have to think about memory management because it is done automatically. The reference count is what is used in python for checking if garbage collection should be done or not. So, what happens behind the scenes is that the garbage collector frees the memory that is no longer needed which it finds out by looking the reference count.
As everything in Python is an object so if an object is referenced by another object then it has a non-zero count and can’t be garbage collected (if not manually performed).
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Below is a short example on how can we find the reference count of any object in python.
Reference count for my_list is: 1
So, now as we are clear with the basics of references and garbage collections let’s move to what are weak references and why do we need them?
What are Weak Reference?
Unlike the references we discussed above , a weak reference is a reference that does not protect the object from getting garbage collected. Why do we want such a thing in the first place?
There are two main applications of weak references:
- Implement caches for large objects (weak dictionaries).
- Reduction of Pain from circular references.
To create weak references, Python has provided us with a module named weakref. A point to keep in mind before using weakref is that some builtins such as tuple or int does not support this. list and dict support is either but we can add support through subclassing. Let’s discuss about the applications in detail.
Sometimes a large object is stored in the cache so there is no need to keep it alive. Let’s you have a large number of image objects and they are being used as keys to map to images due to which these objects will be kept alive.
Fortunately, weakref module provides something called as WeakKeyDictionary and WeakValueDictionary don’t keep the objects alive as they appear in the mapping objects.
The following classes and methods are provided by weakref module:
- class weakref.ref(object[, callback]) – This returns a weak reference to the object.
- weakref.proxy(object[, callback]) – This returns a proxy to object which uses a weak reference.
- weakref.getweakrefcount(object) – Return the number of weak references and proxies which refer to object.
- weakref.getweakrefs(object) – Return a list of all weak reference and proxy objects which refer to object.
Let’s understand the working with some examples:
In the below-given example we create normal list object, a weak reference list object, and a proxy list object and print the weak reference count for all of them.
This is a normal object: [‘G’, ‘e’, ‘e’, ‘k’, ‘s’]
This is a object created using weak reference: [‘G’, ‘e’, ‘e’, ‘k’, ‘s’]
This is a proxy object: [‘G’, ‘e’, ‘e’, ‘k’, ‘s’]
Number of weak references: 2
Number of weak references: 2
Number of weak references: 0
As the normal object has references to the weak reference object hence both of them have a weak reference count of 2 but as proxy_object is a proxy created from the weak reference thus it does not have a reference count.
In this example we will see how to use WeakValueDictionary which we discussed earlier in the article.
Weak reference count is: 1
In the above example we assumed that there is a really large object which has been placed in the cache so we have created a weak ref dictionary to store that key and value pair so it will be garbage collected.