Home > Archive > PerlTk > January 2005 > memory leaks with Tk::Canvas
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
memory leaks with Tk::Canvas
|
|
| Christophe Mertz 2005-01-28, 3:59 pm |
|
--=-yjERO7GJL6rdueGfl72q
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
[sorry, for the re-send... with the attached script this time]
Hello,
we have some memory leaks in one of our (relatively big) interactive
application based on perl/Tk (and zinc!). After some inquiry we finally
detects some leaks in Tk::Canvas
In the attached simple script I reproduce apparently two different
leaks:
1) when clicking repeatedly on the blue button (ie without waiting the
end of the "animation"), it seems there is a leak or 132kB (linux ptk
804.027) or 4kb (win activestate 804.027)
2) when clicking even once on the red button, there seems to be strong
leak of approx 132kB on linux 804.027 but none (or few?) on windows
(activestate 804.027)
This bug appears when we replace an array in a coords method by the
corresponding "tk" string.
Does someone identify some similar leaks on ptk 804 ?
Does someone have a clue?
Cheers,
-- Christophe Mertz
--=-yjERO7GJL6rdueGfl72q
Content-Disposition: attachment; filename=canvas.pl
Content-Type: text/x-perl; name=canvas.pl; charset=us-ascii
Content-Transfer-Encoding: quoted-printable
#!/usr/bin/perl -w
use Tk;
use Tk::Canvas;
my $mw =3D MainWindow->new();
$mw->geometry('1024x768');
my $canvas =3D $mw->Canvas(-width =3D> 1024, -height =3D> 768) -> pack();
my $rect =3D $canvas->create('rectangle', [0, 0, 0, 0]);
my $bt1 =3D $canvas->create('rectangle', [20, 20, 50, 50],
-fill =3D> 'blue');
my $bt2 =3D $canvas->create('rectangle', [20, 100, 50, 130],
-fill =3D> 'red');
$canvas->bind($bt1, '<1>',sub{anim1();});
$canvas->bind($bt2, '<1>',sub{anim2();});
my $count =3D0;
MainLoop;
my $cfor =3D 0;
## bug1
## click repeteadly on blue rectangle so that more
## than one such callback is runned at the same time
sub anim1 {
my $c =3D ++$cfor;
print "begin anim1: $c\n";
for (my $i =3D 0; $i < 800; $i++){
$canvas->coords($rect,[0, 0, $i, $i]);
$canvas->update();
}
print "end anim1: $c\n";
}
# bug #2
# this code leaks approx 264 kB every time it is called
sub anim2 {
my $c =3D ++$cfor;
print "begin anim2: $c\n";
for (my $i =3D 0; $i < 800; $i++){
# using a string rather than the usual perl array
$canvas->coords($rect,"0 0 $i $i");
$canvas->update();
}
print "end anim2: $c\n";
}
## the following code does not leak if activated only once
## (ie no successive click on the green rectangle)
sub anim3 {
my $c =3D ++$cfor;
print "begin anim3: $c\n";
for (my $j =3D 0; $j < 100; $j++) {
for (my $i =3D 0; $i < 800; $i++){
$canvas->coords($rect,[0, 0, $i, $i]);
$canvas->update();
}
}
print "end anim3: $c\n";
}
--=-yjERO7GJL6rdueGfl72q--
-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server. If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to majordomo@lists.stanford.edu
| |
| zentara 2005-01-29, 3:57 pm |
| On Fri, 28 Jan 2005 18:08:09 +0000 (UTC), Christophe Mertz
<mertz@intuilab.com> wrote:
>
>--=-yjERO7GJL6rdueGfl72q
>Content-Type: text/plain
>Content-Transfer-Encoding: 7bit
>
>[sorry, for the re-send... with the attached script this time]
>
>Hello,
>
>we have some memory leaks in one of our (relatively big) interactive
>application based on perl/Tk (and zinc!). After some inquiry we finally
>detects some leaks in Tk::Canvas
>
>In the attached simple script I reproduce apparently two different
>leaks:
>
>1) when clicking repeatedly on the blue button (ie without waiting the
>end of the "animation"), it seems there is a leak or 132kB (linux ptk
>804.027) or 4kb (win activestate 804.027)
On my 804.027 linux, it starts out at 8452kb.
If I slowly repeadedly click on the blue rect, it increases in mem to
8592kb, but levels out there.
If I quickly repeatedly click the blue rect, it increases on each click,
but runs without increasing after the clicking stops, and only seems to
go above the 8592kb value due to "fast clicking".
So intuitively, it looks to me, that the blue rectangle levels out, so
to me it's not leaking. It's just Tk allocating space, and reusing it
eventually. The fact that it keeps gaining with fast clicking, seems
like Tk is "adding events to be processed", which will take some space?
The red rect acts about the same way. It does increase slightly for a
few clicks, then levels out. It seems the "fast mouse clicking" is the
reason for the gain, but if Tk has to add all those "fast clicks" to
it's event's list, there is bound to be an increase.
So to experiment around, (just for the sake of brainstorming), I took
the mouse click out and put it on a timer. It seems that if you let the
callback complete, each cycle, there is no memory increase. So start
out with the timer at
3000... no mem gain
2000.. no mem gain
1000.. slight mem gain and clipping of animation
100 ... huge mem gain and clipping of animation
So it seems that the callback needs to be able to finish, or there
is something left over in memory. Probably the same holds true
for mouse click events. The MeM module is something I use, similar
to your memory monitor.
########################################
###########
#!/usr/bin/perl -w
use Tk;
use Tk::Canvas;
use MeM;
my $mw = MainWindow->new();
$mw->geometry('1024x768');
my $canvas = $mw->Canvas( -width => 1024, -height => 768 )->pack();
my $rect = $canvas->create( 'rectangle', [ 0, 0, 0, 0 ] );
my $bt1 = $canvas->create( 'rectangle', [ 20, 20, 50, 50 ], -fill =>
'blue' );
my $timer = $canvas->repeat(3000,sub{
anim1();
});
my $count = 0;
MainLoop;
sub anim1 {
for ( my $i = 0 ; $i < 800 ; $i++ ) {
$canvas->coords( $rect, [ 0, 0, $i, $i ] );
$canvas->update();
}
}
__END__
After playing around, I found that in order for the memory
to remain stable, the callback code has to be allowed to repeat.
There are probably numerous schemes to accomplish that,
but I had trouble, cancelling the for loop effectively.
This code, puts out a rectangle repeatedly ( like a radar scan ),
and dosn't leak, because I make sure the callback completes.
#!/usr/bin/perl -w
use strict;
use Tk;
use Tk::Canvas;
use MeM;
my $mw = MainWindow->new();
$mw->geometry('1024x768');
my $canvas = $mw->Canvas( -width => 1024, -height => 768 )->pack();
my $rect = $canvas->create( 'rectangle', [ 0, 0, 0, 0 ] );
my $bt1 = $canvas->create( 'rectangle', [ 20, 20, 50, 50 ], -fill =>
'blue' );
$canvas->bind($bt1, '<1>',sub{
while(1){
anim1();
}
});
MainLoop;
sub anim1 {
for ( my $i = 0 ; $i < 800 ; $i++ ) {
$canvas->coords( $rect, [ 0, 0, $i, $i ] );
$canvas->update();
};
}
__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| Christophe Mertz 2005-01-31, 3:59 pm |
| Hello,
I clearly missed many mails on the mailing list...
As I was interested in the repliys to my question, I found the answer of
znetara on google:
http://groups.google.fr/groups?hl=f...mp.lang.perl.tk
This is my reaction.
Sat 29/01/2005, Zentara wrote :
> Friday 28/01/2005, Christophe Mertz wrote:
>
[color=darkred]
> On my 804.027 linux, it starts out at 8452kb.
> If I slowly repeadedly click on the blue rect, it increases in mem to
> 8592kb, but levels out there.
I can easily go beyond 500kB memory leak (for example: 80 repeated
clicks, than waiting until all staled animations are finished, then
clicking apparently more than 80 times... etc...)
However, it seems you are right: the allocated memory seems to be
re-used
>
> If I quickly repeatedly click the blue rect, it increases on each click,
> but runs without increasing after the clicking stops, and only seems to
> go above the 8592kb value due to "fast clicking".
I just test it again and my results (linux ptk 804.027) seems differents
than yours, Zentara:
- when I click on the blue rectangle after the end of each animation, I
have NO memory consomption (tested for 50 clicks!)
- when I click about twelve to twenty times in a short time (about
1-2s), I have this 128kB (or 132kB) memory consomption
A also discovered than if clicked about a hundred times quickly on the
blue rectangle, I got the following message:
Deep recursion on anonymous subroutine at canvas.pl line 37.
Deep recursion on subroutine "main::anim1" at canvas.pl line 20.
It seems that perl/tk allows only 100 "recursive" callbacks, which is
not really a pb in normal use, I guess.
Any ideas?
-- Christophe Mertz
-++**==--++**==--++**==--++**==--++**==--++**==--++**==
This message was posted through the Stanford campus mailing list
server. If you wish to unsubscribe from this mailing list, send the
message body of "unsubscribe ptk" to majordomo@lists.stanford.edu
|
|
|
|
|