On 1 of the newsgroups, someone asked about the pros/cons differences between using forks or threads. Well the newsgroup is c oriented, and I thought I would mention the way that interpreted languages (like Perl) will hold onto memory released by threads.
Specifically I mentioned that if a high memory usage thread is run first, then a series of low memory usage threads, the memory of the process will stay at the high memory usage. I used the following script to demonstrate. (I have a 1 gig ram on a linux 2.6.22 kernel). The results were unpredictable, but generally followed the rule that heavy mem use is freed, medium use is half freed, and light use ( <5%) is not freed( but may be on occaision).
#!/usr/bin/perl
use warnings;
use strict;
use threads;
my $count = 0;
# load a mem intensive thread first, then watch how mem drops
# notice the difference in memory reclamation( top) after
# the following thread is run with heavy,medium, light mem
# on my machine
# heavy mem use will skyrocket to 75%, then drop to 2%
# medium will jump to 40% then drop to 20%
# light use will jump to 5% then stay there
# none use will stay at .3 %
#my $t = threads->new(\&do_my_thing, $count, 1000000); #light
#my $t = threads->new(\&do_my_thing, $count, 4000000); #medium
my $t = threads->new(\&do_my_thing, $count, 8000000); #heavy
#my $t = threads->new(\&do_my_thing, $count, 1); #none
$t->join;
select(undef,undef,undef,.1);
while (1) {
$count++;
print "thread $count started\n";
my $t = threads->new(\&do_my_thing, $count, 1 );
$t->join;
select(undef,undef,undef,.1);
}
<>; #wait for keypress to end, to check top
sub do_my_thing {
my ($val,$cdown) = @_;
my %hash;
for(0..$cdown){ $hash{$_} = 'a'; }
select(undef,undef,undef,.1);
print "thread $val ended\n";
}
So it seems that the kernel is deciding how much to free, depending on some
internal formula.
The question is: Can we trust top/ps to really report what
is going on. Since most threaded scripts may go 10% up occaisionally, if they
don't reach the kernel threshold for freeing, is that dead memory, or will the kernel
auto-free more (the diff between 5% and .3%) if it needs the memory for other apps.
This would be great news for threaded apps, but I worry about not being able to
trust top/ps, or the kernel doing things behind the scenes.
Anyways, this may be a mechanism to reclaim memory in Perl. I would appreciate any thoughts, comments on what is happening.