in reply to Re^2: wait till memory is flushed
in thread wait till memory is flushed

I'm sorry if I haven't been clear before; I'll try to explain it with more words this time.

Let's say you have 1G of memory; you build a huge data structure in a hash; for that perl allocates 300M of memory from the operating system.

You store that hash in a lexical variable my %h = .... When this variable goes out of scope, perl internally marks the memory as "not needed anymore". However it doesn't give the memory back to the OS, because it suspects that you might use more memory later on in your program.

Now if you wait for 30s, perl decides that it can give the memory back, because apparently you don't need it anymore.

On the other hand if you don't wait, but allocate another huge data structure, perl will use the memory that %h occupied before, and only when that is exhausted it will ask the operating system for more memory.

So if your program has the form

* use up 300M of RAM * let the variables go out of scope that point to the memory * build a data structure that needs 400M

then the last step only increases the memory usage of the perl script by 100M, because it uses the 300M first that was marked as not-needed-anymore.

This is why I wrote that you shouldn't bother to sleep before doing the second task - perl will not waste your memory, unless you do something stupid (like keep a reference to the first, huge hash).

Replies are listed 'Best First'.
Re^4: wait till memory is flushed
by Boetsie (Sexton) on May 19, 2010 at 11:37 UTC

    Thanks for the replies.

    I get it now. I can also remove my pause then. I was looking with ītopī for my memory and after the function it kept the memory at the same percentage, which I thought was strange. Now I know why, and I should not bother anymore. Thanks for your explanation.

    Marten