in reply to Re: perl memory re-usage
in thread perl memory re-usage

Using undef will also free the memory for use in other parts of your program. However, it's not nearly as safe as using my because my properly cleans up when return and die are used. The second quote is neither incorrect nor misleading.

Replies are listed 'Best First'.
Re^3: perl memory re-usage
by perrin (Chancellor) on Aug 08, 2006 at 21:35 UTC
    Sorry, I think you've got it wrong. Lexical (my) variables do not free their memory when they go out of scope. That's what the first FAQ is saying, and it agrees with what many others have said here and elsewhere. For example, see this node by Dan Sugalski.
      I see what you mean
      use Devel::Peek qw( Dump ); { my $v1 = "test"; Dump($v1); } { my $v2 = "test"; Dump($v2); } { my $v3 = "test"; Dump($v3); } __END__ SV = PV(0x1abea7c) at 0x1ab2710 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1aa01f4 "test"\0 0x1aa01f4 CUR = 4 LEN = 5 SV = PV(0x1abea94) at 0x1ab26f8 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1aa01e4 "test"\0 0x1aa01e4 CUR = 4 LEN = 5 SV = PV(0x1abeaac) at 0x1ab2734 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1aa01d4 "test"\0 0x1aa01d4 CUR = 4 LEN = 5
      vs
      use Devel::Peek qw( Dump ); { my $v1 = "test"; Dump($v1); undef $v1; } { my $v2 = "test"; Dump($v2); undef $v2; } { my $v3 = "test"; Dump($v3); undef $v3; } __END__ SV = PV(0x1abea7c) at 0x1ab26d4 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1aa01f4 "test"\0 0x1aa01f4 CUR = 4 LEN = 5 SV = PV(0x1abea94) at 0x1ab26f8 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1aa01f4 "test"\0 0x1aa01f4 CUR = 4 LEN = 5 SV = PV(0x1abeaac) at 0x1ab26c8 REFCNT = 1 FLAGS = (PADBUSY,PADMY,POK,pPOK) PV = 0x1aa01f4 "test"\0 0x1aa01f4 CUR = 4 LEN = 5