in reply to Re^2: threads causing memory leak
in thread threads causing memory leak
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:
The join is utterly redundant.
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?
|
|---|