2. About Me
★ Working in python, django, js > 6years
★ Github: github.com/ibrahimsha23
★ Linkedin: in/ibrahimshak
3. GC Definition
Definition(wiki)
● garbage collection (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.
Purpose:
● Freed the memory block of unwanted objects in python.
4. Prerequisites of GC
To know the gc module implementation in CPython, we need to understand few
elements,
➢ Pyobj (Python Core Object)
➢ Memory reference
➢ Reference counter
5. Pyobj
● Pyobj is the python core object.
● All objects in python, finally referred as pyobj.
● Pyobj has three properties,
○ Type
○ Value
○ Reference Counter
● If you inspect any object in python will have these properties.
6. Memory Reference
● In python, Everything is an object.
● Tag reference.
● Creating new object with same value, will not create a new malloc.
○ a=5, b=5
○ a = 6
● Refer: Link
7. Reference Counter
● On every allocation/ tag the memory reference of an object, will increase the ref count for the
object.
● On every assignment, function call args reference count will be increased.
● On de-allocation of object, will decrease the reference count.
● When ref count leads to zero, will be removed from the memory block.
● Reference counter is one of the types of GC.
● `sys.getrefcount( )`
● Refer: Link
8. Drawbacks of Ref counter
● Cyclic reference:
○ Self reference objects
○ Count will never decrease to zero.
○ It always be a unused objects.
<python>
a = [5]
a.append(a)
</python>
● ‘Del’ keyword in python.
● Refer: Link
9. GC - Implementation
● Generational based garbage collection
○ {0, 1, 2}
● Solving cyclic reference issue.
● Enabling/Disabling garbage collection manually.
● Source: Modules/gcmodule.c
● Refer: Link
● Gen -0 -> Newly created objects
● Gen - 1 -> Live objects
● Gen -2 -> Global objects
10. MISC (GC)
● Pypy, jython, iron python implementation on gc.
● Instagram disabling gc.(link).