GC and finalizers [was: No destructor]

Neil Schemenauer nascheme at enme.ucalgary.ca
Wed Aug 23 21:01:05 EDT 2000


Martin von Loewis <loewis at informatik.hu-berlin.de> wrote:
>Neil Schemenauer's collector does not collect a cycle if it involves
>finalization of an instance object.

Real life is a little more complicated than that.  The GC tries
quite hard to do the right thing.  For example, consider the
garbage structure crudely drawn below:

      .->A-->C           
      |  |                  
      B<-'                  

If only C has a finalizer then the structure will be collected
and C's finalizer called.  If either A or B have finalizers then
the structure is not collected but instead linked to the
gc.garbage list.  Theoretically it could also be collected if
either A or B had finalizers but not both.  Unfortunately
Python's collector is not that smart.

The basic algorithm goes as follows:  Once garabage is found, all
objects with finalizers and objects reachable from these objects
are moved to a special "finalizer reachable" set.  The remaining
garbage objects are freed.  This may cause the refcounts of some
objects in the finalizer reachable to go to zero and they will be
freed.  The remaining instance objects in the finalizer reachable
set will be added to gc.garbage.

For all the gory details, search the python-dev archives for the
term "finalizers".

  nas



More information about the Python-list mailing list