Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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


Comment on Re: Do threads do global destruction?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003212]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (21)
As of 2015-07-01 15:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (5 votes), past polls