arenas.extend + thread.arena confusion

D'Alessandro, Luke K ldalessa at
Tue Sep 30 11:08:10 PDT 2014

Hi everyone,

I have an application where I want every thread to have two arenas. One is use for default allocations and has access to the cache, and the other is used for private allocations through malllocx().

I do this by doing an arena.extend + thread.arena in each thread. The problem that I have is that jemalloc seems to reuse arena ids in this context. Essentially I get a trace that looks something like:

t1: t2 = pthread_create()
t1: new1 = arenas.extend
t1: old1 = thread.arena(new1)
t2: new2 = arenas.extend
t2: old2 = thread.arena(new2)

: old1 == old2 == 0

Is this behavior expected? Shouldn’t jemalloc use a fresh arena for each new thread?

The actual test code is attached.


#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <pthread.h>
#include <jemalloc/jemalloc.h>

void *foo(void *UNUSED) {
  unsigned old, new;
  size_t sold, snew;
  sold = snew = sizeof(old);
  int e = mallctl("arenas.extend", &new, &snew, NULL, 0);
  e = mallctl("thread.arena", &old, &sold, &new, snew);
  return (void*)old;

int main(int argc, char * const argv[]) {
  pthread_t thread;
  int e = pthread_create(&thread, NULL, foo, NULL);
  void *arena1 = foo(NULL);
  void *arena2;
  e = pthread_join(thread, &arena2);
  printf("Saw arenas %u and %u.\n", (unsigned)arena1, (unsigned)arena2);
  assert((unsigned)arena1 != (unsigned)arena2);
  return 0;

More information about the jemalloc-discuss mailing list