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.


In reply to Re: Can we determine when the garbage collector works? by Corion
in thread Can we determine when the garbage collector works? by Doraemon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.