in reply to Re: perl memory re-usage
in thread perl memory re-usage

As I understand it, Perl will not reuse memory allocated for one lexical on another lexical, which means the second one is not really correct.

Replies are listed 'Best First'.
Re^3: perl memory re-usage
by Joost (Canon) on Aug 08, 2006 at 21:51 UTC
    Not always true:
    #!/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)