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

General Programming Concepts: Writing and Debugging Programs


Malloc Buckets

Malloc buckets provides an optional buckets-based extension of the default allocator. It is intended to improve malloc performance for applications that issue large numbers of small allocation requests. When malloc buckets is enabled, allocation requests that fall within a predefined range of block sizes are processed by malloc buckets. All other requests are processed in the usual manner by the default allocator.

Malloc buckets is not enabled by default. It is enabled and configured prior to process startup by setting the MALLOCTYPE and MALLOCBUCKETS environment variables.

Bucket Composition and Sizing

A bucket consists of a block of memory that is subdivided into a predetermined number of smaller blocks of uniform size, each of which is an allocatable unit of memory. Each bucket is identified using a bucket number. The first bucket is bucket 0, the second bucket is bucket 1, the third bucket is bucket 2, and so on. The first bucket is the smallest and each bucket after that is larger in size than the preceding bucket, using a formula described later in this section. A maximum of 128 buckets is available per heap.

The block size for each bucket is a multiple of a bucket sizing factor. The bucket sizing factor equals the block size of the first bucket. Each block in the second bucket is twice this size, each block in the third bucket is three times this size, and so on. Therefore, a given bucket's block size is determined as follows:

block size = (bucket number + 1) * bucket sizing factor

For example, a bucket sizing factor of 16 would result in a block size of 16 bytes for the first bucket (bucket 0), 32 bytes for the second bucket (bucket 1), 48 bytes for the third bucket (bucket 2), and so on.

The bucket sizing factor must be a multiple of 8 for 32-bit implementations and a multiple of 16 for 64-bit implementations in order to guarantee that addresses returned from malloc subsystem functions are properly aligned for all data types.

The bucket size for a given bucket is determined as follows:

bucket size = number of blocks per bucket * (malloc overhead +
              ((bucket number + 1) * bucket sizing factor))

The above formula can be used to determine the actual number of bytes required for each bucket. In this formula, malloc overhead refers to the size of an internal malloc construct that is required for each block in the bucket. This internal construct is 8 bytes long for 32-bit applications and 16 bytes long for 64-bit applications. It is not part of the allocatable space available to the user, but is part of the total size of each bucket.

Number of blocks per bucket, number of buckets and bucket sizing factor are all configurable by setting the MALLOCBUCKETS environment variable.

Processing Allocations from the Buckets

A block will be allocated from one of the buckets whenever malloc buckets is enabled and an allocation request falls within the range of block sizes defined by the buckets. Each allocation request is serviced from the smallest possible bucket to conserve space.

If an allocation request is received for a bucket and all of its blocks are already allocated, malloc buckets will automatically enlarge the bucket to service the request. The number of new blocks added to enlarge a bucket is always equal to the number of blocks initially contained in the bucket, which is configurable by setting the MALLOCBUCKETS environment variable.

Support for Multiheap Processing

The malloc multiheap capability provides a means to enable multiple malloc heaps to improve the performance of threaded applications running on multiprocessor systems. Malloc buckets supports up to 128 buckets per heap. This allows the malloc subsystem to support concurrent enablement of malloc buckets and malloc multiheap so that threaded processes running on multiprocessor systems can benefit from the buckets algorithm.

Enabling Malloc Buckets

Malloc buckets is not enabled by default. It is enabled and configured by setting the following environment variables:

To enable malloc buckets with default settings, set the MALLOCTYPE environment variable as follows:

MALLOCTYPE=buckets

To enable malloc buckets with user-specified configuration options, set both the MALLOCTYPE and MALLOCBUCKETS environment variables as follows:

MALLOCTYPE=buckets

MALLOCBUCKETS=options

Where, options is a comma-separated list of one or more predefined configuration options, as defined in the next section of this document, Malloc Buckets Configuration Options.

Note: The following malloc subsystem capabilities are mutually exclusive.

Malloc Buckets Configuration Options

The MALLOCBUCKETS environment variable can be used to provide malloc buckets with one or more of the following predefined configuration options:

number_of_buckets:n

bucket_sizing_factor:n

blocks_per_bucket:n

bucket_statistics:[stdout|stderr|pathname]

Each of these options is described in detail in MALLOCBUCKETS Options.

The MALLOCBUCKETS environment variable is set using the following syntax:

