Garbage collector strategy

Fredrik Lundh fredrik at
Wed Dec 22 12:22:39 CET 2004

Martin Drautzburg wrote:

> Just for curiosity: does python use a mark-and-sweep garbage collector
> or simple reference counting?

python the language doesn't specify this, but I assume you meant the CPython

both, sort of: it uses reference counting, and a separate "cycle breaking" collector
that runs when needed.  but unlike an ordinary m&s-collector, python's collector
looks for unreachable objects, not reachable objects.

> For mark-and-sweep, I assume there must be a toplevel Object from
> which all other objects can be accessed or they will be GCed (called
> "Smalltalk" in Smalltalk). Is there such a thing?

nope.  instead, the allocator keeps track of container objects that support the GC
protocol.  non-containers, such as integers and strings, are not tracked.  for an
overview, see:

for the details, see Modules/gcmodule.c.

other implementations are free to use other GC strategies.  Jython, for example, uses
the Java GC to handle Jython garbage.


More information about the Python-list mailing list