Garbage Collection:
- Python is fully Object Oriented Programming Language.
- In OOP application, data stores in the form of Objects.
- We access the objects information using references.
- Garbage collection is the mechanism of deleting unreferenced objects.
- An object is said to be eligible for Garbage Collection when no pointer is pointing to that object.
- In python garbage collection mechanism is much faster compare to other languages.
In the following program, object get different memory locations
class Test:
def __init__(self):
print("Constructed :",id(self))
return
class GCDemo:
def main():
x = Test()
y = Test()
z = Test()
return
GCDemo.main()
- In the above application, we maintain the references into variables called x, y and z.
- As we are maintaining the references, these objects are not eligible for GC.
- Different objects get memory in different locations.
As soon the object is unused, GC will destroy that object. The same memory is allocated to newly created object.
class Test:
def __init__(self):
print("Constructed :",id(self))
return
class GCDemo:
def main():
Test()
Test()
Test()
return
GCDemo.main()
- Defining a variable inside the function is called Local variable.
- Local variable memory is not permanent
- Local variable will be deleted as soon as function execution completed.
- If the local variable is pointing to any object, then the object become unreferenced once local variable memory will be released.
- The object pointing by local variables will be deleted as soon as function execution completes.
class Test:
def fun():
x = []
x.append(10)
print("Address of x :", id(x))
return
class GCDemo:
def main():
Test.fun()
Test.fun()
Test.fun()
return
GCDemo.main()
- If the object is pointing to itself is called ‘self referenced object’.
- “self” is pointing objects will not garbage collected as soon as they are unreferenced.
class Test:
def fun():
x = []
x.append(x)
print("Address of x :", id(x))
return
class GCDemo:
def main():
Test.fun()
Test.fun()
Test.fun()
return
GCDemo.main()
Manual Garbage collection:
- In the above application, self referenced objects will not be garbage collected as soon as they are unreferenced.
- To destroy such type of object manual garbage collection is required.
- We can reuse the memory quickly by deleting the unused objects.
- ‘gc’ is a pre-defined module providing functionality to implement manual garbage collection.
- collect() method of gc module is used to destroy objects
import time
class Test:
def fun():
x = []
x.append(x)
print("Address of x :", id(x))
return
class GCDemo:
def main():
for i in range(20):
Test.fun()
time.sleep(1)
return
GCDemo.main()
- In the above application, all objects get different memory.
- These unused objects will be deleted with manual garbage collection as follows
import time
import gc
class Test:
def fun():
x = []
x.append(x)
print("Address of x :", id(x))
return
class GCDemo:
def main():
for i in range(20):
Test.fun()
gc.collect()
time.sleep(1)
return
GCDemo.main()