Garbage Collection-
Mechanism of requiring the memory by deleting the objects which are illegible for garbage collection.
- 1) If any object is not referenced by any live thread then that object will be illegible for GC(Garbage collection).
- 2) Illegible for GC doesn’t gives the 100% guarantee that the object will be deleted as soon as it is illegible for GC.
- 3) Local variable are also called temporary variable. Local variable will be automatically deleted as soon as function execution will complete as local variable scope is limited to its function only.
- 4) GC removes the self referenced object only when it reaches to thresh-hold number.
- 1) When memory is running low in that case We can ask GC to run forcefully.
- 5) collect() method (From gc module) can be called to execute GC forcefully to perform GC.
Ex-
import time, gc
class Test:
def fun():
x = []
x.append(10)
print(id(x))
class GCTest:
def main():
for i in range(5):
Test.fun()
gc.collect()
time.sleep(1)
GCTest.main()
Output-
2784965190344
2784967004808
2784967018184
2784967041800
2784967004808