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

Hi Monks, I'm starting to use Tk on Win32 for a small app. I start with a very lightweight timer window and I see the process is already using more than 7Mb in memory. The specs of my app (which is more than just this timer) require less than 4Mb so I have a small problem here.... Actually, just this code: use Tk; sleep(10000); uses more than 6Mb. Is there any way to reduce this memory overhead ? Thanks Jerome

Replies are listed 'Best First'.
Re: Tk Win32 memory overhead
by jettero (Monsignor) on Nov 01, 2007 at 14:38 UTC
    I'm a little surprised it's that high, but I don't think there's a way to reduce that. It represents pulling up all those .pm and .dll symbols into the program's memory space. I suspect you're kinda stuck with it unless you build your own XS that does the Tk parts and only do perl things in perl.

    That's a doable thing, but probably a lot more work than you're hoping. XS can also be quite challenging. Perhaps Inline::C can help. Do your Tk things in there? Still seems like a lot of work. I hate to say it, but if you're trying to fit in a small amount of ram, the perl GUI choices may not be the best choice. :(

    -Paul

Re: Tk Win32 memory overhead
by zentara (Cardinal) on Nov 01, 2007 at 16:58 UTC
    Yeah, that is the roughly normal mem size for a Tk window, even on linux.

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      But does it just do that from perl? or does it do that from C also? That's the real question imo.

      -Paul

        Its hard to test Tk against C, but with Gtk2 it can be done. Below are the same program, one in Gtk2-c and one Gtk2-perl. Here are the top output when run:
        # for c version zentara 15 0 14368 7012 5400 S 0.0 0.7 0:00 .07 . +/basic-window-c #for the Perl version zentara 18 0 21700 12m 9056 S 0.0 1.3 0:00 12 + ./basic-window.pl
        As you can see Perl uses way more memory; 12m versus 7k for c. The question becomes, with systems with 1 and 2 gigs of ram being common, can you afford the extra ram usage, in exchange for the ease of writing it in Perl? For me, the answer is yes. C can take hours, where Perl is done in minutes.
        /* compile test-c.c with gcc -o test-c test-c.c `pkg-config --cflags --libs gtk+-2.0` */ #include <gtk/gtk.h> void destroy(GtkWidget *widget, gpointer data) { gtk_main_quit(); } int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *button; gtk_init(&argc, &argv); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK(des +troy), 1); gtk_container_set_border_width(GTK_CONTAINER (window), 10); button = gtk_button_new_with_label ("Some Button"); gtk_container_add(GTK_CONTAINER(window), button); gtk_widget_show(button); gtk_widget_show (window); gtk_main (); return 0; }
        #!/usr/bin/perl use warnings; use strict; use Gtk2 '-init'; my $window = Gtk2::Window->new; my $button = Gtk2::Button->new('Some Button'); my $vbox = Gtk2::VBox->new(); $window->set( border_width => 15 ); $window->add($button); $window->signal_connect('destroy', sub { Gtk2->main_quit }); $window->show_all(); Gtk2->main;

        I'm not really a human, but I play one on earth. Cogito ergo sum a bum