[Python-Dev] Linus on garbage collection

Greg Ewing greg.ewing at canterbury.ac.nz
Sat May 7 02:22:22 CEST 2011

Mark Shannon wrote:

> With a tracing GC:
> While the Elements are finalized, the Document is still alive.
> While the Document is finalized, the Elements are still alive.
> Then, and only then, is the whole lot reclaimed.

One problem is that, at the C level in CPython, you can't separate
finalisation and reclamation. When an object's refcount drops to
zero, its tp_dealloc method is called, which both finalises the object
and reclaims its memory.

Another problem is that just because an object's memory hasn't
been reclaimed yet doesn't mean it's safe to do anything with that
object. This is doubly true at the C level, where the consequences
can include segfaults.

Seems to me the basic issue here is that the C code wasn't designed
with tracing GC in mind. There is a reference cycle, but it is
assumed that the user is in manual control of deallocation and will
deallocate the Nodes before the Document.


More information about the Python-Dev mailing list