in reply to releasing memory from a loop in Windows

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

Replies are listed 'Best First'.
Re^2: releasing memory from a loop in Windows
by ikegami (Patriarch) on Aug 07, 2006 at 20:33 UTC

    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. } }

      IMHO it uses "reference counting garbage collection". You don't have to allocate and deallocate memory, do you? I don't see why would the term "garbage collection" be used only for the "mark&sweep" and "copying" types.

      Except maybe that Microsoft, for marketing purposes, wanted people to believe .Net has something old VB did not have. So instead of saying that .Net has a mark&sweep garbage collector, while VB used a reference counting one, they want everyone to think, .Net has garbage collection, while VB did not.

        It's not just your opinion that considers "reference counting" as just one of several methods of implementing "gargabe collection", and the trend for Perlists to disown the term "garbage collection" as false elitism.

        See wikipedia "Reference counting" where the first application of "Reference counting" is "Garbage collection".

        See also Wikipedia "Garbage collection"/"Reference counting" where RCGC is contrasted with "Tracing GC".

        See also Wikipedia "Garbage Collection"/"Implementations" where is says:

        Script languages like Perl, Ruby, Python and PHP tend to have built-in support of GC.

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.