in reply to does threads (still) memleak?

UPDATE Also just tested this on the latest Ubuntu8.10, with Perl5.10 and latest threads.....same memory gain.

I think you should look at OS memory reclamation with threads on linux. Depending on your kernel, OS, computer speed, available free memory, etc, memory reclamation will change. I would guess that with so much demand from the script, and with alot of free memory, the kernel may be playing it safe and not freeing all the time. You might try running it with a sleep 1 in your loop, and give the kernel time to free things up. I'm trying it now, bit it may take a while :-)

I have perl 5.8.8, and the latest threads from cpan, running on linux. When the script below starts, its at 15M, and at count 10,000, it is up to 20M. On the way up it seems to oscillate, going up a bit, going down a bit, but generally on an upward trend.

#!/usr/bin/perl use threads; use strict; use warnings; my $count=0; for (;;) { print $count++.' '; my $thr = threads->new( sub { my $a = [ 1 .. 10000] } )->join; undef $thr; }

I'm not really a human, but I play one on earth Remember How Lucky You Are

Replies are listed 'Best First'.
Re^2: does threads (still) memleak?
by faxm0dem (Novice) on Nov 21, 2008 at 12:48 UTC
    I tried using a similar OS (linux 2.6.9 x86_64) but perl 5.8.5, and the code seems to run OK.
      Well, it seems you found another reason to be suspicious of threads, especially if you plan to use the script on multiple unknown platforms. On the other hand, it is a bit unusal to spawn 10,000 threads, and a 5 Meg/10000 threads isn't outrageous.

      Many nodes have been posted concerning mem gains with threads, usually when complex objects (modules) are used in the threads, and the ref count dosn't clean up correctly. The solution there, is to reuse threads and objects, instead of spawning new ones. So....it seems the lesson is to reuse and recycle if you want to be safe with threads.


      I'm not really a human, but I play one on earth Remember How Lucky You Are
        Well, it seems you found another reason to be suspicious of threads, especially if you plan to use the script on multiple unknown platforms.

        Hopefully faxm0dem has reported the bug to p5p, and as the testcase is so simple, it may get included in the testsuite. It so, it should prevent changes that cause memory leaks from getting through into new releases. Which if the problem has been fixed in 5.8.9 and the 5.10 branches, could make for a more reliable future for iThreads.

        And maybe that is good reason to drop some of the old superstitions.


        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.
        That seems a very reasonable suggestion. How would you suggest to recycle threads in practice?