No such thing as a small change | |
PerlMonks |
Re: Avoiding Memory Leaksby kennethk (Abbot) |
on Sep 23, 2013 at 20:37 UTC ( [id://1055381]=note: print w/replies, xml ) | Need Help?? |
Your recollection is correct: garbage collection should happen following both delete $dtgs{$dtg}; and $dtgs{$dtg} = \@files;, since both remove an array ref from %dtgs.
Depending on your array sizes, you may get more efficiency from a splice instead of your grep, since you need to create a new array and copy nearly all the old file names. That code might look something like
If you know lists are unique (no repeats) and that this is the only routine that modifies the arrays, you can add some Loop Control and do a little better:
Note in the original, you missed parentheses in your test, and that all logical tests are scalar context, so the scalar is unnecessary. Of course, this is an optimization, so make sure to actually test (perhaps with Devel::NYTProf) rather than guess at what's slow. Update: Or, of course, given a uniqueness constraint, you could just use a hash:
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
In Section
Seekers of Perl Wisdom
|
|