in reply to Lost anonymous subs

A template copy of your code remains because it is used when creating the value to be assigned to $code. That template does not contain a copy of @arr. Also, the value of $code is GCed. Also, when $code was assigned, it didn't recieve a copy - it got a pointer to the current binding of @arr at the time. That pointer is also GCed and if the value in @arr has no more references, it also is GCed.

In short, your code will behave itself. The now unused memory will be known to the perl interpreter as unused memory and it is now up to perl about whether it will return it to the operating system or not. I'm fuzzy on this but I recall others saying that some operating systems (like Windows) allow perl to return memory and perhaps others don't allow perl to return the memory. In any case, if you were to need that memory for other data in your program, it would be re-used for whatever that new use was.

Replies are listed 'Best First'.
Re^2: Lost anonymous subs
by kappa (Chaplain) on Dec 09, 2004 at 15:30 UTC
    Thanks for information! Things got clearer. And what about closure behaviour? The sub WILL need a copy of @arr, won't it? I cannot understand what «a pointer to the current binding of @arr» is, pardon my ignorance, the term is not in perlref or perlsub :(
    --kap
      This is all perl internals, perlguts stuff. The CV you get in $code has an ->OUTSIDE C pointer to the PADLIST that @arr lives in and then the compiled code has a note on which index in that PADLIST it is suppposed to access the @arr at. So it isn't a copy. The code in $code works with the real, original @arr, not a copy.
        Please, say that the comments down there in the code are right and I will rest calm, thankful and a bit more knowledgable :)
        my $code; { my @arr = (9) x 1000_000; $code = sub { @arr }; } # here @arr got out of the scope but is not freed as # there's a ref from inside $code closure # which enables $code do things with the array when called undef $code; # only now that million of nines has gone away # it always existed in exactly one copy
        --kap