Note that because you don't use "my" here, all these variables will stay in memory even after the loop has finished.

That happens when using my too. Neither the SV nor the string buffer are freed. They are simply cleared.

Update: The following isn't proof, but it does let you see the effect of what I described.

use Devel::Peek qw( Dump ); sub foo { my ($x) = @_; undef($x) if !@_; Dump($x); } foo("abc"); foo("defghijkl"); foo("mno"); foo(undef); # $x = undef foo(); # undef $x foo("pqr");

Pay particular attention to LEN, the allocated length of the string buffer associated with the variable.

SV = PV(0x22612c) at 0x22606c REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x22f4d4 "abc"\0 CUR = 3 LEN = 4 SV = PV(0x22612c) at 0x22606c Could be coincidence, but isn't. REFCNT = 1 The SV wasn't freed. FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x182cadc "defghijkl"\0 New larger (LEN=12) str buf. CUR = 9 LEN = 12 SV = PV(0x22612c) at 0x22606c REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x182cadc "mno"\0 Same "large" (LEN=12) buf reused. CUR = 3 LEN = 12 SV = PV(0x22612c) at 0x22606c REFCNT = 1 FLAGS = (PADBUSY,PADMY) Undefined PV = 0x182cadc "mno"\0 Str buf untouched. CUR = 3 LEN = 12 SV = PV(0x22612c) at 0x22606c REFCNT = 1 FLAGS = (PADBUSY,PADMY) Undefined PV = 0 Str buf freed. SV = PV(0x22612c) at 0x22606c REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x22f4d4 "pqr"\0 New small (LEN=4) str buf. CUR = 3 Same address as earlier LEN = 4 is a coincidence.

In reply to Re^2: out of memory problem by ikegami
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.