Problem with garbage collection (sort of)
pythonguy at Hotpop.com
Wed Aug 20 16:54:53 CEST 2003
Off topic, no doubt, but are you Frank Weiner,
If you are, I can divulge from where I got the
information, otherwise pardon me for the inconvenience.
frank at chagford.com (Frank Millman) wrote in message news:<246a4e07.0308200126.76a3dc78 at posting.google.com>...
> frank at chagford.com (Frank Millman) wrote:
> >y is an instance of class c, which creates an instance of class b,
> which creates an instance of class a. When y goes out of scope and is
> deleted, I want the instances of class b and class a to be deleted as
> >The problem is that class a keeps a reference to class b (self.b) and
> class b keeps a reference to class a (self.p), so the reference counts
> do not go down to zero without some additional action.
> Thanks for all the replies - I have learned a lot. It seems that this
> whole thing is not a problem at all. In other words, my instances
> *are* being deleted by the cyclic garbage collector, and my only
> problem was that I could not confirm this positively.
> As Michael says, the very act of creating a __del__ method in my
> classes prevents them from being deleted! However, as I only added the
> __del__ method to try to confirm the deletion, this is not a problem
> in practice.
> I replaced my __del__ methods with the DelWatcher class suggested by
> Tim. At first, nothing changed. Then I added 'import gc; gc.collect()'
> at the end, and lo and behold, I could see all my instances being
> Being a bit of a sceptic, I still did not regard this as positive
> confirmation that it will work in practice, as I do not have a
> gc.collect() in my live application, so I added the DelWatcher class
> there to see if I got the 'deleted' messages. I understand that
> gc.collect() runs automatically from time to time, so I waited a
> while, and did not get any messages. Then as I did some more work in
> the application, the messages started appearing for the older items.
> Re-reading the documentation on the gc module confirms that
> gc.collect() is only triggered when the number of 'dirty' objects
> exceeds a threshold.
> For the record, I tried Tim's suggestion of using weakrefs, and it
> worked perfectly. I did some timing tests and it seems to have very
> little overhead. However, as Tim says, it is better to stick to the
> cyclic garbage collector now that I have confidence that it is working
> Many thanks to all.
> Frank Millman
More information about the Python-list