问题
I am having an issue with a large leak in Perl/Tk running on Unix. I am unable to post most of the code due to certain restrictions, but I have been able to identify what is causing the memory leak and create a small program that has the same symptoms. Basically I am running a GUI where a frame is destroyed and repopulated regularly. This seems to cause a large memory leak that keeps growing however. I thought the destroy command would get rid of all traces in memory, but it does not seem to be that way. I am not too familiar with the garbage collection in Perl either. This program will be running for days or weeks at a time and so a memory leak is not ideal. Does anyone have any ideas to stop the memory leak? I am restricted to using Perl, so unfortunately I cannot just easily port the code to Python or something. Thanks for help in advance.
#!opt/project/COTS/bin/perl
use strict;
use warnings;
use Tk;
$Tk::mw = MainWindow->new;
$Tk::mw->repeat(10,\$build);
my $status;
&build;
sub build{
$status->destroy() if ($status);
$status = $Tk::mw->Frame->pack(-side => 'top');
my $left_frame = $status->Frame(
-relief =>'sunken',
-borderwidth => 1
)->pack(-side => 'left', -anchor => 'nw', -padx => 4, -pady => 5);
my $right_frame = $status->Frame(
-relief =>'sunken',
-borderwidth => 1
)->pack(-side => 'left', -anchor => 'nw', -padx => 4, -pady => 5);
}
MainLoop;
回答1:
Yes, this is known. There are some leftovers in the Perl/Tk system when destroying a widget (actually it's not the fault of Perl, but of the Perl/Tk subsystem).
The best approach is to reuse widgets. Most of the time this is possible, as you can re-configure all options. If some subwidgets are about to vanish and reappear, then you can use packForget()
to temporarily remove them from display, and use pack()
again to make them visible.
来源:https://stackoverflow.com/questions/14408521/perl-tk-memory-leak-when-using-destroy-command