in reply to Long running tasks, perl and garbage collection

Actually it depends on how you allocate memory and on malloc implementation. E.g. on linux malloc may use to allocate memory brk if you asking some small amount or mmap if you wanna much of it. Memory allocated by brk usually can't be freed, but mmaped memory may be easily munmaped. Here's example that demonstrates this:

use strict; use warnings; my $hr = {}; my ($num, $size) = @ARGV; print "$num, $size\n"; system("ps vp $$"); for (1..$num) { $hr->{$_} = 'memory' x $size; } system("ps vp $$"); undef $hr; system("ps vp $$");

On my Ubuntu amd64 I got the following results:

$ perl memory.pl 10000 1000 10000, 1000 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 14098 pts/0 S+ 0:00 0 3 18164 2292 0.0 perl memory.p +l 10000 1000 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 14098 pts/0 S+ 0:00 0 3 78204 62436 1.5 perl memory.p +l 10000 1000 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 14098 pts/0 R+ 0:00 0 3 77384 61648 1.5 perl memory.p +l 10000 1000 $ perl memory.pl 100 100000 100, 100000 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 14105 pts/0 S+ 0:00 0 3 18164 2292 0.0 perl memory.p +l 100 100000 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 14105 pts/0 S+ 0:00 0 3 77552 61684 1.5 perl memory.p +l 100 100000 PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 14105 pts/0 R+ 0:00 0 3 18752 2888 0.0 perl memory.p +l 100 100000

If you check with strace you'll see that it uses brk in first case, but mmap/munmap in the second.