Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Each iteration through outer while loop creates a new %map, and at the end of each outer while loop, that %map should be falling out of scope, at which time it should be garbage collected. However, you are passing a reference to that hash to process(), and we have no idea what you're doing with that reference. That subroutine could possibly be doing something that causes the reference count to the hash to never fall to zero, so it never gets garbage collected.

Here's a trivial example:

while( 1 ) { my %hash = ( this => 1, that => 2 ); process(\%hash); } sub process { my $href = shift; $href->{me} = $href; }

Run that and watch as your memory usage climbs. On each iteration of the while loop a new lexically scoped hash is created. And at the end of each iteration, it falls out of scope and could be garbage collected, except that you passed a reference to it to process(). And process() is doing something nasty; it's creating an element in the %hash that references the same hash. This is a circular reference, and it prevents the hash's ref count from ever dropping to zero.

What's even worse in this case is that as soon as %hash falls out of scope, it's lost; there are no external variables holding a reference to it. The hash is self-referential, and unreachable.

This is pretty contrived, but just an example of where the trouble can come from. More often (in the real world), some complex arrangement of objects leads to a circular reference that nobody considered until the server started bogging down every day or two.


Dave


In reply to Re^5: Enforce Memory Cleanup by davido
in thread Enforce Memory Cleanup by solegaonkar

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? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2023-06-03 14:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How often do you go to conferences?






    Results (14 votes). Check out past polls.

    Notices?