Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Possible Memory Leak?

by Clovis_Sangrail (Beadle)
on Apr 26, 2013 at 19:25 UTC ( #1030880=perlquestion: print w/replies, xml ) Need Help??
Clovis_Sangrail has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perl Monks:

I am using a Perl CGI script to display/edit a file that has data lines like:

5200 "AMX" gtm_dist-V5.4-002B 5600 "TIAA CREFF" gtm_dist-V5.4-001 5995 "Morgan Stanley" gtm_dist-V5.4-001

I match each data line against:


and save in a hash whose key is the 1st field and whose value is a reference to an anonymous array of the 2nd & 3rd fields, ex:

$bkarr{$1} = [ $2 , $3 ];

Should the user choose to delete a line I just delete the hash entry that he specifies via:

delete $kbarr{$K};

before writing out the file. It works, but am I creating a memory leak by doing this? Do I have to somehow clean up the two-entry array whose reference I am holding in the hash entry? Thanks for any advice.

Replies are listed 'Best First'.
Re: Possible Memory Leak?
by davido (Archbishop) on Apr 26, 2013 at 19:33 UTC

    Perl uses reference counting as its garbage collection technique. When the last reference to a given object falls out of scope, or is explicitly eradicated (as in your call to delete), the object's reference count drops to zero, and Perl is permitted to clean it up. Memory leaks typically occur when reference counts are prevented from dropping to zero, and that usually happens with self-referential or co-referential objects. If $a = \$a, then when $a falls out of scope the reference count cannot drop to zero. This is what Scalar::Util's weaken() is there to address, or object destructors that are written to break known circular cycles.

    A more thorough, and community-refined explanation is available in perlref, under the section Circular References.


Re: Possible Memory Leak?
by BrowserUk (Pope) on Apr 26, 2013 at 19:28 UTC
    Do I have to somehow clean up the two-entry array whose reference I am holding in the hash entry?

    No. When you delete the hash entry, the anonymous array will have no further references and will be cleaned up automatically at the same time.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1030880]
Approved by BrowserUk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (1)
As of 2018-02-23 05:38 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (300 votes). Check out past polls.