Python Memory Manager

Christian Heimes lists at cheimes.de
Sun Feb 17 22:03:34 CET 2008


Pie Squared wrote:
> I've been looking at the Python source code recently, more
> specifically trying to figure out how it's garbage collector works.
> 
> I've gathered that it uses refcounting as well as some cycle-detection
> algorithms, but I haven't been able to figure out some other things.

Python uses ref counting for all objects and an additional GC for
container objects like lists and dicts. The cyclic GC depends on ref
counting.

> Does Python actually have a single 'heap' where all the data is
> stored? Because PyObject_HEAD seemed to imply to me it was just a
> linked list of objects, although perhaps I didnt understand this
> correctly.

In release builds PyObject_HEAD only contains the ref count and a link
to the object type. In Py_DEBUG builds it also contains a double linked
list of all allocated objects to debug reference counting bugs.

Python uses its own optimized memory allocator. Be sure you have read
Object/obmalloc.c!

> Also, if it does, how does it deal with memory segmentation? This
> question bothers me because I've been trying to implement a moving
> garbage collector, and am not sure how to deal with updating all
> program pointers to objects on the heap, and thought perhaps an answer
> to this question would give me some ideas. (Also, if you know any
> resources for things like this, I'd be grateful for links/names)

I don't think it's possible to implement a moving GC. You'd have to
chance some fundamental parts of Python.

Christian




More information about the Python-list mailing list