Class destructor

Tim Peters tim.one at comcast.net
Thu Feb 13 14:45:17 EST 2003


[Jp Calderone]
> ...
>   3) Periodically throughout your program's run, inspect the contents of
>      gc.garbage.  If it contains cycles containing your objects, break
these
>      cycles explicitly, and remove the objects from the garbage list. *
> ...
>
> [*] I'm not sure if this approach actually work.  The gc may not
>     re-examine objects that have already been placed into the garbage
>     list.  Can anyone confirm or deny this?

Yes, it works fine:  there's nothing special (to the garbage collector)
about objects in gc.garbage.  gc doesn't try to collect them again simply
*because* they're reachable from a vanilla list (namely gc.garbage) that is
itself reachable (namely via the expression gc.garbage).  If you remove such
an object from gc.garbage, it's exactly as if it had never been in
gc.garbage.  So, break the cycle(s) it's in, and it will get collected
(provided it's still trash).  Your point about removing such objects from
gc.garbage is crucial here:  so long as an object is in gc.garbage, it can't
be collected, and simply because it's reachable from gc.garbage.

Easiest is to break all the cycles you feel like breaking, then do

    del gc.garbage[:]

Trash that's still in uncollectible cycles will eventually show up in
gc.garbage again.






More information about the Python-list mailing list