#!/usr/bin/perl
use warnings;
use strict;
print "Press enter to start\n";
my $dummy = <>;
{
my $a = [];
my $i = 10000000;
push @$a,$i while ($i--);
print "Allocated first array - Press enter to let it go out of sco
+pe\n";
$dummy = <>;
}
print "Done. Press enter to continue with new array\n";
$dummy = <>;
{
my $a = [];
my $i = 10000000;
push @$a,$i while ($i--);
}
print "Done, press enter to exit\n";
$dummy = <>;
Running this on linux with perl 5.8.8 / usemyalloc=n will reuse (most of) the memory used by the first @$a array. You can also see it free some of it to the OS if you watch top.
Update: as far as I know, this only works for data referenced by lexicals - there are other ways to free memory used by arrays (i.e. setting $#array)
|