Normally, Python internally deletes an object from the memory when it is no longer referenced by any other part of the program. This process is known as Garbage Collection. Once an object has been garbage-collected, it can no longer be used or accessed.
Whenever an object in a Python program is referenced elsewhere, the reference count of that objects gets incremented, this ensures that an object that is still being referenced is not deleted or garbage collected. This is referred to as strong reference because it ensures that an object stays in memory as long as it is being used and referenced by other parts of the program.
In strong references, deleting an object, for example using the del
statement, will not actually delete the object if the reference is still held.
Weak references
In weak references, a reference to an object does not prevent the garbage collector from reclaiming it. This makes them useful for implementing certain caching for large objects, or when attempting to prevent memory leaks due to circular references in an application.
The weakref
module provides support for weak references. The ref
class in the module provide the framework for creating weak references.
Weak references can be used to track an object without preventing it from being garbage collected.
The process of creating weak references is as outlined below:
In the above example:
- We imported the
weakref
module - Created the
Bear
class. - created an instances of
Bear
calledbear1
- Created a weak reference of
bear1
calledbear2
i.eweakref.ref(bear1)
.
Note that the weak reference(bear2
) is no longer accessible after we deleted the original object, bear1
. Instead, the default value None
is returned if the original object has been deleted/garbage collected.
Note that we are calling the weak reference object as if it were a function, as in bear2()
. This is because ref
returns the weak reference instance as a callable object. Consider the fallowing example:
As you can see above, using bear2
without the parentheses does not access the weak reference object, we have to call it with parentheses to access the actual object. As in below:
Reference Callbacks
The ref()
constructor allows us to provide a callback function that will be called if the object has been deleted from memory.
In the above example, we tried to access a weak reference object bear2
, while the origin object(bear1
) has already been deleted from memory. Since we provided a callback function to ref()
, the callback is called instead of returning the default value None
.
Proxies
The ref()
class creates a simple weak reference to an object, this means that if the object is garbage collected, the weak reference will simply be set to None
. It does not provide any additional behavior or properties.
The proxy
class , on the other hand, can be used to create a proxy
for an object. This proxy object provides a way to access the object's attributes and methods even after the object is garbage collected. However, any access to a garbage-collected object's attributes or methods will result in a
ReferenceError
exception.
Weak references in dictionary elements
The module provides two important classes which can be used to implement weak references in dictionaries i.e the WeakValueDictionary
and the WeakKeyDictionary
classes.
WeakValueDictionary
The WeakValueDictionary
class creates dictionary objects in which the values are held weakly. An item is kept in the dictionary as long as its value is strong-referenced elsewhere.
In the above example, we created a WeakValueDictionary
called D
. As you can see from the outputs, whenever a value is deleted the dictionary item with that value
is removed from the dictionary.
WeakKeyDictionary
The WeakKeyDictionary
class works like the WeakValueDictionary
but with keys rather than values.
It creates a dictionary in which items are kept only if they are strong referenced elsewhere in the program.
As shown above, item whose key has been deleted and is no longer strong referenced anywhere else in the program, is automatically removed from the WeakKeyDictionary
.