Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2024-04-25 11:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found