Hi, I'm having huge memleaks using threads; I came upon node_id=625622, which has no conclusion. Here's the thing: running the following code uses up more and more memory, but quite unpredictably depending on the contents of $sub1:
use threads; use strict; use warnings; for (;;) { my $thr = threads->new($sub1)->join; undef $thr; }
I'm monitoring rss usage on linux and perl 5.10 using:
i=0; while :; do A=$(ps -C threads3 -o ppid,pid,cmd,rss --no-headers); + [ -n "$A" ] && echo $i $A || i=0 ; sleep 1; i=$(($i+1)); done
Here's the output of this, with different values of $sub1:
# $sub1 = sub { my $a=[ 1 .. 10000] }; 1 19959 1137 /usr/bin/perl ./threads3 5276 2 19959 1137 /usr/bin/perl ./threads3 5140 3 19959 1137 /usr/bin/perl ./threads3 5700 [...] 642 19959 1137 /usr/bin/perl ./threads3 124244 643 19959 1137 /usr/bin/perl ./threads3 124736 644 19959 1137 /usr/bin/perl ./threads3 125380 # $sub1 = sub { my $a=[ 1 .. 100000] }; 1 19959 8329 /usr/bin/perl ./threads3 24176 2 19959 8329 /usr/bin/perl ./threads3 24176 3 19959 8329 /usr/bin/perl ./threads3 24176 [...] 642 19959 8329 /usr/bin/perl ./threads3 28368 643 19959 8329 /usr/bin/perl ./threads3 26036 644 19959 8329 /usr/bin/perl ./threads3 34460 # $sub1 = sub { }; 1 19959 13288 /usr/bin/perl ./threads3 3152 2 19959 13288 /usr/bin/perl ./threads3 3796 3 19959 13288 /usr/bin/perl ./threads3 4564 [...] 642 19959 13288 /usr/bin/perl ./threads3 400640 643 19959 13288 /usr/bin/perl ./threads3 401312 644 19959 13288 /usr/bin/perl ./threads3 401980
Any idea what's happening here? $sub1 = sub { my $a= 1 .. 100000 } seems to have minor memleak, while an empty sub does the most damage... I tried using 'forks' instead, which seems to solve the problem. PS: The code proposed by BrowserUk in 625622 also leaks

In reply to does threads (still) memleak? by faxm0dem

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.