Doraemon has asked for the wisdom of the Perl Monks concerning the following question:

As much as i know, garbage collector works automatically. In most language, like Java for example, garbage collector collect the 'garbage' when there is no variable referring to it anymore. What about Perl? And can we inspect this using the debugger (or any tools)?
I do lots of codes using pointers, working on a huge set of data. So this concerns me a lot :)
  • Comment on Can we determine when the garbage collector works?

Replies are listed 'Best First'.
Re: Can we determine when the garbage collector works?
by Corion (Patriarch) on May 11, 2004 at 12:54 UTC

    Perl has no Garbage Collection, because Perl does Reference Counting.

    Most likely, you don't code with pointers but with references, and the only thing you have to fear with Reference Counting are circular references, as the reference count for these can never reach zero and thus the memory never be reclaimed.

    There are two ways around this - one way is to have an explicit destroy() method that the user is supposed to call - a stupid idea in my opinion, but sometimes less hassle than the second option. Scalar::Util has weaken(), which allows you to create weak references, that is, references which do not increase the reference count for the referenced object. With that, structures containing circular references can also be reclaimed as soon as the last strong reference falls out of scope.

    Note that a closure never goes out of scope as the subroutine never gets released, and thus the memory held by the closure cannot be reclaimed automatically (if at all before process exit).

    Steve_p points to Devel::Cycle and Test::Memory::Cycle to hunt down the circular references in your programs.

      Since we can't reclaim automatically the memory in closure, is that mean i still need to traverse down (i wonder why not up? :)) the tree?

        First, a very simple example of a closure:

        { my $tree; sub get_tree { return $tree; }; };

        In the above example, $tree and all memory referenced by it will never go out of scope, and since $tree is invisible outside of its containing block, there is no way to free up the referenced memory.

        { my $tree; sub get_tree { return $tree; }; sub release_tree { undef $tree; }; };

        In this example, it is possible to release the memory structure referenced by $tree by calling release_tree, because it also sees the $tree variable. But this release is not automatic and thus must be done by the programmer when the time has come.

Re: Can we determine when the garbage collector works?
by Juerd (Abbot) on May 11, 2004 at 14:38 UTC

    When an object is destroyed (garbage collected), its DESTROY method is called.

    { package Some::Class; sub new { my $self = bless {}, shift; print "Object created.\n"; return $self; } sub DESTROY { print "Object destroyed.\n"; } } print "1\n"; for (1 .. 3) { print "2\n"; my $foo = Some::Class->new; print "3\n"; } print "4\n"; __END__ 1 2 Object created. 3 Object destroyed. 2 Object created. 3 Object destroyed. 2 Object created. 3 Object destroyed. 4
    You can tie a variable to an object.
    use Tie::Scalar; { package Some::Class; our @ISA = qw(Tie::StdScalar); sub DESTROY { print "Object destroyed.\n"; } } if (1) { print "1\n"; # my $foo = 3; tie my $foo, 'Some::Class'; $foo = 3; print "2\n"; } print "3\n"; __END__ 1 2 Object destroyed. 3

    Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }

      Suprisingly, i just realized i haven't tried implementing OOP in Perl (although already done it in PHP, Javascript(?), C++, Java).
      Anyway i'll try it. Thanx a lot :)
      Perl might just be another bright shining pearl...
Re: Can we determine when the garbage collector works?
by pbeckingham (Parson) on May 11, 2004 at 12:50 UTC

    Perl indeed has the garbage collector you require, but has references instead of pointers. The built-in debugger will allow you to navigate your data.

      Could u show me a little about this feature (how to use them, actually :) ) ? Thanx in advance
Re: Can we determine when the garbage collector works?
by Doraemon (Beadle) on May 11, 2004 at 13:53 UTC
    thanx guys :)