On 10 May 2007, at 23:25, Lionel MARTIN wrote:
OK, fine.
So, to sum up, if I have got 10 different scripts in a mod perl
environment (let's call them test1.pl....test10.pl), and using
lexical variables there.
If I first run test1.pl and then, run test2.pl, the only way for
test2.p to get access to the memory used by test2.pl is freeing up
test1.pl lexical variables, by undefining them?
And what if I run test1.pl twice without undefining its lexical
variables? Will the same memory space be used twice, or will each
call use different memory space (I'm talking here about situation
where the same Perl interpreter is running the script twice in a
mod perl environment)?
The memory allocated /directly/ to a lexical remains allocated at the
end of the scope on the assumption that it might be needed again in
the future. That's so that when you call something like
sub string_builder {
my $s = '';
$s .= 'x' for 1 .. 1_000_000;
return $s;
}
repeatedly it's able to reuse memory from the previous invocation
rather than allocating it all from scratch each time. That's mainly a
performance optimisation.
However memory referred to indirectly as in your example
$x = [ 1 .. 1_000_000 ];
is freed as soon as nothing refers to it any more. So when $x goes
out of scope that storage /is/ freed - to Perl at least.
Please don't get the idea that Perl never frees anything and please
don't start littering your code with unnecessary explicit memory
management.
--
Andy Armstrong, hexten.net