Garbage collector strategy

M

Martin Drautzburg

Just for curiosity: does python use a mark-and-sweep garbage collector
or simple reference counting? In the latter case it would not garbage
collect circular references, right ?

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?
 
S

Stephen Kellett

Martin Drautzburg said:
Just for curiosity: does python use a mark-and-sweep garbage collector
or simple reference counting? In the latter case it would not garbage
collect circular references, right ?

gcmodule.c in the python sources shows the implementation, plus the code
for this is well documented.

Stephen
 
F

Fredrik Lundh

Martin said:
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
interpreter.

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:

http://www.arctrix.com/nas/python/gc/

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.

</F>
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

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

I can't resist: Yes, it does.
In the latter case it would not garbage
collect circular references, right ?

Python uses refcounting for all objects, and a generational collector
for container 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?

No. There are no gc roots. Instead, there are global lists of all
container objects, sorted by generation. Garbage is what is not
referenced from outside these lists, everything else must be rooted
somewhere (either in a true root, a local variable of some stack
frame, or an older generation)

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top