If i pause for 30 seconds the memory is empty (<1%) again. So the 30% memory will probably remove after some time? The reason for the question is that if we have even more data, we are worried that the second function canīt run because of too few memory left.
| [reply] |
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). | [reply] [d/l] [select] |
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
| [reply] |
I tried this code. It allocated 13% of my memory at the beginning, and it stayed at this level until the end, which means new invocation of the function does not increase the memory consumption.
sub test {
my $size = 1000000;
my %hash;
@hash{1..$size} = (rand()) x $size;
sleep 5;
print keys %hash;
}
test(),sleep 5 for 1..10;
| [reply] [d/l] |