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

sub remove_from_dtgs { my ($dtg,$file) = @_; for my $i (reverse 0 .. $#files) { splice @{$dtgs{$dtg}}, $i, 1 if $dtgs{$dtg}[$i] eq $file; } delete $dtgs{$dtg} unless @{$dtgs{$dtg}}; }

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:

sub remove_from_dtgs { my ($dtg,$file) = @_; for my $i (reverse 0 .. $#files) { if ($dtgs{$dtg}[$i] eq $file) { splice @{$dtgs{$dtg}}, $i, 1; delete $dtgs{$dtg} unless @{$dtgs{$dtg}}; last; } } }

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:

open(my $dtg_file, "<", $infile) or die "Unable to open $infile: $!\n" +; while(<$dtg_file>) { chomp; my ($dtg,@files) = split /:/; $dtgs{$dtg}{$_}++ for @files; } close $dtg_file; sub remove_from_dtgs { my ($dtg,$file) = @_; delete $dtgs{$dtg}{$file}; delete $dtgs{$dtg} if !keys %{$dtgs{$dtg}} }

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: Avoiding Memory Leaks by kennethk
in thread Avoiding Memory Leaks by wink

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.