Memory leak when replacing listbox contents - Perl
This is a discussion on Memory leak when replacing listbox contents - Perl ; 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 ...
-
Memory leak when replacing listbox contents
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
-
Re: Memory leak when replacing listbox contents
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
-
Re: Memory leak when replacing listbox contents
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
-
Re: Memory leak when replacing listbox contents
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
-
Re: Memory leak when replacing listbox contents
Thanks Zentara, using listvariables as you suggested stopped the leak
in my program.
Cheers,
Stephen
-
Re: Memory leak when replacing listbox contents
Hello Ken,
Thank you for the information.
Cheers,
Stephen
Similar Threads
-
By Application Development in forum ADO DAO RDO RDS
Replies: 3
Last Post: 09-11-2007, 08:52 AM
-
By Application Development in forum Javascript
Replies: 3
Last Post: 05-03-2007, 10:42 AM
-
By Application Development in forum Adobe Indesign
Replies: 9
Last Post: 02-05-2007, 01:52 PM
-
By Application Development in forum DOTNET
Replies: 0
Last Post: 03-23-2006, 05:13 PM
-
By Application Development in forum basic.visual
Replies: 1
Last Post: 02-17-2005, 03:48 PM