Perl memory manager

Discussion in 'Perl Misc' started by Nishant, Jan 3, 2008.

  1. Nishant

    Nishant Guest

    Hi ,
    My understanding about the perl memory manager is that it initially
    allocates a chunk of memory and allocates more memory when required.
    but it does not return the whole memory to the OS. Rather, it keeps it
    with itself ,so that the next time it has to use memory , it can use
    it from the already chunk of memory that it has. Is my understanding
    correct ? If yes, in what all scenarios does perl return memory back
    to OS and scenarios in which it does not return.
    Also, what is the size of the chunk of the memory that perl allocates
    at one time. Is it variable ?

    Regards
    Nishant Sharma
     
    Nishant, Jan 3, 2008
    #1
    1. Advertising

  2. Nishant <> writes:

    > Hi ,
    > My understanding about the perl memory manager is that it initially
    > allocates a chunk of memory and allocates more memory when required.
    > but it does not return the whole memory to the OS. Rather, it keeps it
    > with itself ,so that the next time it has to use memory , it can use
    > it from the already chunk of memory that it has. Is my understanding
    > correct ?


    In general, yes, your understanding is correct.

    > If yes, in what all scenarios does perl return memory back
    > to OS and scenarios in which it does not return.


    In my experience it depends on the OS and possibly also on the malloc
    implementation that your perl interpreter is configured with.

    As a quick guideline you should assume *no* memory is ever returned to
    the OS except when the program exits or is replaced by another program
    via exec.

    In certain cases you can get memory back to the OS by just letting
    variables go out of scope, for instance:

    for (@someting) {
    my @x = some_really_long_strings();
    }

    may or may not release @x's memory to the OS when the loop is done.

    Sometimes explicitly undefining collections works when scoping by itself
    doesn't:

    my @x;
    for (@someting) {
    @x = some_really_long_strings();
    }
    undef @x;

    This works at least in some cases on some versions of perl on some linux
    systems. You'll just have to test it yourself to be sure.

    > Also, what is the size of the chunk of the memory that perl allocates
    > at one time. Is it variable ?


    I'm pretty sure it's variable. Allocating hundreds of small chunks just
    to get a single big chunk would be stupid. Things like arrays, strings
    and hashes all need continous chunks of memory of unpredictable sizes.

    Joost.
     
    Joost Diepenmaat, Jan 3, 2008
    #2
    1. Advertising

  3. [A complimentary Cc of this posting was sent to
    Nishant
    <>], who wrote in article <>:
    > Hi ,
    > My understanding about the perl memory manager is that it initially
    > allocates a chunk of memory and allocates more memory when required.
    > but it does not return the whole memory to the OS. Rather, it keeps it
    > with itself ,so that the next time it has to use memory , it can use
    > it from the already chunk of memory that it has. Is my understanding
    > correct ?


    No. What you wrote is applicable to Perl configured with mymalloc.
    Perl's compiled with system malloc() will behave differently.

    > Also, what is the size of the chunk of the memory that perl allocates
    > at one time. Is it variable ?


    This is kinda documented somewhere. About 10 years ago, mymalloc
    default was to start with about 48K, then increase in >= 3% increments.
    Configurable at compile time.

    Hope this helps,
    Ilya
     
    Ilya Zakharevich, Jan 3, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John
    Replies:
    0
    Views:
    399
  2. Calum Grant

    ANN: Lightwave Memory Manager 1.0

    Calum Grant, Mar 3, 2005, in forum: C++
    Replies:
    0
    Views:
    310
    Calum Grant
    Mar 3, 2005
  3. padma
    Replies:
    0
    Views:
    354
    padma
    Oct 3, 2007
  4. Metalone
    Replies:
    0
    Views:
    346
    Metalone
    Jan 6, 2010
  5. Replies:
    4
    Views:
    128
Loading...

Share This Page