Memory allocation

Discussion in 'Perl Misc' started by John Blevin, Jan 26, 2004.

  1. John Blevin

    John Blevin Guest

    Let's say my program has built up a fairly large "hash of hashes"
    data structure, rooted at %my_hash. If I then say

    %my_hash = ();

    this wipes out the structure, but what happens to the
    memory that had been allocated? Is it freed, or is
    it still allocated?

    Thanks!

    - John
    John Blevin, Jan 26, 2004
    #1
    1. Advertising

  2. Also sprach John Blevin:

    > Let's say my program has built up a fairly large "hash of hashes"
    > data structure, rooted at %my_hash. If I then say
    >
    > %my_hash = ();
    >
    > this wipes out the structure, but what happens to the
    > memory that had been allocated? Is it freed, or is
    > it still allocated?


    It's freed (but not necessarily handed back to the operating system).
    Most often the memory will be re-used by perl whenever it would need to
    allocate new memory.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    Tassilo v. Parseval, Jan 26, 2004
    #2
    1. Advertising

  3. John Blevin

    John Blevin Guest

    "Tassilo v. Parseval" wrote:
    >
    > Also sprach John Blevin:
    >
    > > Let's say my program has built up a fairly large "hash of hashes"
    > > data structure, rooted at %my_hash. If I then say
    > >
    > > %my_hash = ();
    > >
    > > this wipes out the structure, but what happens to the
    > > memory that had been allocated? Is it freed, or is
    > > it still allocated?

    >
    > It's freed (but not necessarily handed back to the operating system).
    > Most often the memory will be re-used by perl whenever it would need to
    > allocate new memory.


    OK, thanks. Is there a way to force it to hand the memory back to
    the operating system? (My program is using huge amounts of memory,
    sometimes running out of memory...)

    - John

    >
    > Tassilo
    > --
    > $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    > pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    > $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
    John Blevin, Jan 26, 2004
    #3
  4. John Blevin

    Ben Morrow Guest

    John Blevin <> wrote:
    > OK, thanks. Is there a way to force it to hand the memory back to
    > the operating system? (My program is using huge amounts of memory,
    > sometimes running out of memory...)


    No. This is not a perl question: on most OSen, programs cannot give
    memory back to the OS. It would not help, in any case: perl will reuse
    the memory it has freed before allocating any more.

    Ben

    --
    "If a book is worth reading when you are six, *
    it is worth reading when you are sixty." - C.S.Lewis
    Ben Morrow, Jan 26, 2004
    #4
  5. John Blevin wrote:
    > "Tassilo v. Parseval" wrote:
    >
    >>Also sprach John Blevin:
    >>
    >>
    >>>Let's say my program has built up a fairly large "hash of hashes"
    >>>data structure, rooted at %my_hash. If I then say
    >>>
    >>>%my_hash = ();
    >>>
    >>>this wipes out the structure, but what happens to the
    >>>memory that had been allocated? Is it freed, or is
    >>>it still allocated?

    >>
    >>It's freed (but not necessarily handed back to the operating system).
    >>Most often the memory will be re-used by perl whenever it would need to
    >>allocate new memory.

    >
    >
    > OK, thanks. Is there a way to force it to hand the memory back to
    > the operating system?


    Generally not. Usually memory allocated by a process isn't handed
    back to the system until the process ends.

    > (My program is using huge amounts of memory,
    > sometimes running out of memory...)


    One technique that sees use sometimes is to fork() a new copy of
    the process and then have the parent terminate. The new copy
    only allocates enough space to hold what it's currently using,
    and the old copy surrenders all its memory when it terminates.
    Of course, you'll need to have enough free memory to do the
    fork()...

    Chris Mattenr
    Chris Mattern, Jan 26, 2004
    #5
  6. In article <bv3ic0$7v3$>,
    Ben Morrow <> wrote:

    :John Blevin <> wrote:
    :> OK, thanks. Is there a way to force it to hand the memory back to
    :> the operating system? (My program is using huge amounts of memory,
    :> sometimes running out of memory...)

    :No. This is not a perl question: on most OSen, programs cannot give
    :memory back to the OS.

    Unix systems -can- give back memory, but if the memory was allocated
    by the standard memory allocator, it's too hard to know whether the
    memory is still in use or not. There are alternative memory allocation
    libraries that are available to make it easier to give back memory.

    In other words, for Unix, it isn't that it isn't possible to give back
    memory at all, it's that the program has to be specially constructed in
    order to give back memory without crashing. And most of the alternative
    allocators that I've heard of don't even try using the official
    deallocation procedure sbreak(): they instead use mmap() to allocate in
    a different pool and manage that pool.
    --
    Scintillate, scintillate, globule vivific
    Fain would I fathom thy nature specific.
    Loftily poised on ether capacious
    Strongly resembling a gem carbonaceous. -- Anon
    Walter Roberson, Jan 26, 2004
    #6
    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. s.subbarayan

    Dynamic memory allocation and memory leak...

    s.subbarayan, Mar 18, 2005, in forum: C Programming
    Replies:
    10
    Views:
    677
    Eric Sosman
    Mar 22, 2005
  2. Rodrigo Dominguez

    memory allocation and freeing memory

    Rodrigo Dominguez, Jun 13, 2005, in forum: C Programming
    Replies:
    11
    Views:
    579
    Jean-Claude Arbaut
    Jun 15, 2005
  3. Ken
    Replies:
    24
    Views:
    3,832
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    971
    chris
    Oct 28, 2005
  5. Bjarke Hammersholt Roune
    Replies:
    14
    Views:
    1,167
    Bjarke Hammersholt Roune
    Mar 6, 2011
Loading...

Share This Page