Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Losing my memory using Tk::Photo

by Discipulus (Abbot)
on Feb 04, 2021 at 20:29 UTC ( #11127895=note: print w/replies, xml ) Need Help??

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:

# # 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 ;)


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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://11127895]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2021-04-13 05:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found