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.
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 succeeding bucket 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 preceding 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.
The number of blocks per bucket, number of buckets, and bucket-sizing factor are all set with the MALLOCBUCKETS environment variable.
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 configured by setting the MALLOCBUCKETS environment variable.
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.
Malloc buckets is not enabled by default, but 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.
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.
To set the the MALLOCBUCKETS environment variable, use 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 valid delimiters 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 the MALLOCBUCKETS environment variable. 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 writes a warning message to standard error and then continues execution using a documented default value.
The MALLOCBUCKETS environment variable will be recognized by the malloc subsystem only if MALLOCTYPE is set to buckets, as in the following example:
MALLOCTYPE=buckets
MALLOCBUCKETS=number_of_buckets:8,bucket_statistics:stderr
The default value for number_of_buckets is 16. The minimum value allowed is 1. The maximum value allowed is 128.
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.
The default value for blocks_per_bucket is 1024.
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 path name 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.
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. Before modifying the default configuration, 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.
Malloc buckets is only available for applications using the default allocator. It is not supported for the malloc 3.1 allocation policy.
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 for system-wide use. For optimal performance, malloc buckets should be enabled and configured on a per-application basis.