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

I've written a Tk app that seems to have some kind of memory leak. I thought Devel::Size might help me track the leak down, but it doesnt seem to work when I apply it to MainWindows or other Tk objects. It seems to enter an endless loop and repeatedly prints out "CV isn't complete".

Does anyone know what this means or, better yet, how to reduce Tk memory usage?

TIA,
Bill

NOTE: Source for the aformentioned app is available at http://www.milkbone.org.

Replies are listed 'Best First'.
Re: Devel::Size with Tk
by Elian (Parson) on Feb 21, 2003 at 15:05 UTC
    Well, the "CV isn't complete" message means that you're trying to get the size (directly or indirectly) of a code ref, which Devel::Size doesn't completely support at the moment. (Specifically it doesn't know how to look for scratchpads, and it doesn't have a way to walk the optree holding the code and check how big it is)

    Devel::Size isn't supposed to get into any sort of infinite loop--there's code in it to identify all the items it checks the size of so it doesn't get into a loop. If it is, it means I missed something somewhere, so I'll go check.

    You'll still not necessarily get the full memory size for your stuff, unfortunately, since Tk allocates memory itself in spots (both the Tk module and the Tk libraries) which Devel::Size doesn't know about.

Re: Devel::Size with Tk ()
by tye (Sage) on Feb 21, 2003 at 17:21 UTC

    I was convinced that Tk leaked like a sieve and then I heard that Perl created circular references that prevent subroutines from being garbage collected. This prompted me to rework my Tk applications so that I didn't use convenient closures but instead reused (more complicated) static subroutines. This got rid of all my leaks.

    So, for example, I switched from using

    -command => sub { MyRefresh($row,"force") },
    to using
    -command => [ \&MyButton, "refresh",$row,"force" ],

    This memory leak problem was identified a long time ago so I think it is fixed in Perl v5.8, but I haven't verified that. I know it isn't fixed in Perl v5.6.

                    - tye