I'll just pretend the 3rd "my" in your code isn't there. Others have already pointed it out...

One may still perform a variety of operations on the hash reference (and consequently the hash itself). Is this correct?

Yes, and that's why this isn't a memory leak :-) Java has the same behaviour in this case: if you can get at it, it's kept in memory.

To reduce the reference count to 'zero' (eliminating any potential memory leak that is present), I could simply set the $hash_ref variable to 'undef'(?)
You could. You could also let the reference go out of scope, and it will be cleaned up automatically. The only problem with a reference counting garbage collector are circular references. Like this:

{ my %monks = ( Zaxo => 'W. Virginia', tye => 'California', theorbtwo => 'Germany', castaway => 'Germany', atcroft => 'Georgia', rozallin => 'England', ); # %monks has a reference count of 1 my $ref = \%monks; # $ref has a reference count of 1 # %monks has a reference count of 2 $monks{myself} = \%monks; # %monks has a reference count of 3 } # leaving the scope decrements the reference count # for %monks and $ref by one. # $ref is collected (has a refcount of 0) # - decrement the reference count for monks # by one again # "%monks" has a reference count of 1 # and is not garbage collected

The reason %monks is not collected is because there still is a reference to %monks in $monks{myself}. $monks{myself} would be removed if %monks were collected, but there is still a reference to %monks in $monks{myself}... etc...

A mark-and-sweep collector can detect these circular references and would also collect %monks here. Perl doesn't (except at the end of execution).


In reply to Re: Memory leaks and reference counting within Perl. by Joost
in thread Memory leaks and reference counting within Perl. by DigitalKitty

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.