richardwfrancis has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks
I wish to be enlightened by your collective wisdom once more, please
I've tried to simplify the problematic area of my code below
The trouble I am experiencing is that between the loops, even though I am resetting my hashref, I don't seem to be freeing up the memory it occupied.
If you run the code while looking at "top" in Linux or some equivalent, you'll see that after the first loop, even though the hashref is reset on line 89, there's still a fair chunk in memory
I've no idea what I'm doing wrong here. PLEASE can someone tell me what I'm doing wrong.
I've commented the code to show you what it is doing and note that this code will take about 25seconds (cos of the sleeps) and use up about 4GB RAM. If you need it to use less of both then adjust the sleeps and decrease the values on lines 12 and 13
MANY thanks in advance for any advice
Kind regards,
Rich
#!/usr/bin/perl use strict; use Devel::Size qw(total_size); # creating a reference to an anonymous hash my $coverage = {}; # I'm going to run a loop twice with these values my @times = qw(once twice); # some data to use to fill an array in the sub my %lengths = ( "id1" => 50000000, "id2" => 50000000, ); # keeping track of the estimated memory size of the coverage hashref my $total_size = total_size($coverage); print "size before the loops = $total_size\n"; # OK run the sub twice with different parameters foreach my $t (@times){ if ($t eq "once"){ # I'm doing stuff here that's conditional on $t # then running the sub &doWork($t); } elsif ($t eq "twice"){ # I'm doing stuff here that's conditional on $t # then running the sub &doWork($t); } # keeping track of the estimated memory size of the coverage hashref my $total_size = total_size($coverage); print "size after $t sub = $total_size\n"; # reset the hashref so that memory should be small again %{ $coverage } = (); # keeping track of the estimated memory size of the coverage hashref my $total_size = total_size($coverage); print "size after resetting the hashref = $total_size\n"; # while total_size reports that the hashref is small, top shows that +the program is still hogging memory # what am I doing wrong???? print "what's the mem doing?\n"; sleep(10); } sub doWork { my $time = shift; # keeping track of the estimated memory size of the coverage hashref my $total_size = total_size($coverage); print "size at the start of $time = $total_size\n"; # the main work in the sub is to fill a large reference to an array foreach my $l (keys %lengths){ print "creating $l\n"; push @{ $coverage->{$l} }, 0 for(1 .. $lengths{$l}); } # keeping track of the estimated memory size of the coverage hashref my $total_size = total_size($coverage); print "size after cov made in $time = $total_size\n"; sleep(5); # then the sub goes off and does some work that gives back some coordi +nates to use to increment particular values in the array created abov +e # get some data to work with my %increments = { "id1" => qw(400-500,6000-6500,100000-100500), "id2" => qw(400-500,6000-6500,100000-100500) }; # then the values in the array are incremented and passed back to the +main program to do something with it print "incrementing\n"; foreach my $i (keys %increments){ foreach my $p (@{ $increments{$i} }){ my ($s,$e) = split("-",$p); $coverage->{$i}->[$_]++ for ($s - 1 .. $e - 1); } } # keeping track of the estimated memory size of the coverage hashref my $total_size = total_size($coverage); print "size after increments made in $time = $total_size\n"; sleep(5); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unexplained memory hogging
by RichardK (Parson) on Aug 22, 2014 at 17:12 UTC | |
|
Re: Unexplained memory hogging
by Laurent_R (Canon) on Aug 22, 2014 at 17:45 UTC | |
|
Re: Unexplained memory hogging
by zentara (Cardinal) on Aug 22, 2014 at 18:12 UTC | |
by Laurent_R (Canon) on Aug 22, 2014 at 20:56 UTC | |
by zentara (Cardinal) on Aug 23, 2014 at 12:57 UTC | |
by Anonymous Monk on Aug 22, 2014 at 23:38 UTC | |
by zentara (Cardinal) on Aug 23, 2014 at 12:41 UTC | |
by richardwfrancis (Beadle) on Aug 23, 2014 at 20:54 UTC | |
|
Re: Unexplained memory hogging
by Anonymous Monk on Aug 22, 2014 at 23:34 UTC |