FrankyGT has asked for the wisdom of the Perl Monks concerning the following question:

What's wrong with perl threads?

If I run this code :

use threads; sub my_sub() { return 1; } while (1) { my $thr=threads->create(\&my_sub); my @res=$thr->join(); }

Within 5 minutes my heap has grown to over 50MB and keeps growing...

Replies are listed 'Best First'.
Re: perl threads & memory
by dave_the_m (Monsignor) on Oct 23, 2014 at 12:29 UTC
    This leak appears to have been fixed in perl-5.14.0. 5.10.1 is over 5 years old, so you may wish to upgrade to something newer.

    Dave.

Re: perl threads & memory
by choroba (Cardinal) on Oct 23, 2014 at 11:44 UTC
    Hi FrankyGT, welcome to the Monastery!

    What OS and Perl version do you run? How do you measure the heap? Your code is running on my machine (Win7, cygwin, Perl 5.14.4) for 6 minutes now and I see no problem.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: perl threads & memory
by BrowserUk (Patriarch) on Oct 23, 2014 at 12:27 UTC

    On my system, your code uses a constant, unchanging 3.9MB.

    So the problem is not with "perl threads"; but with your system.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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.
Re: perl threads & memory
by Loops (Curate) on Oct 23, 2014 at 11:47 UTC

    The Perl Threads documentation says:

    Memory consumption
    
    On most systems, frequent and continual creation and destruction of threads can lead to ever-increasing growth in the memory footprint of the Perl interpreter. While it is simple to just launch threads and then ->join() or ->detach() them, for long-lived applications, it is better to maintain a pool of threads, and to reuse them for the work needed, using queues to notify threads of pending work. The CPAN distribution of this module contains a simple example (examples/pool_reuse.pl) illustrating the creation, use and monitoring of a pool of reusable threads.
    
Re: perl threads & memory
by FrankyGT (Initiate) on Oct 23, 2014 at 11:47 UTC
    Some more info : This is on perl 5.10.1 on solaris 10 (csw perl) Tried the same on linux perl 5.10, and I don't see it there. The VM size starts with 200MB, and stays that way.