I shall not completely dwelve into all aspects of memory management of python but will explain how memory is allocated, reference count is tracked and object is deallocated by the python.

Open the file Objects/listobject.c and observe line number 142, there is a call to the macro PyObject_GC_New. Let us observe the code for it:

#define PyObject_GC_New(type, typeobj) \
( (type *) _PyObject_GC_New(typeobj) )

It calls _PyObject_GC_New function which internally calls _PyObject_GC_Malloc

PyObject *
_PyObject_GC_New(PyTypeObject *tp)
PyObject *op = _PyObject_GC_Malloc(_PyObject_SIZE(tp));
if (op != NULL)
op = PyObject_INIT(op, tp);
return op;

PyObject *
_PyObject_GC_Malloc(size_t basicsize)
PyObject *op;
PyGC_Head *g;
if (basicsize > PY_SSIZE_T_MAX – sizeof(PyGC_Head))
return PyErr_NoMemory();
g = (PyGC_Head *)PyObject_MALLOC(
sizeof(PyGC_Head) + basicsize);
if (g == NULL)
return PyErr_NoMemory();
g->gc.gc_refs = GC_UNTRACKED;
generations[0].count++; /* number of allocated GC objects */
if (generations[0].count > generations[0].threshold &&
enabled &&
generations[0].threshold &&
!collecting &&
!PyErr_Occurred()) {
collecting = 1;
collecting = 0;
op = FROM_GC(g);
return op;

which calls PyObject_MALLOC which uses Python Arenas for allocation for small objects else uses malloc to create larger objects.  Python Arenas will be explained in the coming section.

Let us observe how the reference counting mechanisms work.

There are basically two macros:

#define Py_INCREF(op) ( \

#define Py_DECREF(op) \
do { \
–((PyObject*)(op))->ob_refcnt != 0) \
else \
_Py_Dealloc((PyObject *)(op)); \
} while (0)

The first macro is called when a new reference is created to the object and second one when a reference is removed for example using del.

We observe that the object is automatically deallocated using _Py_Dealloc when the reference count becomes 0. I would suggest you to debug the macro once.

When the object is unable to deallocate due to cyclic references the cyclic garbage collector is called which is located in file Module/gcmodule.c in line number 872. I would urge you to debug and understand how it works.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s