Discipulus has asked for the wisdom of the Perl Monks concerning the following question:
I'm writing a Tk program to choose and copy photos choosing among hundreads of them.
It is almost complete and usable but when I've used it with a very big list of files i've noticed the program was eating my memory and finally become impossible to reproduce further images.
The original error generated was
Tk::Error: not enough free memory for image buffer at [path]/lib/Tk/Image .pm line 21.
The original code that produced the above error is a bit complicated because i preload a number of photos to render them faster, it scale them to a customizable size (using modules), it handle rotation looking into EXIF tags..
So even if i was almost sure to had cleared all unneeded variables I decided to look at some classic author's example before claiming that Tk was eating my memory.
I've found Tk-thumbnail-viewer and i tried it loading seven pics of 3.5 Mb each one: the program occupy ~58Mb after had loaded thumbs and ~225Mb after i've clicked all 7 thumbs!
So I prepared a little test program reproducing the memory leak: on start it consumes ~7Mb but end at 7th photos with ~222Mb.
Looking at the process it seems sometimes it release some Mb of memory. Im working with Tk 804.032 on strawberry Perl 5.14.2
here the test program
#!/usr/bin/perl use warnings; use strict; use Tk; use Tk::JPEG; use Tk::Pane; use File::Spec; use Data::Dump; my $glob = $ARGV[0]||'./*.jpg'; my @files; &build_list($glob); ########################################################### my $mw = new MainWindow (); $mw->geometry("100x50+0+0"); $mw->Button(-text => "nex picture", -command => sub{ &next_pic; } )->pack(); my $phwin = $mw->Toplevel(); # default empty image # see http://www.perlmonks.org/?node_id=535837 my $image = $phwin->Photo(-file => '' ) or die $!; my $photo_label; #fill mainframe with default screen setup_pane(); $mw->MainLoop; ###################################################################### +########## sub next_pic { my $pic_file = shift @files; print "processing [$pic_file]\n"; $image->blank; $image = $phwin->Photo(-file => $pic_file ); $photo_label->configure(-image => $image ); } ############################################################# # see http://www.perlmonks.org/?node_id=535837 sub setup_pane{ my $pane = $phwin->Scrolled('Pane', Name => 'Main Display', -width => 1000, -height =>1000, -background => 'black', -scrollbars => 'osoe', -sticky => 'n', )->pack(-side => "left", -anchor => "n", -fill=>'both',-expand=>1); # the global! $photo_label = $pane->Label(-image => $image, -background =>'black' )->pack(-side => 'top', -anchor => 'n', -fill => 'both', -expand => 1, ); } ###################################################################### +########## sub build_list{ my $glob=shift; print "received [$glob]\n"; map { push @files,File::Spec->file_name_is_absolute($_) ? $_ : File::Spec->rel2abs($_); } glob($glob); print "$_\n" for @files; }
How can i inspect the memory used by the program?
How can i prevent memory usage increase while cycling my photos?
I'm missing some Tk related best practice?
L*
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Tk photo display: memory never released
by Anonymous Monk on Jul 04, 2016 at 09:46 UTC | |
by Anonymous Monk on Jul 04, 2016 at 09:58 UTC | |
by Discipulus (Canon) on Jul 04, 2016 at 10:11 UTC | |
by Anonymous Monk on Jul 04, 2016 at 21:31 UTC | |
by Discipulus (Canon) on Jul 04, 2016 at 21:56 UTC | |
by Anonymous Monk on Jul 04, 2016 at 21:54 UTC | |
by Discipulus (Canon) on Jul 04, 2016 at 10:12 UTC | |
Re: Tk photo display: memory never released (not leaking solution feeding -data to Tk::Photo)
by Discipulus (Canon) on Jul 05, 2016 at 08:22 UTC |