As I suspected it (and Corion called it), you constantly start new threads and never join or detach them, then they just hang around consuming memory once they've completed.

Also, as the last line of the thread sub is $rss, that is returned from the thread, consuming more memory. The fact that you've shared it, as well as returning it means you've consumed even more.

And while I'm critiquing, there are some other anomalies in your code:

  1. "x" x (1024 * 1024 * 20) is a single string of 20MB 'x's.

    The join is utterly redundant.

  2. return unless $rss;

    Return (nothing) unless $rss has some value.

    Otherwise, just fall off the end of the subroutine and return ???

Try something like this (untested):

#! perl -slw use strict; use threads; use Win32::GUI qw(); use LWP::UserAgent; sub t { my $rss; my $ua = LWP::UserAgent->new; my $response = $ua->get( 'http://www.mektek.net/mekmatch/listServersRss.mkz' ); $rss = $response->decoded_content if $response->is_success; print "done1"; return $rss; } my $window = Win32::GUI::DialogBox->new( -title => "Test", -name => "Test", -onTimer => sub { return poll() + } ); my $use_up_some_memory = join( "", "x" x (1024 * 1024 * 20) ); $window->AddTimer( "T1", 5000 ); $window->Show; Win32::GUI::Dialog(); sub poll { print "polling ... "; my $thread = threads->create( \&t ); while ( $thread->is_running ) { Win32::Sleep 10; Win32::GUI::DoEvents; } print " done2"; return $thread->join; }

Presumably you want to do something with the data returned from poll?


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.
RIP an inspiration; A true Folk's Guy

In reply to Re^3: threads causing memory leak by BrowserUk
in thread threads causing memory leak by holli

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.