http://qs1969.pair.com?node_id=1082726

abonvici has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i have a great trouble with perl 5.16.3 activestate and tk 8.4 32. I need to update a tablegrid content every one second to display a changing matrix of data. I noticed that the call to refresh the view leaks memory, ie the memory uccupied by the script grows forever. Anyway I do not destroy or delete any row or widget and reuse always the same data container structure. The script is

use Tk; use Tk::TableMatrix; my $i=0; my $macchina_='38'; my $dbh; $mw = MainWindow->new; $mw->geometry("1000x600+0+0"); my $gridContainer={}; my ($rows,$cols) = (100, 16); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $gridContainer->{"$row,$col"} = " "; } } my $frameFermiDelTurno= $mw->Frame()->pack(-expand=>1,-fill=>'both' +); -orient=>"hori +zontal"); my $t = $frameFermiDelTurno->Scrolled('TableMatrix', -rows => $rows, -cols => $cols, -width =>500, -height => 14, -titlerows => 1, -titlecols => 1, -variable => $gridContainer, -selectmode => 'extended', -state => 'disabled', -bg => 'white', -font=> '{arial} 16 bold', -padx=>0, -ipadx=>0, -sparsearray=>0, -scrollbars=>'se' ); $t->tagConfigure('active',-bg => 'gray90', -relief => 'sunken' +); $t->tagConfigure('title', -bg => 'gray85', -fg => 'black', -re +lief => 'sunken'); $t->colWidth(2,20); $t->colWidth(3,20); # force scrollbar refresh: $t->pack( -expand=>1,-fill => 'x' , -side=>'left', -anchor=>'w +'); my $row=0; # $gridContainer->{"$row,0"} = "ID"; # $gridContainer->{"$row,1"} = "-"; # $gridContainer->{"$row,2"} = 'STATO'; # $gridContainer->{"$row,3"} = 'TURNO INIZIO'; # $gridContainer->{"$row,4"} = 'TURNO FINE'; # $gridContainer->{"$row,5"} = 'ORA INIZIO'; # $gridContainer->{"$row,6"} = 'ORA_FINE'; # $gridContainer->{"$row,7"} = 'ODL'; # $gridContainer->{"$row,8"} = 'LOTTO'; # $gridContainer->{"$row,9"} = 'NOTE'; # $gridContainer->{"$row,10"} = 'OPERATORE'; # $gridContainer->{"$row,11"} = 'CODICE PRODOTTO'; # $gridContainer->{"$row,12"} = 'CODICE_FERMO'; # $gridContainer->{"$row,13"} = 'DATA FINE'; # $gridContainer->{"$row,14"} = 'DATA INIZIO'; # $gridContainer->{"$row,15"} = 'DESCRIZIONE'; # $mw->repeat (1000,[ \&leak ]); MainLoop; sub leak{ TMRefresh(); } ###################################################################### sub TMRefresh{ ###################################################################### # this leaks $t->see("end"); # this leaks $->see("1,1"); $t->configure(-padx =>($t->cget(-padx))); #this leaks memory too my ($rows,$cols) = (100, 16); foreach my $row (0..($rows-1)){ foreach my $col (0..($cols-1)){ $gridContainer->{"$row,$col"} = int(rand(10)); } } $mw->update; }

Please , may you give me some advice if i am doing something wrong? I noticed that if I comment out the $t->see or $t->configure the table does not diplay anything other that column label but the memory allocated is costant. SO I think that the process of updating display has something that i don't understand ....Please help me. Thnak you a lot.

Replies are listed 'Best First'.
Re: Tk::tablematrix update leaks memory
by zentara (Archbishop) on Apr 18, 2014 at 09:04 UTC
    Hi, one of the design features of many Tk list type widgets, is that they tend to keep an internal counter of all the list changes. For instance say you add 100 entries, internally they would be labeled 0..99. Now if you delete 0..99, and then try to add another fresh 100, their internal counter would range from 100... 199, even though you think of them as 0..99 because of their appearance on the screen.

    So, just as with everything else in this world, it is best to learn to reuse things. An example is here: Tk::TableMatrix Memory Usage.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Thank you so much for your prompt answer. But seems to me that in my script the things are exactly as you say, ie i reuse the item of the container hash. I already read and tested your example script, and even if reusing the value of the hash ref that contains data the leaks happen anyway. I have tried my script with tcl::ptk instead of tk and after long time the memory seems stable with no leaks. To install tcl::ptk i need to pre-install activetcl and Class::ISA. Could be that tk 8.5 has a different behavior respect tk 8.4? I hope you can give me some ideas .Thank a lot .

        I hope you can give me some ideas

        I'm not an expert on TableMatrix, and if you want some advice, if you are experiencing trouble with it, I would dump the widget in favor of one that works for you.

        I would implement something on a Tk::Canvas or Tk::Zinc. The canvas handles thousands of items and changes very well. It's a bit more work, but you get to control every aspect of your widget. I wrote Tk-CanvasDirTree as an educational example to show how easy it is to make your own custm widget.

        The basic list and table widgets in Tk are simple widgets, and you can't expect too much from them.


        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku ................... flash japh