Home > Archive > PerlTk > October 2005 > Memory leak when replacing listbox contents
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 leak when replacing listbox contents
|
|
| Stephen 2005-10-24, 3:57 am |
| Hello,
In one of my perl/tk programs I refresh the contents of a listbox with
new data by deleting the current values and inserting new ones. A few
bytes of memory seem to be leaked by the Listbox->insert() function
that are not recovered by the Listbox->delete() function. Given time,
a program refreshing a listbox in this way can consume a lot of memory.
I have observed the problem under both Linux and Windows. Under Linux,
I am using perl 5.8.5 with perl-Tk version 804.27.
A short piece of example code follows at the end of this message.
Despite its simplicity, it will gradually consume more and more memory.
I would appreciate any advice that anyone could offer. Am I doing
something wrong, or is there a bug in the tk modules?
Kind regards,
Stephen
Example code:
#!/usr/local/bin/perl -w
use strict;
use Tk;
# Create main window containing a listbox
my $main_window = new MainWindow;
my $listbox = $main_window->Listbox->pack;
while(1)
{
# Delete all entries in listbox
$listbox->delete(0, "end");
# Insert an entry into listbox (memory leak here!)
$listbox->insert("end", "test");
# Refresh window
$main_window->update;
}
# EOF
| |
| zentara 2005-10-24, 7:56 am |
| On 24 Oct 2005 00:32:28 -0700, "Stephen" <stephenpage@hotmail.com>
wrote:
>Hello,
>
>In one of my perl/tk programs I refresh the contents of a listbox with
>new data by deleting the current values and inserting new ones. A few
>bytes of memory seem to be leaked by the Listbox->insert() function
>that are not recovered by the Listbox->delete() function. Given time,
>a program refreshing a listbox in this way can consume a lot of memory.
>
>I have observed the problem under both Linux and Windows. Under Linux,
>I am using perl 5.8.5 with perl-Tk version 804.27.
>
>A short piece of example code follows at the end of this message.
>Despite its simplicity, it will gradually consume more and more memory.
>
>I would appreciate any advice that anyone could offer. Am I doing
>something wrong, or is there a bug in the tk modules?
>
>Kind regards,
>
>Stephen
>
It's a pretty small leak in the code you shown. In the following,
I get a 32k increase every 6000 cycles.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
# Create main window containing a listbox
my $main_window = new MainWindow;
my $listbox = $main_window->Listbox->pack;
my $count = 0;
while(1){
$count++;
# Delete all entries in listbox
$listbox->delete(0, "end");
# Insert an entry into listbox (memory leak here!)
$listbox->insert("end", "test");
# Refresh window
$main_window->update;
my $index = $listbox->index('end');
print "$index->$count\n";
}
# EOF
########################################
#######
The only solution I have found is to store your data in an array,
and use the listbox only to display the array.
#!/usr/bin/perl
use warnings;
use strict;
use Tk;
#no leaks, but watch out for @array being empty...crash
#make sure @array has at least a '';
my $mw = Tk::MainWindow->new(-title => 'Listbox Leak');
my @array = (1..1000);
my $lb = $mw->Listbox(
-listvariable=> \@array,
)->pack();
my $b = $mw->Button(-text => 'Leak Test',
-command => \&leak
)->pack();
MainLoop;
sub leak {
my $count = 0;
while(1){
$count++;
@array = map{"$_.$count"}(1..1000);
$lb->update;
}
}
__END__
--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html
| |
| Stephen 2005-10-24, 7:56 am |
| Hello,
Thank you for the suggestion. I will try switching to using an array
as a listvariable to see whether that fixes the problem in my case.
I don't think there was anything fundamentally wrong with the way I was
doing things though and this modification seems to sidestep a possible
bug rather than solving it (unless I misunderstood something).
Eventually the leak causes a considerable amount of memory to be
consumed in my case (though I am looping at ~1Hz in my program rather
than continuously as in the example).
Should a Listbox->delete not clear up after a Listbox->insert?
Thank you very much for your advice.
Stephen
| |
| x3v0-usenet@yahoo.com 2005-10-24, 6:57 pm |
| Tk::Listbox does indeed have a memory leak.
This has been reported as CPAN bug #12466 :
http://rt.cpan.org/NoAuth/Bug.html?id=12466
Hopefully we will get a new version of Tk soon that fixes this.
Ken
| |
| Stephen 2005-10-25, 6:59 pm |
| Thanks Zentara, using listvariables as you suggested stopped the leak
in my program.
Cheers,
Stephen
| |
| Stephen 2005-10-25, 6:59 pm |
| Hello Ken,
Thank you for the information.
Cheers,
Stephen
|
|
|
|
|