destructors, how?

Lex Spoon lex at cc.gatech.edu
Tue Oct 22 16:21:22 UTC 2002


On Tue, Oct 22, 2002 at 06:11:41PM +0200, Ragnar Hojland Espinosa wrote:
> On Tue, Oct 22, 2002 at 03:55:39PM +0100, Peter Crowther wrote:
> > > I can't find out how to write a destructor 
> > > or something functionally equivalent.
> > 
> > Check out implementors of 'finalize' (IIRC).  In essence, you hold onto
> > the object using a weak array; are notified when it is
> > garbage-collected; and clear appropriate fields based on copies of the
> > data in the object.
> 
> Finalize did get some nice matches.  Does finalize get called right away, or
> only when squeak does the GC?  And if so, am I guaranteed that it'll be GCed
> that moment?

No.  It happens whenenever the GC happens, which can be a long time.

One way to improve this is to manually invoke a GC if you get an error
from being out of resources.

However, finalization turns out to be fairly limited with a typical
garbage collector like Squeak's.  It seems to work best as a protection
against stupid mistakes during development, and not as a general programming
technique.  It's interesting to think about memory systems that can do
better at this, but that's not practical right now.  (Though, Squeak would
be a fine platform for doing memory management experiments!!)

This really irritates me intellectually.  Garbage collection works so
beautifully for *one* type of resource at a time.  Note that you can
run garbage collections efficiently if you just wait until memory is
low.  However, how do you do this for multiple kinds of resources in
addition to memory, while staying reasonably efficient?  For example,
suppose you have an application that *really* starts chewing through
sockets without using much memory?  Then collecting the sockets means
collecting memory more frequently, which sounds inefficient.

It seems like garbage collection should generalize somehow, but I have
absolutely no idea what that way would be.


> Ah well.. at least I can add the closes by hand :/  but that means leaks
> and sort of rules out to have decent db connection pools in any way I can
> think of.. so I must be missing something.

There are still things you can do.  You can have "close" decrement a
counter, and not do a real close until the counter reaches 0.



Lex




More information about the Squeak-dev mailing list