Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re^4: Enforce Memory Cleanup

by solegaonkar (Beadle)
on Jun 18, 2014 at 04:30 UTC ( #1090251=note: print w/replies, xml ) Need Help??

in reply to Re^3: Enforce Memory Cleanup
in thread Enforce Memory Cleanup

Thanks for your interest. But I am not able to understand what is the meaning of this undef? undef %map I thought that should clear off all the memory!

I tried to push the map inside the while loop, so that the scope itself takes care of things. But, the code still runs out of memory..

while(sysread(CSV, $record, 66)) { my $count = 0; my %map; $map{substr($record, 18, 14)}->{substr($record, 3, 15)} = subs +tr($record, 36, 29); while (sysread(CSV, $record, 66)) { $map{substr($record, 18, 14)}->{substr($record, 3, 15) +} = substr($record, 36, 29); if ($count++ > 1000) { $count = 0; last; } } &process(\%map); }

Replies are listed 'Best First'.
Re^5: Enforce Memory Cleanup
by davido (Archbishop) on Jun 18, 2014 at 05:27 UTC

    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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1090251]
Tanktalus is looking for any blogs or other sources of info on creating a "partial" cache - an incomplete cache that can tell the difference between data being present and known, data being absent and known, and data being unknown (we haven't looked for it
[Tanktalus]: yet, so it may or may not exist) in a reasonably fast and memory-efficient manner.

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2018-08-16 19:27 GMT
Find Nodes?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:

    Results (171 votes). Check out past polls.