Automatic garbage collection is one of the important features of Python. Garbage collector mechanism attempts to reclaim memory occupied by objects that are no longer in use by the program.
Python uses reference counting mechanism for garbage collection. Python interpreter keeps count of number of times an object is referenced by other objects. When references to an object are removed, the count for an object is decremented. When the reference count becomes zero, the object memory is reclaimed.
Normally this mechanism is performed automatically. However, it can be done on purpose if a certain situation arises in the program. The 'gc' module defines the garbage collection interface. Following functions are defined in 'gc' module
|enable()|| Enable automatic garbage collection.|
|disable()|| Disable automatic garbage collection.|
|isenabled()|| Returns true if the automatic collection is enabled.|
|collect()||With no arguments, run a full collection. The optional integer argument specifies which generation to collect(from 0 to 2). A ValueError is raised if the generation number is invalid. The number of unreachable objects found is returned.|
|set_threshold()|| Set the garbage collection thresholds (thecollection frequency).|
|get_threshold()|| Return the current collection thresholds as a tuple|
|callbacks|| A list of callbacks that will be invoked by the garbage collector before and after collection.|