1.

Differentiate between reference counting and automatic garbage collection with respect to Python.

Answer»

Reference counting works by counting the number of times an object is referenced by other objects in the system. Python’s garbage collector runs during program execution and is triggered when an object’s reference count reaches zero. An object’s reference count changes as the number of aliases that point to it change. 

An object’s reference count increases when it is assigned a new name or placed in a container (list, tuple or dictionary). The object’s reference count decreases when it is deleted with del, its reference is reassigned, or its reference goes out of scope. When an object’s reference count reaches zero, Python collects it automatically. 

Automatic garbage collectiomPython deletes the objects which are not required, may it be built-in types or class instances, through the process named garbage collection. When the number of allocations minus the number of de-allocations is greater than the threshold number, the garbage collector is run and the unused block of memory is reclaimed. 

One can inspect the threshold for new objects by loading the garbage collector (GC) module and asking for garbage collection thresholds.



Discussion

No Comment Found