Garbage Collector(GC) in Python (Part-I)

Ibrahim Sha
Analytics Vidhya
Published in
3 min readFeb 28, 2021

--

Garbage collector

Hi readers, this blog post would try to explain the implementations of the GC module.

Garbage Collector(GC) {wiki definition}:
Garbage collector (GC) is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program.

The basic idea of GC, is to free the memory block of unwanted objects in python.

Following are the Prerequisites to understand the GC module implementation in CPython. They are,

  • PyObj(Python core obj)
  • Memory reference
  • Reference counter

PyObj

  • PyObj is a python core object.
  • All objects in python, are finally referred to as PyObj.
  • PyObj has three properties,

> Type

> Value

> Reference Counter(ref_count)

  • Inspect any object in python, will have these properties.
PyObj structure
# source code to see pyobj propertiesimport sys

a = 5
print(type(a)) # this line will print obj type

print(a) # this line will print obj value

print(sys.getrefcount(a)) # this line will reference count of an obj.

Source code: https://github.com/ibrahimsha23/gc_python_meetup/blob/main/5.py

Memory Reference

a = 5; b = 5;
  • Assignment of the variable a and b with the same value(Eg: 5) will create a new memory block for every assignment in other programming languages such as C, C++, Java.

Python will not create new memory allocations for multiple assignments with same value(Eg: a=5; b=5). Internally, python is tagging the memory reference (memory location) to the variable for every assignment.

Memory reference

After the reallocation on variable `a` from 5 to 6 value, now the reference is changed from 00001 to 00002 memory location.

import sysa = 5b = 5def print_id(var_val, var):
print("id of {0} - {1}".format(var_val, id(var)))
print_id("a", a) # o/p: id of a - 140336997356632print_id("b", b) # o/p: id of b - 140336997356632
print("===================== value change ===============")
a = 6 # new valueprint_id("a", a) # o/p: id of a - 140336997356608print_id("b", b) # o/p: id of b - 140336997356632

Source code: https://github.com/ibrahimsha23/gc_python_meetup/blob/main/6.py

  • The above code block included the output of memory reference as a comment and you can see the change of memory address from 140336997356632 to 140336997356608 for the variable a.
  • Note: To know the memory address of a variable, we can use the python built-in method id.
                 id(variable_name)

Thanks for reading !

On part-II blog(will be published soon), will explain about the reference counter and its drawbacks.

On part-III blog(will be published soon), will explain about the implementations of GC.

Please share your suggestions in comments.

To reach out me, connect with me on LinkedIn.

--

--