Memory leak test. Ctrl-C to stop. 20 threads (17831 lifetime), 845.99 MiB RSS [================ ] / ^C #### 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));