Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re^2: threads causing memory leak

by holli (Abbot)
on Sep 11, 2010 at 13:37 UTC ( [id://859753]=note: print w/replies, xml ) Need Help??


in reply to Re: threads causing memory leak
in thread threads causing memory leak

Here you are:
use strict; use threads; use warnings; use Win32::GUI qw(); use LWP::UserAgent; my $window = Win32::GUI::DialogBox->new( -title => "Test", -name => "T +est", -onTimer => sub { return poll() } ); my $use_up_some_memory = join( "", "x" x (1024 * 1024 * 20) ); $window->AddTimer( "T1", 20000 ); $window->Show; Win32::GUI::Dialog(); sub poll { my $rss : shared; print "polling\n"; my $thread = threads->create( sub { my $ua = LWP::UserAgent->new; my $response = $ua->get( 'http://www.mektek.net/mekmatch/listS +erversRss.mkz' ); $rss = $response->decoded_content if $response->is_success; } ); while ( $thread->is_running ) { sleep(1); Win32::GUI::DoEvents; } return unless $rss; }


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^3: threads causing memory leak
by Corion (Patriarch) on Sep 11, 2010 at 13:40 UTC

    I think you really need to do a $thread->join at the end of your subroutine.

    But instead of polling, I would have the thread send a (Windows) message to the main thread to tell the main thread/window that it can now fetch the data and update things.

      That's an interesting idea. I will consider it.


      holli

      You can lead your users to water, but alas, you cannot drown them.
Re^3: threads causing memory leak
by BrowserUk (Patriarch) on Sep 11, 2010 at 14:44 UTC

    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://859753]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-18 01:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found