I want to test the performance improvement that I could get by using Google’s tcmalloc. My program is built using quite a lot of the utilities provided by glib (hashes, lists, arrays, …). So what I want is basically to make glib to use tcmalloc instead of glibc’s malloc.
I could address this issue with two approaches:
- By compiling glib with the
-tcmallocoption. - by using
g_mem_set_vtable ()from the glib’s memory allocation functions.
I actually prefer the second one but I have not found any examples for implementing it.
Any hints ? Any ideas for doing this ?
You could use the
LD_PRELOADmethod suggested in the tcmalloc documentation.Alternatively, before using any glib functions, load the tcmalloc library using
dlopen().dlsym()themalloc(),realloc(), andfree()routines, and initialize astruct GMemVTablewith them. (Assuming the tcmalloccalloc()is superior, that too). Be sure to initialize members you don’t use to0(C99 named member initialization is great for this). Lastly callg_mem_set_vtable()