You might also like to try this. On my system it settles down to a steady number of threads within 5 or 10 seconds and thereafter creates and destroys threads at the rate of about 7/second with the memory usage fluctuating a few Kb either side of 30MB.

I left this running whilst I ate (be warned, it thrashes the cpu). The thread cycles had reached over 12000 by the time I returned and the memory usage was still locked at ~30MB. And that was with 5.8.6. If there is a leak there, it's an extremly slow one.

If your processor is faster or slower than mine, you might need to tweak the sleep parameter within the thread to get it to achieve equilibrium quickly.

#! perl -slw use strict; use threads; use Thread::Queue; use threads::shared; use Time::HiRes qw[ time ]; $|++; our $N ||= 1000; our $M ||= 10; our $aClonedGlobal = 12345; our $aSharedGlobal :shared = 12345; my $aClonedLexical = 12345; my $aSharedLexical :shared = 12345; my $count :shared = 0; my $running :shared = 0; my $Q = new Thread::Queue; sub thread { { lock $running; ++$running } my $tid = threads->self->tid; my( $some, $thread, $local, $vars ) = (12345) x 4; require Carp; require IO::Socket; require LWP::UserAgent; $Q->enqueue( threads->tid ); sleep $M/10; { lock $running; --$running }; return 1; } threads->create( \&thread ) for 1 .. $M; my $start = time; while( 1 ) { threads->object( $Q->dequeue )->join; threads->create( \&thread ); printf "\r%d\t (%7.3f/sec)\t", ++$count, $count / ( time() - $star +t ); sleep 0; }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re^8: Threads question by BrowserUk
in thread Threads question by xiaoyafeng

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.