Perl's uses reference counting as its method of doing garbage collection. As soon as the last reference to a value is released, the value is destroyed and freed. This has both advantages and disadvantages over Java's approach.

A disadvantage over Java's method is that you have to worry about circular references which will cause a leak.

push @{ $parent->{children} }, $child; weaken( $child->{parent} = $parent ); # Needed to avoid memory leak.

An advantage over Java's method is that you don't have to worry about when the object is destroyed, as it will be destroyed the moment is ceases being referenced. This avoids subtle, intermittent, timing-dependent errors.

{ Resource res = get_resource_exclusively(res_id); // ... res.release(); // Needed or the next line might fail. } { Resource res = get_resource_exclusively(res_id); // ... res.release(); }

You might even need a try/catch block to ensure timely destruction in some places.

Resource res = get_resource_exclusively(res_id); try { // ... } catch (Exception e) { res.release(); throw e; }

In reply to Re: Perl and Garbage Collection by ikegami
in thread Perl and Garbage Collection by Svetlana

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.