- In old languages like C++, programmer is responsible for both creation and destruction of objects.Usually programmer taking very much care while creating object, but neglecting
- destruction of useless objects. Because of his neglectance, total memory can be filled with useless objects which creates memory problems and total application will be down with out of memory error.
- But in Python, We have some assistant which is always running in the background to destroy useless objects. Because this assistant the chance of failing Python program with memory problems is very less. This assistant is nothing but garbage collector.
- Hence the main objective of garbage collector is to destroy useless objects.
- If an object does not have any reference variable then that object eligible for garbage collection.
How to enable and disable Garbage Collector in our program
- By default Gargbage collector is enabled, but we can disable based on our requirement. In this context we can use the following functions of gc module.
- gc.isenabled()
Returns True if GC enabled
- gc.disable()
To disable GC explicitly
- gc.enable()
To enable GC explicitly
Example
Python
import gc
print(gc.isenabled())
gc.disable()
print(gc.isenabled())
gc.enable()
print(gc.isenabled())
Output
PowerShell
True
False
True
Destructors
- Destructor is a special method and the name should be del
- Just before destroying an object garbage collector always calls destructor to perform clean up activities (Resource deallocation activities like close database connection etc).
- Once destructor execution completed then garbage collector automatically destroys that object.
Note
- The job of destructor is not to destroy object and it is just to perform clean up activities.
Example
Python
import time
class Test:
def __init__(self):
print("Object Initialization...")
def __del__(self):
print("Fulfilling Last Wish and performing clean up activities...")
t1=Test()
t1=None
time.sleep(5)
print("End of application")
Output
PowerShell
Object Initialization...
Fulfilling Last Wish and performing clean up activities...
End of application
Note
- If the object does not contain any reference variable then only it is eligible fo GC. ie if the reference count is zero then only object eligible for GC.
Example
Python
import time
class Test:
def __init__(self):
print("Constructor Execution...")
def __del__(self):
print("Destructor Execution...")
t1=Test()
t2=t1
t3=t2
del t1
time.sleep(5)
print("object not yet destroyed after deleting t1")
del t2
time.sleep(5)
print("object not yet destroyed even after deleting t2")
print("I am trying to delete last reference variable...")
del t3
Output
PowerShell
7
Example
Python
import time
class Test:
def __init__(self):
print("Constructor Execution...")
def __del__(self):
print("Destructor Execution...")
list=[Test(),Test(),Test()]
del list
time.sleep(5)
print("End of application")
Output
PowerShell
Constructor Execution...
Constructor Execution...
Constructor Execution...
Destructor Execution...
Destructor Execution...
Destructor Execution...
End of application
How to find the number of references of an object
- sys module contains getrefcount() function for this purpose.
- sys.getrefcount(objectreference)
Example
Python
import sys
class Test:
pass
t1=Test()
t2=t1
t3=t1
t4=t1
print(sys.getrefcount(t1))
Output
PowerShell
5
Note
- For every object, python internally maintains one default reference variable self.
Ungraded Questions
Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!