Configuring and using the malloc thread cache

The Malloc Thread Cache maintains a per-thread pool of unallocated memory for the purpose of reducing contention for the global heap structures.

This cache attempts to preallocate memory pieces for future use according to the pattern of allocations already performed by the thread. If an allocation request can be serviced using one of the unallocated pieces in the Thread Cache, it is removed from the cache and returned to the caller. If an allocation request cannot be serviced using an unallocated piece in the cache, the request is passed on to the global heap structure.

Thread cache allocation strategy|

The first time a thread requests memory less than 4096 bytes, the thread cache preallocates multiple chunks of memory of the same size from the global heap structure. It also reserves a larger piece of memory to be used to service future requests. If the thread frees a piece of memory, that piece will be kept in the thread cache for future allocations. However, if during a free the size of the thread cache goes over a certain threshold, half of the elements in the cache will be returned to the backing allocator. Essentially, the behavior of the thread cache can be described as a "batch processor", grouping up individual allocation/deallocation calls to be ran at one single time. This leads to less contention on the global heaps and, in many cases, efficiency gains.

Enabling malloc thread cache

Malloc Thread Cache is enabled by default for the Watson allocator. It can be disabled prior to process startup by setting the MALLOCOPTIONS environment variable as follows:
$ MALLOCOPTIONS=threadcache:off
Malloc Thread Cache is not enabled by default for the default allocator. It can be enabled prior to process startup by setting the MALLOCOPTIONS environment variable as follows.
$ MALLOCOPTIONS=threadcache