#!/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 coordinates to use to increment particular values in the array created above # 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); }