thanks, but i am trying to determine the circumstances under which perl will or will not re-use the memory allocated for one variable in one scope for another variable in another scope (not release it to the OS, just re-use it within the same perl process).
it appears that for space used for the contents of arrays and hashes will get re-used, but space allocated for scalars is reserved for that same scalar.
thus, given
sub one { my $a = 'x' x 1024; my @b = ($a) x 102400; }
sub two { my $c = 'x' x 1024; my @d = ($c) x 102400; }
sub three { my $w = 'x' x 1024; my $x = $w x 102400; }
sub four { my $y = 'x' x 1024; my $z = $y x 102400; }
calling one(); two(); will allocate a large chunk for @b and re-use that same large chunk for @d (though not _all_ space is re-usable so the process does grow a little more).
calling three(); four(); will allocate a large chunk for $x, then another large chunk for $z, and those memory allocations are not available for re-use within this same process (except for subsequent calls to the same subroutine).
|