Can someone explain how to clean up threads so that they do not leak memory? The code below is a thread test script. Why is $endmem so much more than $startmem? All the threads are cleaned up by then...
~confused...
use strict; use Win32::API; use threads; my $hProcess = getCurrentProcess(); my $startmem=getProcessMemoryInfo($hProcess); for(my $x=0;$x<25;$x++){ select(undef,undef,undef,.1); #my $ctime=time(); threads->new(\&threadTest); } print STDOUT "OUT OF THREAD LOOP\n"; my $midmem=getProcessMemoryInfo($hProcess); #Wait for a bit and then clean up print STDOUT "Waiting 5 seconds\n"; select(undef,undef,undef,5); print STDOUT "CLEANING UP THREADS\n"; foreach my $thr (threads->list) { # Don't join the main thread or ourselves if ($thr->tid && !threads::equal($thr, threads->self)) { $thr->join; } } print STDOUT "Waiting 5 seconds\n"; select(undef,undef,undef,5); print STDOUT "Checking Memory\n"; my $endmem=getProcessMemoryInfo($hProcess); print "-"x30,"\n"; print "hprocess: $hProcess\n"; print "StartMem: $startmem MB\n"; print "MidMem: $midmem MB\n"; print "EndMem: $endmem MB\n"; exit; sub threadTest{ my $wtime=int(rand(5)) || .5; select(undef,undef,undef,$wtime); my $tid=threads->tid; print STDOUT "Thread $tid waited $wtime seconds\n"; } ############### sub getCurrentProcess{ my $GetCurrentProcess = new Win32::API("Kernel32", "GetCurrentProc +ess", [], 'N') || return $^E; my $hProcess=$GetCurrentProcess->Call(); return $hProcess; } ############### sub getProcessMemoryInfo { #usage: $memusage=getProcessMemoryInfo($hprocess); or ($memusage,$ +peakmemusage,$vmsize)=getProcessMemoryInfo($hprocess); my $hProcess=shift || return; my $name=shift; my $pid=shift; # memory usage is bundled up in ProcessMemoryCounters structure # populated by GetProcessMemoryInfo() win32 call my $DWORD = 'B32'; # 32 bits my $SIZE_T = 'I'; # unsigned integer # build a buffer structure to populate my $pmem_struct = "$DWORD" x 2 . "$SIZE_T" x 8; my $pProcessMemoryCounters = pack($pmem_struct, 0, 0, 0, 0, 0, 0, +0, 0, 0, 0); # GetProcessMemoryInfo is in "psapi.dll" my $GetProcessMemoryInfo = new Win32::API('psapi','GetProcessMemor +yInfo', ['I', 'P', 'I'], 'I') || return $^E; my $DWORD_SIZE = 4; my $BufSize = 10 * $DWORD_SIZE; my $MemStruct = pack( "L10", ( $BufSize, split( "", 0 x 9 ) ) ); if( $GetProcessMemoryInfo->Call( $hProcess, $MemStruct, $BufSize ) + ){ my( @MemStats ) = unpack( "L10", $MemStruct ); my $memusage=int($MemStats[3]/1024); my $peak_memusage=int($MemStats[2]/1024); my $vmsize=int($MemStats[8]/1024); if(wantarray){return ($memusage,$peak_memusage,$vmsize);} return $memusage; } return; }

-------------------------------
by me
http://www.basgetti.com
http://www.kidlins.com


In reply to Cleaning up threads so they do not leak memory. (Win32) by slloyd

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.