Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I've written this subroutine to create bitmaps for me automatically. I am working with large number of fairly large jpegs (ie, 700 or 800 k). I call this routine on a loop of filenames, but I keep running out of memory. (I have 384 MB so this should'nt be a problem). Somewhere, something isn't going out of scope like I think it should...

use TK; use TK::JPEG; sub makeLittlePic { # my $filename = shift; my $newFilename = shift; my $targetSize = shift; my $main = new MainWindow; my $jpgimg = $main -> Photo(-format => 'jpeg', -file => $filename); unless ($jpgimg->height() > $targetSize or $jpgimg->width() > $tar +getSize) { $jpgimg->write( $newFilename ); return 1; } my $newimg = $main -> Photo('-format' => 'jpeg'); if($jpgimg->height > $jpgimg->width) { $newimg->copy( $jpgimg, -subsample => int($jpgimg->height / $t +argetSize) ); } else { $newimg->copy( $jpgimg, -subsample => int($jpgimg->width / $ta +rgetSize) ); } $newimg->write( $newFilename ); $main = undef; $jpgimg = undef; $newimg = undef; return 1; }

Replies are listed 'Best First'.
Re: TK (And my Faulty Script) Eating Memory
by bbfu (Curate) on Jun 11, 2003 at 20:36 UTC

    When you create a Tk::Image (the parent class of Tk::Photo) without specifying a name, it generates a unique name. Tk then keeps references to these images, whether you do or not, so that you can later specify just a name anywhere that Tk is expecting a Tk::Image (or child).

    You have to call $image->delete() to actually delete the image. See the docs for Tk::Image.

    bbfu
    Black flowers blossom
    Fearless on my breath

Re: TK (And my Faulty Script) Eating Memory
by webfiend (Vicar) on Jun 11, 2003 at 20:32 UTC

    Are you using Tk::JPEG to create thumbnail image files? It does seem like there would be more effective ways to go about it ;-). You might consider playing with the Image::Magick modules, especially Image::Magick::Thumbnail.