fork()

Guido van Rossum guido at cnri.reston.va.us
Thu Jun 10 16:26:14 EDT 1999


"Andrew M. Kuchling" <akuchlin at mems-exchange.org> writes:

> Guido van Rossum writes:
> >(You can create a cycle using only lists, but I bet that happens only
> >for demonstration purposes, as in ``a = []; a.append(a)''.  So I can
> >live with not freeing such cycles.)
> 
>       I'm doubtful of that argument; I've written classes which track
> all instances that get created, and this can be done with a class
> attribute containing a list, that gets mutated as instances are
> created.  You reconsider this decision later in your post.

You misread me (or I didn't state it very carefully).  I didn't mean
to say that cycles wouldn't contain lists; I meant to say that cycles
typically contain at least one dictionary.

> 	  In this solution, I don't understand how Python would keep
> track of all the dictionaries that get created, in order to determine
> which ones are left unreachable, and hence can be garbage collected.
> From my memory of the Jones-Lins book, this is usually handling by
> doing all the allocation yourself by slicing memory into chunks, and
> then sweeping through it.  It's not obvious (to me, at least) how
> you'd do this with objects created by malloc().  Perhaps lists and
> dictionaries could contain prev/next pointers that are used to
> maintain them in a doubly-linked list, and a linear-time traversal
> would be done to find unmarked objects, but there has to be a better
> way.

A doubly-linked list is exactly what I had in mind.  Sorry I wasn't
more explicit.  I would think that linear time to find unmarked
objects is fine...  Isn't it?  The elegance (I hope) of my scheme is
that it only needs to mark relatively few objects (all dicts and
perhaps all lists) compared to the total number of allocated objects.

Of course finalizers are still screwed, since if an instance is
involved in a cycle, I may have zapped its instance variables before
calling its __del__.  (To break the cycles, the collector would call
the clear() method of all unmarked dicts, which would break the
cycles, and then the ref counting would free the depending objects.)

--Guido van Rossum (home page: http://www.python.org/~guido/)




More information about the Python-list mailing list