monktim has asked for the wisdom of the Perl Monks concerning the following question:

According to the Learning Perl book, truncating an array does not recover its memory. To feee memory I need to undef the array like this undef(@array). It also says that the memory doesn't always get free because not many operating systems support this. I find this strange as I've never had this problem in other languages.

I'm using Win2K and not much memory gets free when I undef my array. When I run the script below the @array takes ~76,000K in memory when I hit the breakpoint. After undef(@array) gets executed the memory only goes down to ~72,000K. If I continue the loop and watch memory it'll fluctuate between these two numbers.

Is there another way to free the memory? Does anyone else find this behavior strange? I must be missing something, perhaps some monks can enlighten me.


use strict; use warnings; print $$; my @array; my $i = 0; while (1) { $array[$i++]= '1234567890'; if ($i == 1_000_000) { undef(@array); #BREAKPOINT HERE $i = 0; } }
The memory is even kept after the array goes out of scope.
use strict; use warnings; print "PID: $$\n"; { my @array; my $i = 0; while ($i <= 1_000_000) { $array[$i++]= '1234567890';# for([0..1_000_000]); if ($i == 1_000_000) { undef(@array); } } } print "END\n"; # BREAKPOINT HERE, MEMORY IS STILL NOT FREE