Perl memory manager

N

Nishant

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
 
J

Joost Diepenmaat

Nishant said:
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.
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was sent to
Nishant
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
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top