On Mon, Aug 18, 2003 at 10:52:50AM -0700, K Stol wrote:
> After reading most of the messages on timely destruction, I still don't quite 
> understand what it is. If someone has a spare minute free, could you please explain? 

In perl5 you can write this.

my $Destroyed = 0;
sub DESTROY { $Destroyed = 1; }
{
    my $object = bless {};
}
die unless $Destroyed;
print "That's timely destruction\n";

and expect it to work.  It doesn't die and you reach the final print
statement.  That's because object are destroyed *immediately* upon their
falling out of scope so you can trust that the DESTROY method is called
immediately following the block.

Other languages don't work this way.  Java, for example, cleans up unused
objects whenever it happens to get around to it.  This means your object
might be destroyed immediately, or after a few statements have run, or
stick around until the end of the program.

Timely destruction is simple in Perl5 because it has a simple garbage
collection scheme, reference counting.  Each bit of date keeps track of how
many variables and references point to it.  When it drops to 0, its cleaned
up.  This count is kept up-to-date all the time so timely destruction
is easy.

Reference counting has problems.  One is memory leaks due to circular
dependencies.

    { my $a;
      my $b;
      $a = \$b;
      $b = \$a;
    }

$a refers to $b.  $b refers to $a.  Their ref counts remain 1 even after
the block ends and their variables fall out of scope.  The data is never
garbage collected.  Memory leak.

Another is that it turns out to be pretty slow (last I heard Dan talk about
it) compared to modern garbage collecting techniques.

So Parrot is going with something else.  Don't ask me what it is, I don't
know.  With this other garbage collecting technique its more involved than
ref counting to guarantee timely destruction as we desire in Perl.
Apparently someone's figured a way to do it and do it efficiently.


-- 
Michael G Schwern        [EMAIL PROTECTED]  http://www.pobox.com/~schwern/

Reply via email to