Problems freeing memory in GHashTable Glib 2.12.11



Hello,

 

I’m having difficulties freeing memory from a GHashTable. First, I add 1000000 elements to the hashtable, the image01.jpg in attach shows the top output from the process after the additions. At this moment the process is consuming 288M of physical memory. Then, I call the g_hash_table_remove_all function to remove all the elements from the hashtable, the image02.jpg shows the top output after the deletions. At this point I’m starting to get confused. I was expecting to free more memory than the top shows (image03.jpg), it only frees 3M of it showing 285M . And now, even more weird is when I add again the 1000000 elements to the hashtable and the memory doesn’t 288M, like earlier, but increases 1M, showing 289M. This is supposed to happen? My program is very simple, the code is in attachment.

 

My system is Debian GNU/Linux 2.6.18 and Glib 2.12.11 was compiled from source.

 

Thank you,

Pedro Simões

 

Attachment: image03.JPG
Description: image03.JPG

Attachment: image01.JPG
Description: image01.JPG

Attachment: image02.JPG
Description: image02.JPG

#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct data
{
        int age;
        char name[256];
};

void free_data(gpointer d)
{
        g_free(d);
}

int main()
{
        GHashTable *ht;
        struct data *d;
        int i, n_data = 1000000 ;

        ht = g_hash_table_new_full(g_int_hash, g_int_equal, g_free, free_data);

        for(i=0; i < n_data; i++) {
                d = g_new0(struct data, 1);
                d->age = 27;
                strcpy(d->name, "MyName");
                g_hash_table_insert(ht, g_memdup(&i,sizeof(i)), d);
        }

        printf("Size: %d\n", g_hash_table_size(ht));

        sleep(10);

        g_hash_table_remove_all(ht);

        printf("Removed elements\n");

        sleep(10);

        for(i=0; i < n_data; i++) {
                d = g_new0(struct data, 1);
                d->age = 27;
                strcpy(d->name, "MyName");
                g_hash_table_insert(ht, g_memdup(&i,sizeof(int)), d);
        }

        printf("Size: %d\n", g_hash_table_size(ht));

        sleep(30);

        return 0;
}


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