Garbage collection in Python programming language

Garbage collection in Python programming language

Garbage Collection

Garbage collection is a tool used by the programming languages to delete the objects in memory that are not in the use. Garbage collection is invented by an American computer scientist John McCarthy in around 1959 to simplify manual memory management in lisp. Garbage collection can also be termed as the memory optimizer as it de-allocates the objects which are no longer in use in the code. Now lets us have a look at what is “Garbage collection in Python programming language”.

Strategies for memory management in Python

Python follows two approaches for memory management in the programming language:

  • Reference Counting
  • Garbage collection

These two features are used by the Python language for memory management. Now users don’t have to use pre-allocate or de-allocate memory like dynamic memory allocation in languages like C, or C++. This feature was inherited by the ABC programing language which is considered as the successor of the Python programming language.

Strategies of garbage collection

Reference Counting in Python programming language

In the python version, 2.x python uses Reference counting for memory management. Reference counting is done by counting the number of objects is referred by another object. As the reference of the object is removed the count of the object is de-allocated. As the reference count is decreased to 0 the object will be de-allocated.

The memory of the object or variable is freed only when the scope of the variable or object is over. Until the variable or an object is inside the scope it will not be cleared.

Reference Counting in Python programming language

Garbage Collection in Python programming language

Garbage collection

Garbage collection in python programming as the automatic scheduled memory management tool. Garbage collection in python is scheduled upon a threshold of object allocation and object de-allocation. The garbage collection runs when the number of object allocation – the number of object de-allocation is greater than the threshold. The threshold of the garbage collection in python can be checked by some python commands.

import gc
print('Threshold is:', gc.get_threshold())

The above command will help you to know about the default threshold of the python. The garbage collection of the python will only run if the maximum threshold is crossed.


Does python garbage collects automatically?

Python allocation and de-allocation are automatic, it works according to the default threshold. As the code cross the threshold value the garbage collection is initiated.

Is there garbage collection in C++?

No C++ programming does not have garbage collection as it does not contain any garbage in the code.


What are python collections?

Collection in python are containers which are used for collecting data. List, Tuples, Sets, Dictionaries are the example of the collection in python.

What are the types of garbage collection?

Different type of garbage collection in java are:

  • Serial Garbage Collector
  • Parallel Garbage Collector
  • CMS Garbage Collector
  • G1 Garbage Collector
  • Garbage Collection JVM Options

What is the purpose of garbage collection?

Purpose of garbage collection is to identify and destroy the memory locations that are not reachable. Garbage collection de-allocates the memory of the variable or the object which is no longer in use.