in reply to How do I dumping Perls memory usage?
Are there any tips on efficient memory usage?
A few things to check for.
sub foo{ my ($hashref) = @_; for my $key (keys %{$hashref}) { do stuff; } } my %hash = (Gobs=>'many', of=>'loads', data=>'Huge'); ... foo( \%hash);
Instead of
sub foo{ my (%hash) = @_; #! Wrong! for my $key (keys %hash) { do stuff } } ... foo( %hash ); #! Wrong!
The second method will effectiviely double the memory usage for the hash. Go a third level deep that way and you triple it.
while( my ($key, $value)= each %hash) { do stuff; }
rather than for my $key (keys %hash) { do stuff; }
On a hash containing 500,000 4-char keys, the latter method will consume an extra 20MB to build the list, the former essentially causes no growth.
When doing unions, add the elements from the smaller of the two, to the larger rather than combining them into a third hash.
Similarly intersections. Delete the elements one set from the other set rather than building a third.
rather thanexists $b{$k} or delete $a{$k} while ($k,$v) = each %a;
exists $b{$k} and $c{$k}=$v while ($k,$v) = each %a
If you do have to build a third hash to contain the results of such an operation and you discard it after processing, declare the temporary hash at as close a local scope as possible to ensure timely release.
..and remember there are a lot of things monks are supposed to be but lazy is not one of them
|
|---|