Editors and books

Thomas Wouters thomas at xs4all.net
Mon Apr 17 12:48:33 EDT 2000


On Mon, Apr 17, 2000 at 09:00:56AM -0600, Jeff Massung wrote:

> Also, on another NG there's talk about Python, and how it does (and still
> doesn't) do garbage collecting. Could someone here expand on it so I know
> what it can and can't do? Memory is a big issue in my project and I need to
> know how to structure my program so that garbage is collected. Thanks
> again!!

Python doesn't do GC, it does RC, reference counting. That means that as
long as there is a reference to an object, it will not be collected. When
you delete a reference to an object, it's refcount goes done by one, and
when the count has dropped to 0, it's eligible for collection. (Note that
this doesn't mean it gets collected right away! It usually happens fairly
soon, though.)

The problem with RC is that you can create circular references:

a = [some large list]
b = [some other large list]
a.append(b)
b.append(a)

del a
del b

Now you can never refer to a and b and their contents again, but as they are
still referenced (by the other list) they can't be collected, either. So
avoid recursive constructs ;) It's fairly easy to fall into this trap when,
for instance, fiddling with tracebacks, though that isn't common practice
last time I looked.

There is work being done to cure this problem in a limited way -- not
all-encompassing, but enough to cure all but the most pathological case, I
gather. See the newsgroup archives ;)

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list