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

In reply to Re: perl memory re-usage by radiantmatrix
in thread perl memory re-usage by mreece

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.