First, a suggestion: supply more information! Please read the section of the PerlMonks FAQ on "How do I post a question effectively?".

After poking through the Camel Book, esp. the pages (299-301 in my copy, which is the Sep 1996 version of the 2d edition), it says "When a block is exited, my variables are normally freed up." (italics mine). I suspect that means that for a loop like this:

foreach(@file){ open(my $fh, "<", "$file") or die "Could not open $file because $! +\n"; my @data = <$fh>; close($fh); } #memory used for @data gets garbage collected here

memory doesn't get garbage collected until after the comment (or at the fragments's die statement)

I understand this to mean that code like the fragment above and this would g/c in the same way

{ my @data; foreach(@file){ open(my $fh, "<", "$file") or die "Could not open $file becaus +e $!\n"; @data = <$fh>; close($fh); } }

added in update

My understanding would also imply that the memory used will be based on the largest file processed in the loop. Also, Perl doesn't return memory to the O/S until it exits, so the (kilo|mega|giga|tera|peta|exa)byte or so you've used to read the largest of the files is kept in Perl's hot little hands.

end of addition

Of course, there is a non-zero chance I'm wrong, and totally in left field (US idiom meaning "my answer is not only wrong but inane.").

emc

Experience is a hard teacher because she gives the test first, the lesson afterwards.

Vernon Sanders Law

In reply to Re: releasing memory from a loop in Windows by swampyankee
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.