Andrey wrote:
> Memory leak cannot be normal for any program. So, it is either Perl bug or 
> Win32::GUI bug

I don't know exactly where (or what) the bug is, but I found a workaround.

problem is, the Win32 heap is not freed (or garbage collected, or 
whatever) while the window is active. if you minimize the window, you 
will see that the memory load goes down to less than 1MB. this is a well 
known "issue" Win32::GUI has, that can be observed also in several other 
Windows programs.

now the good news: I've found a microsoft page 
(http://support.microsoft.com/kb/293215) that explains how to force the 
freeing of RAM.

so this is the "non-leaking" code, using Win32::API:

===================
use strict;
use Win32::GUI();

use Win32::API;
Win32::API->Import('kernel32',
        'HANDLE GetCurrentProcess()'
);
Win32::API->Import('kernel32',
        'BOOL SetProcessWorkingSetSize(HANDLE hProcess, SIZE_T 
dwMinimumWorkingSetSize, SIZE_T dwMaximumWorkingSetSize)'
);

my $main = Win32::GUI::Window->new(-name => 'Main', -text => 'Perl', 
-width => 200, -height => 200);

my $label = $main->AddLabel(-name => "Label", -text => "Hello, world", 
-left => 20, -top => 20, -notify => 1);

sub Label_Click {
        for (1..10000) {
                $label = $main->AddLabel(-name => "Label", -text => rand(), 
-left => 
20, -top => 20, -notify => 1);          
        }
         # trim RAM usage
        SetProcessWorkingSetSize(GetCurrentProcess(), -1, -1);
}

$main->Show(); Win32::GUI::Dialog();

sub Main_Terminate { -1; }

===================

you will notice that the RAM goes up to several MB then, when the loop 
ends, it goes down to ~1MB.

I guess Windows does this to speed things up, and the KB page seems to 
suggest that trimming RAM is probably making your program run slower. so 
it's your choice, basically, to favour speed over RAM allocation or vice 
versa.

at least, that's my understanding of it :-)

for normal GUI applications, speed should not be so crucial (unless 
you're developing a videogame :-), so it's probably safe to call 
SetProcessWorkingSetSize() once in a while in your program if you're 
afraid of sucking up too much RAM. maybe the very same call could be 
added directly somewhere in the Win32::GUI code...

happy 2010 to all!

cheers,
Aldo

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
_______________________________________________
Perl-Win32-GUI-Users mailing list
Perl-Win32-GUI-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perl-win32-gui-users
http://perl-win32-gui.sourceforge.net/

Reply via email to