The three x expressions are constant folded at compile time

Nice theory... but if the size to allocate isn't known before run-time, you still do get the same behavior.  Even in slightly modified cases like these, where the $y should definitely go out of scope (and thus the memory behind it be available for reuse):

$ strace -e mmap,munmap perl -e '$n=2**20; $x="x" x $n; {my $y=$x}{my +$y=$x}{my $y=$x}' ... mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7fa5d43d3000 mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7fa5d2ecd000 mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7fa5d2dcc000 mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7fa5d2ccb000 mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7fa5d2bca000 munmap(0x7fa5d2bca000, 1052672) = 0 munmap(0x7fa5d2ccb000, 1052672) = 0 munmap(0x7fa5d2dcc000, 1052672) = 0 munmap(0x7fa5d43d3000, 1052672) = 0

Personally, I'd consider this a bug.

Note that if you write for (1..3) {my $y = $x} instead of spelling out {my $y = $x} three times, the memory is being reused just fine:

$ strace -e mmap,munmap perl -e '$n=2**20; $x="x" x $n; for (1..3) {my + $y=$x}' ... mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7f9d9bdfd000 mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7f9d9a8f7000 mmap(NULL, 1052672, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, - +1, 0) = 0x7f9d9a7f6000 munmap(0x7f9d9a7f6000, 1052672) = 0 munmap(0x7f9d9bdfd000, 1052672) = 0

In reply to Re^2: Freeing memory, revisited (Linux) by Eliya
in thread Freeing memory, revisited (Linux) by lkundrak

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.