Like any other programming language, concept of variable is connected to memory allocation. Variable is the storage space which can store the values, as per its behaviour.
For instance, let us consider a statement, a = 20, here memory is allocated with name ‘a’ and value 20 is stored in it. In the similar manner, for every variable, a seperate memory space is allocated.
Garbage Collection is a form of automatic memory management. The garbage collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program. Module that represents the garbage collector is named as ‘gc’. Garbage collector aims to identify the reference cycles. Reference cycle is the cycle of references from initial object to final object. It is used to identify reference count of any object. When any object is referenced, its reference count is set to 1. Garbage collector will only remove the objects if its reference count is 0.
Garbage collector runs automatically. Python schedules the garbage collector on the basis of threshold value. It can be known with the help of get_threshold() of gc module. However, in some cases, where reference cycle is not found, it is suggested to run gc manually using collect() method.