By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,035 Members | 944 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,035 IT Pros & Developers. It's quick & easy.

Garbage collector strategy

P: n/a
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?
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
In message <87************@web.de>, Martin Drautzburg
<ma***************@web.de> writes
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
--
Stephen Kellett
Object Media Limited http://www.objmedia.demon.co.uk
RSI Information: http://www.objmedia.demon.co.uk/rsi.html
Jul 18 '05 #2

P: n/a
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
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>

Jul 18 '05 #3

P: n/a
Martin Drautzburg wrote:
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

Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.