Using jemalloc with static link

Greg Dhuse gdhuse at
Fri Jul 19 08:23:33 PDT 2013

Hi Colin,

On Jul 19, 2013, at 2:41 AM, Colin Hercus <colin at> wrote:

> Hi,
> I am trying to test a HPC appl'n with jemalloc to see if it can reduce memory fragmentation and slow memory growth of a long running multi-threaded app.
> This application is statically linked as it's not open source and we need one binary distribution to run on multiple Linux versions.
> If I configure jemalloc without a jemalloc prefix (je_) and link with -static -ljemalloc I get linker errors for multiply defined symbols
> g++  -m64 -Wl,--eh-frame-hdr -o ./bin/xxxx ./obj/xxxx.o  .....   -static -pthread -lcrypto -lz -lbz2 -ldl  -ljemalloc -lunwind -Wl,-Map=gccmaps/
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libcrypto.a(dso_dlfcn.o): In function `dlfcn_globallookup':
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a(malloc.o): In function `__libc_free':
> (.text+0x56b0): multiple definition of `free'
> jemalloc.o:/wd5/downloads/malloc/jemalloc-3.4.0/src/jemalloc.c:1254: first defined here
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a(malloc.o): In function `__libc_malloc':
> (.text+0x50d0): multiple definition of `malloc'
> jemalloc.o:/wd5/downloads/malloc/jemalloc-3.4.0/src/jemalloc.c:857: first defined here
> /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/libc.a(malloc.o): In function `__libc_realloc':
> (.text+0x57a0): multiple definition of `realloc'
> jemalloc.o:/wd5/downloads/malloc/jemalloc-3.4.0/src/jemalloc.c:1105: first defined here
> If I build with a prefix of je_ then linking is okay but memory allocation is via glibc malloc. The same is true if I dynamically link without a prefix.
> Is there any simple easy way to get je_malloc to hook itself in as a replacement for malloc the way tcmalloc does?

One option is to use glibc's mallloc hooks:

Here's a small example program that hooks malloc and free, calling jemalloc's implementation built with "--with-jemalloc-prefix=je_":

#include <stdlib.h>
#include <jemalloc/jemalloc.h>

/* Prototypes for __malloc_hook, __free_hook */
#include <malloc.h>

/* Prototypes for our hooks.  */
static void my_init_hook (void);
static void *my_malloc_hook (size_t, const void *);
static void my_free_hook (void*, const void *);
/* Override initializing hook from the C library. */
void (*__malloc_initialize_hook) (void) = my_init_hook;

static void
my_init_hook (void)
	__malloc_hook = my_malloc_hook;
	__free_hook = my_free_hook;
static void *
my_malloc_hook (size_t size, const void *caller)
	return je_malloc(size);

static void
my_free_hook (void *ptr, const void *caller) 

int main() 
	je_malloc_stats_print(NULL, NULL, NULL);

	void *foo = malloc(123);
	je_malloc_stats_print(NULL, NULL, NULL);

	return 0;


More information about the jemalloc-discuss mailing list