in reply to Cleaning up threads so they do not leak memory. (Win32)

I played a bit with your code, and I just want to register my confusion on a couple of matters. First, on this:
foreach my $thr (threads->list) { # Don't join the main thread or ourselves if ($thr->tid && !threads::equal($thr, threads->self)) { $thr->join; } }

The main thread has an id of "0", and threads->list doesn't seem to return thread 0, so I wonder if that's a bug in the list() method, or if the conditional before the join is unnecessary.

Second, on this:

my $GetProcessMemoryInfo = Win32::API->new('psapi','GetProcessMemo +ryInfo', ['I', 'P', 'I'], 'I') or return $^E;
I thought it was a waste linking to that function on every call to it, so I tried to move it (and the other Win32::API function definition), to near the top of the program, but it ended up crashing perl. If I defined it near the top, and still defined it again on every call, it worked a couple of times, but then returned "The specified module could not be found" trying to link to it again after joining the threads.

Replies are listed 'Best First'.
Re^2: Cleaning up threads so they do not leak memory. (Win32)
by BrowserUk (Patriarch) on Feb 24, 2006 at 10:10 UTC

    You might find this version of the OP code useful--or not:)

    It uses tasklist.exe to get the memory stat rather than Win32::API as I have had great problems with using that module with threads in the past and despite several attempts to work out why, my XS foo isn't strong enough.

    #! perl -slw use strict; use threads; sub mem { my( $usage ) = `tasklist /NH /FI \"pid eq $$\" ` =~ m[ (\S+) \s+ +K \s* $ ]x; return $usage; } sub threadTest{ sleep 1 } my $startmem = mem; print "StartMem: $startmem KB"; for my $pass ( 1 .. 50 ) { my @threads = map{ Win32::Sleep 100; threads->new(\&threadTest); } 1 .. 25; my $midmem = mem; $_->join for @threads; undef @threads; my $endmem = mem; print "Pass $pass\tMidMem: $midmem KB\tEndMem: $endmem KB"; }

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.