Strange memory allocation behaviour



Earlier I asked about GnomeDialog and memory allocation. I claimed
that 8kB of memory was allocated and not freed each time I opened and
closed the dialog. I wanted to test how fast I would run out of memory,
so I modified my test code to automatically close the dialog with a
timeout callback. I ran it with memprof and the result is interesting:

To start with memprof reports about 164 kB of memory in 4000
allocations. The number of allocated bytes first increase linearly
(related to time) to about 900 kB (in 9500 allocations) where the
allocation speed starts to retard. At 1006 kB allocation is realy slow,
but still increasing. Obiously the allocated memory is not linearly
related to the number of times the dialog is open and closed. But
why this behaviour? Can anybody explain?

Here is the code I used. If you don't have memprof, ps works just
as well to examine the behaviour.

Regards,
Björn Andesson

-- snip --
#include <gnome.h>
static gint
delete_event(GtkWidget *widget, GdkEvent *event, gpointer data)
{
        gtk_main_quit();
        return FALSE;
}

gboolean
timeout_cb(gpointer data)
{
        GtkWidget *dialog = data;
        gnome_dialog_close(GNOME_DIALOG(dialog));
        return FALSE;
}

void
clicked_cb(GtkButton *button,
           gpointer user_data)
{
        GtkWidget *dialog;

        while(1) 
        {
                dialog = gnome_dialog_new("The title",
                                          GNOME_STOCK_BUTTON_OK,
                                          GNOME_STOCK_BUTTON_CANCEL,
                                          NULL);
                g_timeout_add(100, timeout_cb, dialog);
                gnome_dialog_run_and_close(GNOME_DIALOG(dialog));
        }
}

int
main(int argc, char *argv[])
{
        GtkWidget *app;
        GtkWidget *button;

        gnome_init("memtest", "0.1", argc, argv);
        
        app = gnome_app_new("memtest", "Dialog memory test");
        gtk_signal_connect(GTK_OBJECT(app), "delete-event",
                           GTK_SIGNAL_FUNC(delete_event),
                           NULL);

        button = gtk_button_new_with_label("Click Me");
        gtk_signal_connect(GTK_OBJECT(button), "clicked",
                           GTK_SIGNAL_FUNC(clicked_cb),
                           NULL);
                
        gnome_app_set_contents(GNOME_APP(app), button);
        gtk_widget_show_all(app);
        gtk_main();
        return 0;
}
-- snip --

-- 
Björn Andersson  <bjorn@lifix.fi>                        +358-50-3412556
Lifix Systems Oy <http://www.lifix.fi/>     Professional Linux Solutions
Tekniikantie 21, FIN-02150 Espoo                          +358-9-4375272



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]