http://qs321.pair.com?node_id=11127895


in reply to Losing my memory

Hello nzsvz9

> is taking up more and more memory with every image and never releases it

yes Tk when dealing with images indeed leaks memory! I had the very same problem with Tk::Photo

The trick is to reuse every object as much as possible, using undef against variables containing data using delete where it applies and, in my case, using a dummy empty file in $phwin->Photo(-file => "" )

In my picwoodpecker program I load many big pictures and finally got it not leaking memory. See the setup_draw_area sub to see it in action (search the program for various $tk_ph_image->delete if $tk_ph_image->blank and similar calls).

I asked a very similar question to your one when I noticed the leak: you can find it an interesting read and a possible starting point: Tk photo display: memory never released where the last post of the thread show my attempt to debug with a trivial attempt to show memory usage too. The code is full of comments and links useful to solve your problem.

Interesting part of the above perlmonks post:

# http://search.cpan.org/~srezic/Tk-804.033/pod/Image.pod # It's necessary to use the "delete" method to delete an image obje +ct and # free memory associated with it. Just using a lexical variable for + storing # the image object and letting the variable to go out of scope or s +etting to # undef is not sufficient. # # $tk_ph_image is a Tk::Photo object $tk_ph_image->delete if $tk_ph_image->blank;

Please share your findings ;)

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.