These slides explain the implementation of the Python core object, memory reference, reference counter, and garbage collector in python.
Size: 319.64 KB
Language: en
Added: Mar 01, 2021
Slides: 11 pages
Slide Content
Garbage Collector(GC)
in Python
About Me
★Working in python, django, js > 6years
★Github: github.com/ibrahimsha23
★Linkedin: in/ibrahimshak
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.
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
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.
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
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
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