First of all, Perl doesn't use garbage collection — it uses reference counting — so to say @data is *garbage collected* is wrong. Use the more general term *freed* instead.

The general rule is that variables are freed at the end of the scope in which they are declared, and new ones are created the next time the scope is entered. In this case, the end of the scope is the end of the loop pass. (It is not the end of the loop.) That means @data gets freed at the end of every loop pass, so there is no accumulation of memory use. There's an optimization at play, but it is of no consequence.

The exception is when a reference to a variable survives the scope in which the variable is declared. In the following, @data and its contents are NOT freed at the end of every loop pass, because a reference to the array survives the loop.

my %all_files; foreach my $file (@files) { open(my $fh, "<", $file); my @data = <$fh>; $all_files{$file} = \@data; close($fh); }

By the way, the close($fh) is redundant, since freeing $fh closes the file handle.

Caveat

To speed things up, Perl loops free the contents of variables instead of freeing the variables themselves at the end of every loop pass. This is completely transparent and does not use up any extra memory. In this case, it means

foreach my $file (@files) { open(my $fh, "<", $file; my @data = <$fh>; close($fh); }

is equivalent to

{ my @file; my $fh; foreach my $file (@files) { open($fh, "<", $file); @data = <$fh>; close($fh); undef $fh; # Frees $fh's content. undef @data; # Frees @data's content. } }

In reply to Re^2: releasing memory from a loop in Windows by ikegami
in thread releasing memory from a loop in Windows by Anonymous Monk

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.