Revisting: glib/gtk memory (re)use



I was under the understanding that glib tries to intelligently reuse
dynamically allocated memory.  That is, when you g_free() a hunk of
memory, it gets placed on a free list so that it can possibly be
recycled in the future when g_malloc() and friends are called.

(Yes, I am also aware that on most systems, the C runtime library will
do the same for malloc()/free() calls, but this is not the issue
here.  In this situation, I am referring strictly to glib and gtk.)

However, after trying to track down memory leaks rather
unsuccessfully, I tried a small test (GTK) program.  It simply creates
a toplevel GTK widget and then immediately destroys it.  This is done
1000 times.  This is then done 5 more times (for a total of 5000).
The program code is included below.

Each time after completing 1000 iterations, I checked the memory use
of the program and it keeps increasing.  So unless my eyes deceive me,
my understanding must be flawed.  Can someone clear this up?

Puzzled,
--andy


#include <unistd.h>
#include <gtk/gtk.h>

#define INNER	1000
#define OUTER	5

void foo(void);

int main(int argc, char **argv)
{
	int i;

	gtk_init(&argc, &argv);
	
	for (i = 0; i < OUTER; i++) {
		printf("iteration %d\n", i + 1);
		foo();
	}

	return 0;
}

void
foo(void)
{
	GtkWidget *widget;
	int i;

	printf("sleeping for 5 seconds before starting...\n");
	sleep(5);
	for (i = 0; i < INNER; i++) {
		widget = gtk_window_new(GTK_WINDOW_TOPLEVEL);
		gtk_widget_destroy(widget);
	}
	printf("sleeping for 5 more seconds before exiting...\n");
	sleep(5);
	/* at this point, memory use *ALWAYS* increases! */
}



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