Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

This post answers a few common questions and misconceptions about Perl's memory management.

Lexical variables don't get freed at end of scope

Lexical variables don't get freed at end of scope. They are cleared on scope exit to give you the apperance of a fresh variable. This is an optimisation that should be transparent to you.

On scope exit, if anything prevents the variable from being resused (e.g. if there are remaining references to the variable), the existing variable becomes anonymous and a new variable is allocated.

Clearing a variable does not free the memory it uses

It was previously mentioned that variables on cleared on scope exit. Variables can also be cleared as follows:

$s = undef; @a = (); %h = ();

Clearing a variable frees no memory directly. The buffer of a string is marked as being unused, but it remains allocated for later use. The underlying array of pointers in arrays and hashes is cleared, but it remains allocated for later use. This is an optimisation that should be transparent to you.

As a result of a variable being cleared, referenced variables (incl array elements) have their refcount lowered, and that may free those variables. But not the variable being cleared.

undef does not free a variable

undef can be used to clear a variable.

undef $s; undef @a; undef %h;

undef does do more than clear the variable. It also frees any buffers the variable has.

$ perl -MDevel::Peek -e' $s = "abc"; Dump $s; $s = undef; Dump $s; undef $s; Dump $s; ' SV = PV(0x814fb00) at 0x814f69c REFCNT = 1 FLAGS = (POK,pPOK) <-- Var contains a string PV = 0x81651b8 "abc"\0 CUR = 3 LEN = 4 SV = PV(0x814fb00) at 0x814f69c REFCNT = 1 FLAGS = () <-- Var is undefined PV = 0x81651b8 "abc"\0 <-- String buffer still CUR = 3 allocated after clearing LEN = 4 SV = PV(0x814fb00) at 0x814f69c REFCNT = 1 FLAGS = () <-- Var is undefined PV = 0 <-- undef freed the buffer

This function is rarely useful.

However, undef will never free a variable. Clearing the variable may result in the reduction of the ref counts of other variables, which may free those variables. But not the variable that was passed to undef.

Freeing variables may result in no change in available system memory

You can't rely on memory being returned to the system, but it can happen.

If and when memory is returned to the OS is dependant on the memory allocation mechanism perl was compiled to use. You may also see differences between system using the same memory allocation mechanism. It's also possible that a mechanism will only ever release the large blocks it allocated.

The default memory allocation mechanism varies by OS. You are more likely to see memory being released to the OS on Windows.


In reply to Mini-Tutorial: Perl's Memory Management by ikegami

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (6)
    As of 2014-04-17 02:20 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (437 votes), past polls