Reference counting garbage collection

Frederic Giacometti frederic.giacometti at arakne.com
Thu Aug 23 15:01:09 EDT 2001


"Paul Rubin" <phr-n2001 at nightsong.com> wrote in message
news:7xwv3w78y2.fsf at ruckus.brouhaha.com...
> Is there any particular reason Python uses reference counting garbage
> collection (which leaks memory if there's circular structure) instead

1) Reference counting is efficient, simple and robust
2) Circular lists are relatively unfrequent, this low frequency makes it
bearable to take special care with them.

> of Lisp-style garbage collection?  As Python gets used for more large
> application development, it gets troublesome to make the programmer
> worry about whether the data has cycles.  Also, using a compacting GC

Reference cycles are first a design issue. Object models without circularity
are usually better models, and designs are usually improved after
circularity was removed. A little like multiple inheritance...
Situations where reference cycles are actually needed (and beneficial) are
very rare.

> helps localize memory references, speeding up the program by improving
> cache performance.  As CPU's get faster and memory doesn't, the cycle
> cost of a cache miss continues to increase.  So maybe the GC
> implementation should be revisited at some point.

Whereas reference counting scales up very well as the number of objects
increases, automated garbage collection just does not scale up.
In other words: GC will collapse before you can get a couple million objects
in memory, where reference counting has no performance impact.
(Hint: Java...)

The amazing stability, efficiency, and scalability of Python actually owes
much to the reference counting schema...

Finally, as far as the C API is concerned, garbage collextion actually make
C interfacing sensibly more complicated than reference counting. In effect,
as soon as the C structures point on language objects, a means for the GC
must be provided to traverse the structure, and that really becomes a pain
to program properly.
Reference counting is a lot simpler in this regard!

Frederic Giacometti











More information about the Python-list mailing list