此内容没有您所选择的语言版本。
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:
- M_MXFAST
- M_TRIM_THRESHOLD
- M_TOP_PAD
- M_MMAP_THRESHOLD
- M_MMAP_MAX
- M_CHECK_ACTION
- M_PERTURB
- M_ARENA_TEST
- M_ARENA_MAX
Specific definitions for the above can be found at http://www.makelinux.net/man/3/M/mallopt.
malloc_trim
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
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.
Further Information
More information on
mallopt
can be found at http://www.makelinux.net/man/3/M/mallopt and http://www.gnu.org/software/libc/manual/html_node/Malloc-Tunable-Parameters.html.