Memory allocation

J

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?

Thanks!

- John
 
T

Tassilo v. Parseval

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
 
J

John Blevin

Tassilo v. Parseval said:
Also sprach John Blevin:


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
 
B

Ben Morrow

John Blevin said:
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
 
C

Chris Mattern

John said:
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
 
W

Walter Roberson

:> 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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top