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

i have a question about if/when perl reuses memory allocated for scoped lexicals.

specifically, a question regarding conflicting answers to the FAQ "How can I free an array or hash so my program shrinks?"

according to http://perldoc.perl.org/perlfaq3.html (which claims to be Perl 5.8.8 documentation):

You usually can't. Memory allocated to lexicals (i.e. my() variables) cannot be reclaimed or reused even if they go out of scope. It is reserved in case the variables come back into scope. Memory allocated to global variables can be reused (within your program) by using undef()ing and/or delete().
however, according to 'perldoc perlfaq3' on my perl 5.8.6,
However, judicious use of my() on your variables will help make sure that they go out of scope so that Perl can free up that space for use in other parts of your program.
is one of these incorrect or outdated? or are they both correct for their respective versions?
(that is, allocations for scoped lexical is reused in 5.8.6 but not 5.8.8?)

Replies are listed 'Best First'.
Re: perl memory re-usage
by Corion (Patriarch) on Aug 08, 2006 at 20:56 UTC

    The two articles are talking about different things. The FAQ entry on shrinking the program talks about giving back memory to the operating system, which basically doesn't happen, or rather, depends on the OS and the memory allocation strategy built into Perl. The second entry talks about internal reuse of the memory allocated from the OS.

    So, both are true, the memory gets reused by Perl but not (necessarily) given back to other programs.

      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.
        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)

Re: perl memory re-usage
by eyepopslikeamosquito (Archbishop) on Aug 08, 2006 at 21:45 UTC
      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).

Re: perl memory re-usage
by radiantmatrix (Parson) on Aug 09, 2006 at 20:16 UTC

    A lot of the above comments seem to be missing something, though I confess I had to read the question a few times to really see the issue -- it's not about whether memory gets released back to the OS.

    The 5.8.6 version of the FAQ seems to claim that the memory reserved for lexicals during a given run is released back to the interpreter when they go out of scope, and other lexicals later can use that recycled memory.

    The 5.8.8 version of the FAQ seems to suggest that once you reserve memory for a lexical, it always belongs to that lexical, even after it goes out of scope. In other words, the more different lexicals you have in your code -- even if they aren't ever in the same scope -- the more memory your program will use.

    In other words, the implication is that given this code:

    sub one { my $large_struct = get_1MB_from($dbh); do_stuff_on(\$large_struct); } sub two { my $other_struct = get_1MB_from($dbh).get_1MB_from($dbh2); do_stuff_on(\$other_struct); } one; two;

    As the FAQ is written for 5.8.6, the application would end up consuming 2MB of memory (+overhead, of course) as a result of the declared lexicals: 1MB gets allocated inside one(), then released to perl, then 2MB gets allocated inside two().

    According to the 5.8.8 wording, the same code would take 3MB: 1MB gets allocated inside one(), and perl hangs on to it, even though the lexical has gone out of scope. When two() is called, that 1MB is still reserved "just in case" one() is called again, and an additional 2MB gets allocated.

    That's what the docs appear to say: if it's really the case, then using lexicals could actually hurt applications that need to be careful about memory, and this would encourage developers not to trust perl to do the memory management, instead trying to do it themselves with globals.

    That would, frankly, piss me off a bit. So, those who are perl internal gurus, what does 5.8.8 really do when a lexical goes out of scope?

    <radiant.matrix>
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
      According to the 5.8.8 wording, the same code would take 3MB:

      I believe that this has always been the case since (at least) 5.6.1 (and probably before). This is one of the places perl trades space for speed.

      It's only with 5.8.8 that the documentation has correctly documented the reality.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
      So, those who are perl internal gurus, what does 5.8.8 really do when a lexical goes out of scope?

      I'm no perlguts guru, but I showed the memory is not being released unless the variable is explicitely undefed. I ran my test on ActivePerl 5.6.1 and 5.8.8.

      thank you, thank you; that is exactly my concern.
Re: perl memory re-usage
by perrin (Chancellor) on Aug 08, 2006 at 21:16 UTC
    I think the second quote is incorrect, or at least misleading. The first fits with what others have told me, although I thought that undef would release memory on a lexical as well as a global.

      Using undef will also free the memory for use in other parts of your program. However, it's not nearly as safe as using my because my properly cleans up when return and die are used. The second quote is neither incorrect nor misleading.

        Sorry, I think you've got it wrong. Lexical (my) variables do not free their memory when they go out of scope. That's what the first FAQ is saying, and it agrees with what many others have said here and elsewhere. For example, see this node by Dan Sugalski.