[ Previous | Next | Table of 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 users to enable 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, meaning that the malloc subsystem can only service one thread at a time. This can have 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, then 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 malloc() in a multiprocessor environment.

Activation and configuration of the malloc multiheap capability is available at process startup via 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, as described in the next section of this document.

MALLOCMULTIHEAP Options

MALLOCMULTIHEAP environment variable options are as follows:

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

The MALLOCMULTIHEAP environment variable is set using 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 above 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.

Each of the MALLOCMULTIHEAP options is described below:

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() calls. However, because of the additional processing required, the considersize heap selection algorithm is somewhat slower than the default heap selection algorithm.

Related Information

System Memory Allocation Using the malloc Subsystem

malloc, free, realloc, calloc, mallopt, mallinfo, alloca, or valloc Subroutine in AIX 5L Version 5.1 Technical Reference: Base Operating System and Extensions Volume 1.


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