You already have some worthy replies to your question, but I thought I'd take a run at a more visual presentation. I create a bunch of threads that each create a large-ish array of random numbers and return after a short delay.
After about 30 minutes on my 64-bit linux system it was clear the memory was not increasing; it hit its maximum within the first several seconds of the test while the threads were spooling up:
Memory leak test. Ctrl-C to stop.
20 threads (17831 lifetime), 845.99 MiB RSS [================ ] / ^C
Code below. Certainly not my most elegant work. :-)
use warnings;
use strict;
use threads;
use threads::shared;
use Time::HiRes qw/sleep/;
my $DELAY = 2.5; # Delay for returning from threads
my $NUM = 20; # Number of active threads to spawn
my $total :shared = 0; # Total threads that we have created
my @whirly = qw(| / - \\);
printf "Memory leak test. Ctrl-C to stop.\n\n";
# Consume a lot of memory for a few seconds
sub highmem {
my @a = rand() x 1000000;
{ lock $total; $total++; }
sleep($DELAY);
}
# Return total perl memory usage in bytes, with a progress
# bar and maximum memory usage
{
my $max_len = 1; # Maximum bar length we've seen so far
my $max_mem = 1; # Maximum memory we've seen so far
sub print_mem {
my $rss = `ps -p $$ -o rss=`;
chomp($rss);
$rss /= 2**10; # MiB
my $bar = "=" x int($rss/50);
$max_len = length($bar) if (length($bar) > $max_len);
my $pad = " " x ($max_len - length($bar));
my $whirly = shift @whirly;
push @whirly, $whirly;
printf "\r%2d threads (%4d lifetime), "
."%7.2f MiB RSS [%s%s] %s ",
scalar threads->list(),
$total,
$rss,
$bar, $pad,
$whirly;
}
}
# Main loop; join() threads when we can, and create new
# threads once every time through the loop if we (still)
# have less than $NUM threads running. Print total Perl
# memory usage.
do {
$_->join for (threads->list(threads::joinable));
threads->create(\&highmem) if (threads->list() < $NUM);
print_mem();
} while(sleep(0.1));