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

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

What is required to free up memory used by Tk::TableMatrix when data in table is deleted (or get it to reuse the memory it has already consumed) instead of just grabbing more and more memory?

In test script below we fill up table with data. When table is emptied memory usage stays the same (about 138_000 KB) and when we fill up table again with new data, memory just increases more and more.. Hopefully I just missed the boat somewhere.

MSWin32|perl5.8.7|Tk804.027

Thanks for advice.

use warnings; use strict; use Tk; use Tk::TableMatrix::Spreadsheet; $| = 1; my $mw = tkinit; my $NumCols = 50; our $tmVar = {}; our $TM_Table = $mw->Scrolled('Spreadsheet', -variable => $tmVar, -sparsearray=>1, -titlerows => 1, -titlecols => 1, -scrollbars => 'se', -selectmode => 'extended', -rows=>1, -cols=> $NumCols, -resizeborders => 'both', -wrap=>1, -bd=>1, -cache=>0, -selecttitle=>1, -colstretchmode => 'none', -rowstretchmode => 'none', -rowheight => 1, -drawmode=>'fast')->pack(-expand=>1,-fill=>'both'); $mw->Button(-text=> 'Fill Tablematrix', -command => \&FillTM)->pac +k; $mw->Button(-text=> 'Clear Tablematrix', -command => \&ClearTM)->p +ack; #Top row $TM_Table->set("0,$_", $_) for (0..$NumCols-1); MainLoop; sub FillTM { my $Rows = 5000; $TM_Table->insertRows("0.0", $Rows); for my $Row (1..5000) { $mw->update if (not $Row % 500) xor $TM_Table->see("$Row,0"); for (0..$NumCols-1){ $TM_Table->set("$Row,$_",qw(111 222)[int rand 2] x 50 ); } } } sub ClearTM { #$TM_Table->deleteRows('-keeptitles', '0.0', $TM_Table->cget(-rows +)); $TM_Table->deleteRows( '0.0', $TM_Table->cget(-rows)); $TM_Table->clearAll('0,0','end'); undef $tmVar; }

Replies are listed 'Best First'.
Re: Tk::TableMatrix Memory Usage
by zentara (Archbishop) on Nov 09, 2005 at 13:32 UTC
    I havn't played with TableMatrix except in examples, but the following snippet seems to reuse memory ok. Of course, I'm not changing the table size, just refilling cells. I think that is your problem, you are deleting and creating rows instead of reusing them. I'm not sure if there is a way to dynamically resize a TableMatrix. Usually in these big Tk lists, the trick to preventing memory gain, is to use a separate array for the actual data (I use $arrayVar), and just let the TableMatrix display it. When you try to use TableMatrix rows and cells as "disposible data containers", it will gain memory, as TableMatrix tries to keep track of the old entries.

    But I'm just giving my best "guess", someone else may have a better example.

    There is 1 bug which baffles me, after I do a "fill" or "clear", I only get the matrix to update after I jiggle the right scrollbar. Anyone else see this? I'm using Perl587 and Tk804.027 on linux.

    #!/usr/bin/perl use strict; use warnings; use Tk; use Tk::TableMatrix; use Tk::TableMatrix::Spreadsheet; my $top = MainWindow->new; my $arrayVar = {}; print "Filling Array...\n"; my ($rows,$cols) = (40000, 10); foreach my $row (0..($rows-1)){ $arrayVar->{"$row,0"} = "$row"; } foreach my $col (0..($cols-1)){ $arrayVar->{"0,$col"} = "$col"; } print "Creating Table...\n"; sub colSub{ my $col = shift; return "OddCol" if( $col > 0 && $col%2) ; } my $label = $top->Label(-text => "TableMatrix v2 Example") ->pack( -expand => 1, -fill => 'both'); my $t = $top->Scrolled('Spreadsheet', -rows => $rows, -cols => $cols, -width => 6, -height => 12, -titlerows => 1, -titlecols => 1, -variable => $arrayVar, -coltagcommand => \&colSub, -colstretchmode => 'last', -flashmode => 1, -flashtime => 2, -wrap=>1, -rowstretchmode => 'last', -selectmode => 'extended', -selecttype=>'cell', -selecttitles => 0, -drawmode => 'slow', -scrollbars=>'se', -sparsearray=>0 )->pack(-expand => 1, -fill => 'both'); my $realmatrix = $t->Subwidget('scrolled'); $top->Button( -text => "Clear", -command => sub{&clear})->pack(-expand => 1, -fill => 'x'); $top->Button( -text => "Fill", -command => sub{&fill})->pack(-expand => 1, -fill => 'x'); $top->Button( -text => "Exit", -command => sub{$top->destroy})->pack(-expand => 1, -fill => 'x' +); $t->colWidth( -2 => 8, -1 => 9, 0=> 12, 4=> 14); $arrayVar->{"1,1"} = 42; Tk::MainLoop; ###################################################################### +#### sub clear{ foreach my $row(1..$rows){ foreach my $col(1..$cols){ $arrayVar->{"$row,$col"} = 0; } } $realmatrix->update; $t->update; $top->update; } ###################################################################### +#### sub fill{ foreach my $row(1..$rows){ foreach my $col(1..$cols){ $arrayVar->{"$row,$col"} = 1000; } } $realmatrix->update; $t->update; $top->update; }

    I'm not really a human, but I play one on earth. flash japh
      Looks like set (and deleteRows?) is the method causing leaks in this module. Guess we just have to work directly with the hash tied to the tablematrix widget instead.

      Luckily both clearTags and windowDelete methods seem to behave properly regarding memory usage.

      zentara, you can use this trick to force update of TableMatrix widget:

      sub TMRefresh { #Required input TableMatrix object. return if (!$_[0]); $_[0]->configure(-padx =>($_[0]->cget(-padx))); }
        Thanks for the tips. I also found you can force an update with
        $t->see("end"); $t->see("1,1");

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