Ticker

6/recent/ticker-posts

Python: Garbage Collection

In old languages like C++, the programmer is responsible for both the creation and destruction of objects. Usually, programmers take very much care while creating objects but neglect the destruction of useless objects. Because of his neglect, total memory can be filled with useless objects which creates memory problems and total application will be down with Out of memory errors.

But in Python, We have some assistant which is always running in the background to destroy useless objects. Because of this assistance the chance of failing a 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 is 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.

1. gc.isenabled()

 Returns True if GC enabled

2. gc.disable()

 To disable GC explicitly

3. gc.enable()

 To enable GC explicitly

Example:

1) import gc 
2) print(gc.isenabled()) 
3) gc.disable() 
4) print(gc.isenabled()) 
5) gc.enable() 
6) print(gc.isenabled()) 
Output
True
False
True

"Python Garbage Collection"

"Python Garbage Collection Example"

"Python Garbage Collection Cyclic References"

"Python Garbage Collection Algorithm"

"Python Garbage Collection Performance"

"Del Python Garbage Collection"

"Disable Python Garbage Collection"

"Run Python Garbage Collection"

"Python Force Garbage Collection"

"Python Memory Garbage Collection"