Hello monks!

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*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Tk photo display: memory never released by Discipulus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.