Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Do threads do global destruction?

by rjt (Deacon)
on Nov 09, 2012 at 22:30 UTC ( #1003212=note: print w/replies, xml ) Need Help??

in reply to Do threads do global destruction?

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));

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003212]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2017-11-20 12:34 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (286 votes). Check out past polls.