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