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


In reply to Re^2: perl memory re-usage by mreece
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.