You're right. The reuse of the address could be coincidence, but as the LEN stay 12, we can conclude that the buffer was never freed.

I've done a little bit of testing too:

sub foo{ my $var = 'xyz' x 1_000_000; } sub bar{ my $var2 = 'xyz' x 1_000_000; } warn "ONE\n"; foo(); warn "TWO\n"; foo(); warn "THREE\n"; bar(); warn "FOUR\n";

Running this through strace gives this output:

[...] write(2, "ONE\n", 4) = 4 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb7956000 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb7679000 write(2, "TWO\n", 4) = 4 write(2, "THREE\n", 6) = 6 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb739c000 mmap2(NULL, 3002368, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, +-1, 0) = 0xb70bf000 write(2, "FOUR\n", 5) = 5

This shows that perl does not allocate memory between "TWO" and "THREE" (the variable in sub foo gets reused), but it does allocate new memory before running bar, even though it could reuse $var's memory for $var2.

Including system('ps', '-Orss', '-p', $$) and die "$! $?"; in various places leads to the same conclusion without strace.

One question remains: When does perl free memory? I can't quite believe that every lexical variable that has ever been used results in large stale memory blocks. This would be against the spirit of this excerpt from perlsyn:

You wouldn't want memory being free until you were done using it, or kept around once you were done. Automatic garbage collection takes care of this for you.

In reply to Re^3: out of memory problem by betterworld
in thread out of memory problem by lightoverhead

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.