[ Bottom of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]

General Programming Concepts:
Writing and Debugging Programs

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.

Activation and configuration of the malloc multiheap capability is available at process startup through the MALLOCMULTIHEAP environment variable. The maximum number of heaps available with malloc multiheap enabled is 32.

Enabling Malloc Multiheap

Malloc multiheap is not enabled by default. It is enabled and configured by setting the MALLOCMULTIHEAP environment variable.

To enable malloc multiheap with default settings, set the MALLOCMULTIHEAP environment variable to any non-null value, as follows:

  MALLOCMULTIHEAP=true

Setting MALLOCMULTIHEAP in this manner will enable malloc multiheap in its default configuration, with all 32 heaps and the fast heap selection algorithm.

To enable malloc multiheap with user-specified configuration options, set the MALLOCMULTIHEAP environment variable as follows:

  MALLOCMULTIHEAP=options

where options is a comma-separated list of one or more predefined configuration options.

MALLOCMULTIHEAP Options

The MALLOCMULTIHEAP environment variable options are as follows:

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

To set the MALLOCMULTIHEAP environment variable, use the following syntax:

  MALLOCMULTIHEAP=[heaps:n] | [considersize]

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

  MALLOCMULTIHEAP=heaps: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 MALLOCMULTIHEAP. If a configuration option is specified more than once per setting, only the final instance will apply.

The MALLOCMULTIHEAP options are described as follows:

heaps:n
By default, the maximum number of heaps available to malloc multiheap is 32. The heaps: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.
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.

[ Top of Page | Previous Page | Next Page | Contents | Index | Library Home | Legal | Search ]