in reply to Re^2: memory leaks with threads
in thread memory leaks with threads
The thing is, I'm not at all convinced that your posted code is evidence of a memory leak.
On the surface, it looks like it is because it looks like you are recording the maximum number of concurrent threads, but the way you are doing it is flawed.
If your main thread gets a timeslice between this line:
$count = $threadscount;
and this line:
if ( $count>$maxthreads ){
Then it could easily create 300 more threads, that would increment $threadscount, but they would not be reflected in $count, so you would not be recording the true maximum concurrent threads.
Basically, there is nothing in your code to prevent you from creating more than 300 threads, so the fact that the maximum thread count never gets higher than 300 is highly suspicious.
In essence, I don't believe you are demonstrating a memory leak. I think the amount of memory used is simply a multiplication factor resulting from the fact that there is no constraint on the concurrent number of threads, and at various times, this number is rising well above the 300 number that your code is reporting.
Would you please run the following code, which explicitly constrains the maximum number of threads, whilst creating and destroying threads at a high rate and monitor the memory usage using top and post your findings?
use threads; use threads::shared; our $S ||= 1; my $t:shared = 0; $|++; while( 1 ){ printf"\r$m\t%d\t", do{ lock $t; $t }; if( do{ lock $t; $t } < 10 ) { $m++; threads->create( sub{ { lock $t; $t++ } select undef, undef, undef, $S; { lock $t; $t-- } } )->detach; } else{ select undef, undef, undef, 0.01; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: memory leaks with threads
by misc (Friar) on Jul 09, 2007 at 17:39 UTC | |
by BrowserUk (Patriarch) on Jul 09, 2007 at 18:24 UTC | |
by misc (Friar) on Jul 09, 2007 at 18:49 UTC | |
by BrowserUk (Patriarch) on Jul 09, 2007 at 19:25 UTC | |
by misc (Friar) on Jul 09, 2007 at 20:07 UTC | |
| |
by tye (Sage) on Jul 09, 2007 at 21:09 UTC | |
| |
|
Re^4: memory leaks with threads
by royer_k (Initiate) on Sep 24, 2007 at 10:55 UTC | |
by BrowserUk (Patriarch) on Sep 24, 2007 at 12:42 UTC |