Unfortunately, the current (as of 5.8) state of the ithreads implementation in perl still has some bugs, and the garbage collection of scalars shared implicitely through a shared array seems to be one of them. I don't know the ins and outs of it, nor if or when it is likely to be cured. From what I've read elsewhere, the bug seems to be a "perl thing", rather than relating to any particular platforms.
You can, at least in my very crude imperical tests, slow down the rate of growth by using Thread::Queue as the mechanism for distributing your data to your threads, but it doesn't fix it completely.
use 5.008; use strict; use threads qw(yield); use threads::shared (); use Thread::Queue; use Time::HiRes qw(sleep); $|++; my $Q = new Thread::Queue; $Q->enqueue( 1 .. 1000 ); my $sub1 = sub { my $a; while (1) { $Q->dequeue; yield; } }; my $t1 = new threads $sub1; my $t2 = new threads $sub1; my $t3 = new threads $sub1; $t1->detach; $t2->detach; $t3->detach; while(1) { # readFile(); # ... $Q->enqueue(1..1000) unless $Q->pending; print $Q->pending; yield; }
The other possibility, and it's not a good one, is to not detach your threads, but rather to occasionally join them and spawn a new one to replace it. The leaked memory seems to be released back to the OS once the thread is joined. I can't offer you a good designed for this as I haven't yet worked one out for myself. This is a very unsatisfactory situation, and I wish it were not so.
I also wish that I had the ability to assist in fixing the problem but even if I understood enough to construct a patch, I see no way for me to participate in the process.
In reply to Re: Does Perl have garbage collection mechanism and how it performs?
by BrowserUk
in thread Does Perl have garbage collection mechanism and how it performs?
by RobCheung
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |