Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Perl memory manager (http://www.velocityreviews.com/forums/t905942-perl-memory-manager.html)

Nishant 01-03-2008 12:26 PM

Perl memory manager
 
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

Joost Diepenmaat 01-03-2008 02:35 PM

Re: Perl memory manager
 
Nishant <nishant.031@gmail.com> 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.

Ilya Zakharevich 01-03-2008 09:07 PM

Re: Perl memory manager
 
[A complimentary Cc of this posting was sent to
Nishant
<nishant.031@gmail.com>], who wrote in article <ebe644b7-62d8-463f-b378-097d8a366bed@i12g2000prf.googlegroups.com>:
> 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


All times are GMT. The time now is 02:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.