Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Tk::NoteBook memory usage

by eXile (Priest)
on May 09, 2004 at 17:52 UTC ( [id://351881]=perlquestion: print w/replies, xml ) Need Help??

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

Hi,

I'm working on a perl/Tk program that contains a Tk::Notebook were regularly tabs (or 'pages' as they are called in the documentation) are added and deleted. I've noticed everytime a tab is added and deleted my memory usage goes up, so I've reduced the problem to this piece of code:

#!/usr/bin/perl use strict; use warnings; $| = 1; use Tk; use Tk::NoteBook; use Devel::Size qw(total_size); use Data::Dumper; my $mw = MainWindow->new(); my $nb = $mw->NoteBook()->pack; my $i; my $amount = $ARGV[0]; $amount ||= 20; print "0 tabs:\n"; print "total_size " . total_size($nb) . "\n"; print Dumper($nb); print "\n-------------\n"; for ($i=0;$i<$amount;$i++) { my $p1 = $nb->add("$i", -label => "$i"); $p1->Label(-text => "aap" x 100)->pack(); } for ($i=0;$i<$amount;$i++) { $nb->delete($i); } print "0 tabs: (after removing $amount tabs)\n"; print "total_size " . total_size($nb) . "\n"; print Dumper($nb); print "\n-------------\n"; ##MainLoop();
When I run this code on my PC (FreeBSD4.9 perl5.8.2 Tk804.025 (also tried Tk804.027 but same results)) after adding and removing tabs the memory usage of the Tk::Notebook increases, and the Data::Dumper output shows the '_names_'-hash inside of the blessed Tk::NoteBook-hash still contains the names of the tabs I deleted, so this obviously still takes up some memory.

This raises a lot of questions for me:
  • Is this intended behaviour, or is this an unintended side-effect? Is there a workaround for this?
  • I've tried to dig deeper into this problem, I can see Tk::Notebook uses Tk::NBFrame which in it's turn is XS-code. At that point I get lost. Any directions for debugging XS-code for people who haven't written XS-code before?
  • On my PC Devel::Size prints out the warning 'CV isn't complete' when using the total_size function on the Tk::Notebook. Anybody knows what can cause a 'code value' to be incomplete?

Replies are listed 'Best First'.
Re: Tk::NoteBook memory usage
by biosysadmin (Deacon) on May 09, 2004 at 19:02 UTC
    It seems like you've done a lot of work on tracking down this problem, why not just e-mail the author(s) with what you already have? I'm sure that they could fix it fairly easier as it's their code, and they'd probably be happy to explain it to you.
      mmm, didn't think about that, and it makes sense. I'll try that for the Tk::NoteBook part. Any suggestions on troubleshooting XS and the 'CV'-error are still welcome though.
        In my limited XS experience, I've found it best to start putting in a lot of sv_dump() calls to wherever I feel suspicious in the XS source. I have not seen the code in question, so I can't offer you my opinions where that might be.

        Since sv_dump() is not recursive (it won't follow references, nor parse array or hash elements), you may find Devel::Peek::Dump function more useful (if the code is in Perl, of course).

Re: Tk::NoteBook memory usage
by zentara (Archbishop) on May 10, 2004 at 13:27 UTC
    You have to be very careful with Tk when you start deleting widgets. The Tk::DynaTabFrame has a nice demo which lets you add and delete notebook tabs. I've monitored the demo and the memory use increases as you add tabs, it holds constant as you delete tabs, and holds constant as you re-add tabs, until you exceed the previous max-tab number, when it starts to increase again.

    This is just how you would expect it to work.

    In your particular example, I think you are assuming that when you delete a tab, it disappears completely, and it's "internal tab number" is automatically made available for reuse. I don't believe it acts that way.

    You seem to be just creating new tabs, up to infinity.

    I have seen a similar problem with other widgets, specifically the HList, which I have been toying with lately. Say for instance I add 10 entries to the HList, the internal counter of HList numbers them from 0 to 9. Now if I delete them all, and then add 10 new entries, the internal numbering is now 10 to 19, NOT 0 to 9.

    So you need to learn how to create a set number of widgets, and then reuse them over and over again.

    I realize it would be easier if you could do it like you want, but it just dosn't work that way. It may have something to do with the TCL underneath perlTk, I am not that much of an internals expert. I have been toying with the idea of testing GTK2-perl for this, and see if it behaves in a similar manner, or whether it has a better internals design.


    I'm not really a human, but I play one on earth. flash japh
      Given that, it should be possible to hid the tab, rip everything off of it, reconfigure it, and redisplay it then? Admittedly hackish, but you could create a Tk::NoteBook2 which was just a wrapper to a Tk::NoteBook, just would have to be very careful about how "fake-delete" was implemented in the subclass.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://351881]
Approved by matija
Front-paged by PERLscienceman
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found