Red Hat Training

A Red Hat training course is available for Red Hat Enterprise Linux

Appendix A. Appendix

A.1. mallopt

mallopt is a library call that allows a program to change the behavior of the malloc memory allocator.

Example A.1. Allocator heuristics

An allocator has heuristics to determine long versus short lived objects. For the former, it attempts to allocate with mmap. For the later, it attempts to allocate with sbrk.
In order to override these heuristics, set M_MMAP_THRESHOLD.
In multi-threaded applications, the allocator creates multiple arenas in response to lock contention in existing arenas. This can improve the performance significantly for some multi-threaded applications at the cost of an increase in memory usage. To keep this under control, limit the number of arenas that can be created by using the mallopt interface.
The allocator has limits on the number of arenas it can create. For 32bit targets, it will create 2 * # core arenas; for 64bit targets, it will create 8 * # core arenas. mallopt allows the developer to override those limits.

Example A.2. mallopt

To ensure no more than eight arenas are created, issue the following library call:
mallopt (M_ARENA_MAX, 8);
The first argument for mallopt can be:
Specific definitions for the above can be found at


malloc_trim is a library call that requests the allocator return any unused memory back to the operating system. This is normally automatic when an object is freed. However, in some cases when freeing small objects, glibc might not immediately release the memory back to the operating system. It does this so that the free memory can be used to satisfy upcoming memory allocation requests as it is expensive to allocate from and release memory back to the operating system.


malloc_stats is used to dump information about the allocator's internal state to stderr. Using mallinfo is similar to this, but it places the state into a structure instead.