Malloc multiheap

By default, the malloc subsystem uses a single heap, or free memory pool.

However, it also provides an optional multiheap capability to allow the use of multiple heaps of free memory, rather than just one.

The purpose of providing multiple-heap capability in the malloc subsystem is to improve the performance of threaded applications running on multiprocessor systems. When the malloc subsystem is limited to using a single heap, simultaneous memory-allocation requests received from threads running on separate processors are serialized. The malloc subsystem can therefore only service one thread at a time, resulting in a serious impact on multiprocessor system performance.

With malloc multiheap capability enabled, the malloc subsystem creates a fixed number of heaps for its use. It will begin to use multiple heaps after the second thread is started (process becomes multithreaded). Each memory-allocation request will be serviced using one of the available heaps. The malloc subsystem can then process memory allocation requests in parallel, as long as the number of threads simultaneously requesting service is less than or equal to the number of heaps.

If the number of threads simultaneously requesting service exceeds the number of heaps, additional simultaneous requests will be serialized. Unless this occurs on an ongoing basis, the overall performance of the malloc subsystem should be significantly improved when multiple threads are making calls to the malloc subroutine in a multiprocessor environment.

Enabling malloc multiheap

Malloc multiheap is not enabled by default. It is enabled and configured by setting the MALLOCOPTIONS environment variable. To enable malloc multiheap with the default settings, set MALLOCOPTIONS=multiheap prior to process startup. Setting MALLOCOPTIONS in this manner will enable malloc multiheap in its default configuration, with 32 heaps and the fast heap selection algorithm.

Malloc multiheap options

The Malloc Multiheap options are as follows:

  • multiheap:n
  • considersize

Each of these options is described in detail later in this document.

To set any of these options, use the following syntax:

MALLOCOPTIONS=[multiheap:n] | [considersize]

One or both options can be specified in any order, as long as options are comma-separated, as in the following example:

MALLOCOPTIONS=multiheap:3,considersize

In the preceding example, malloc multiheap would be enabled with three heaps and a somewhat slower heap selection algorithm that tries to minimize process size.

Each configuration option should only be specified once when setting MALLOCOPTIONS. If a configuration option is specified more than once per setting, only the final instance will apply.

The Malloc Multiheap options are described as follows:

multiheap:n
By default, the maximum number of heaps available to malloc multiheap is 32. The multiheap:n option can be used to change the maximum number of heaps to any value from 1 through 32, where n is the number of heaps. If n is set to a value outside the given range, the default value of 32 is used. Only enable as many heaps as are necessary for the process' requirements. Unnecessarily enabled heaps can increase the amount of fragmentation and waste.
considersize
By default, malloc multiheap selects the next available heap. If the considersize option is specified, malloc multiheap will use an alternate heap-selection algorithm that tries to select an available heap that has enough free space to handle the request. This may minimize the working set size of the process by reducing the number of sbrk subroutine calls. However, because of the additional processing required, the considersize heap-selection algorithm is somewhat slower than the default heap selection algorithm.

If the heaps are unable to allocate space, the malloc subroutine will return NULL and set errno to ENOMEM. If there is no available memory in the current heap, the malloc subsystem will check the other heaps for available space.