MALLOCBUCKETS=[[ number_of_buckets:n | bucket_sizing_factor:n | blocks_per_bucket:n |
bucket_statistics:[stdout|stderr|pathname]],...]

More than one option can be specified (and in any order) as long as options are comma-separated, for example:

MALLOCBUCKETS=number_of_buckets:128,bucket_sizing_factor:8,bucket_statistics:stderr

MALLOCBUCKETS=bucket_statistics:stdout,blocks_per_bucket:512

Commas are the only delimiters that are valid for separating configuration options in this syntax. The use of other delimiters (such as blanks) between options will cause configuration options to be parsed incorrectly.

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

If a configuration option is specified with an invalid value, malloc buckets will write a warning message to standard error and then continue execution using a documented default value.

It is important to remember that the MALLOCBUCKETS environment variable will only be recognized by the malloc subsystem if MALLOCTYPE is set to buckets, as in the following example:

MALLOCTYPE=buckets

MALLOCBUCKETS=number_of_buckets:8,bucket_statistics:stderr

MALLOCBUCKETS Options

number_of_buckets:n
The number_of_buckets:n option can be used to specify the number of buckets available per heap, where n is the number of buckets. The value specified for n will apply to all available heaps.

The default value for number_of_buckets is 16. The minimum value allowed is 1. The maximum value allowed is 128.

bucket_sizing_factor:n
The bucket_sizing_factor:n option can be used to specify the bucket sizing factor, where n is the bucket sizing factor in bytes.

The value specified for bucket_sizing_factor must be a multiple of 8 for 32-bit implementations and a multiple of 16 for 64-bit implementations. The default value for bucket_sizing_factor is 32 for 32-bit implementations and 64 for 64-bit implementations.

blocks_per_bucket:n
The blocks_per_bucket:n option can be used to specify the number of blocks initially contained in each bucket, where n is the number of blocks. This value is applied to all of the buckets. The value of n is also used to determine how many blocks to add when a bucket is automatically enlarged because all of its blocks have been allocated.

The default value for blocks_per_bucket is 1024.

bucket_statistics:[stdout|stderr|pathname]
The bucket_statistics option will cause the malloc subsystem to output a statistical summary for malloc buckets upon normal termination of each process that calls the malloc subsystem while malloc buckets is enabled. This summary will show buckets configuration information and the number of allocation requests processed for each bucket. If multiple heaps have been enabled by way of malloc multiheap, the number of allocation requests shown for each bucket will be the sum of all allocation requests processed for that bucket for all heaps.

The buckets statistical summary will be written to one of the following output destinations, as specified with the bucket_statistics option.

If a user-specified pathname is provided, statistical output will be appended to the existing contents of the file (if any).

Standard output should not be used as the output destination for a process whose output is piped as input into another process.

The bucket_statistics option is disabled by default.

Notes:

  1. One additional allocation request will always be shown in the first bucket for the atexit() handler that prints the statistical summary.
  2. For threaded processes, additional allocation requests will be shown for some of the buckets due to malloc subsystem calls issued by the pthreads library.

Malloc Buckets Default Configuration

The following table summarizes the malloc buckets default configuration.

Configuration Option Default Value (32-bit) Default Value (64-bit)
number of buckets per heap 16 16
bucket sizing factor 32 bytes 64 bytes
allocation range 1 to 512 bytes (inclusive) 1 to 1024 bytes (inclusive)
number of blocks initially contained in each bucket 1024 1024
bucket statistical summary disabled disabled

The default configuration for malloc buckets should be sufficient to provide a performance improvement for many applications that issue large numbers of small allocation requests. However, it may be possible to achieve additional gains by setting the MALLOCBUCKETS environment variable to modify the default configuration. Developers who wish to modify the default configuration should first become familiar with the application's memory requirements and usage. Malloc buckets can then be enabled with the bucket_statistics option to fine tune the buckets configuration.

Limitations

Malloc buckets is only available for applications using the default allocator. It is not supported for the AIX 3.1 malloc.

Because of variations in memory requirements and usage, some applications may not benefit from the memory allocation scheme used by malloc buckets. Therefore, it is not advisable to enable malloc buckets system-wide. For optimal performance, malloc buckets should be enabled and configured on a per-application basis.

Related Information

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.

Malloc Multiheap, AIX 5L Version 5.1 General Programming Concepts, Chapter 19.


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