Memory Allocation Tunables (The GNU C Library)

### 39.3 Memory Allocation Tunables ¶

Tunable namespace: glibc.malloc ¶

  Memory allocation behavior can be modified by setting any of the following tunables in the `malloc` namespace:

Tunable: glibc.malloc.check ¶

  This tunable supersedes the `MALLOC_CHECK_` environment variable and is identical in features. This tunable has no effect by default and needs the debug library `libc_malloc_debug` to be preloaded using the `LD_PRELOAD` environment variable.

  Setting this tunable to a non-zero value less than 4 enables a special (less efficient) memory allocator for the `malloc` family of functions that is designed to be tolerant against simple errors such as double calls of free with the same argument, or overruns of a single byte (off-by-one bugs). Not all such errors can be protected against, however, and memory leaks can result. Any detected heap corruption results in immediate termination of the process.

  Like `MALLOC_CHECK_`, `glibc.malloc.check` has a problem in that it diverges from normal program behavior by writing to `stderr`, which could by exploited in SUID and SGID binaries. Therefore, `glibc.malloc.check` is disabled by default for SUID and SGID binaries.

Tunable: glibc.malloc.top_pad ¶

  This tunable supersedes the `MALLOC_TOP_PAD_` environment variable and is identical in features.

  This tunable determines the amount of extra memory in bytes to obtain from the system when any of the arenas need to be extended. It also specifies the number of bytes to retain when shrinking any of the arenas. This provides the necessary hysteresis in heap size such that excessive amounts of system calls can be avoided.

  The default value of this tunable is ‘`131072`’ (128 KB).

Tunable: glibc.malloc.perturb ¶

  This tunable supersedes the `MALLOC_PERTURB_` environment variable and is identical in features.

  If set to a non-zero value, memory blocks are initialized with values depending on some low order bits of this tunable when they are allocated (except when allocated by `calloc`) and freed. This can be used to debug the use of uninitialized or freed heap memory. Note that this option does not guarantee that the freed block will have any specific values. It only guarantees that the content the block had before it was freed will be overwritten.

  The default value of this tunable is ‘`0`’.

Tunable: glibc.malloc.mmap_threshold ¶

  This tunable supersedes the `MALLOC_MMAP_THRESHOLD_` environment variable and is identical in features.

  When this tunable is set, all chunks larger than this value in bytes are allocated outside the normal heap, using the `mmap` system call. This way it is guaranteed that the memory for these chunks can be returned to the system on `free`. Note that requests smaller than this threshold might still be allocated via `mmap`.

  If this tunable is not set, the default value is set to ‘`131072`’ bytes and the threshold is adjusted dynamically to suit the allocation patterns of the program. If the tunable is set, the dynamic adjustment is disabled and the value is set as static.

Tunable: glibc.malloc.trim_threshold ¶

  This tunable supersedes the `MALLOC_TRIM_THRESHOLD_` environment variable and is identical in features.

  The value of this tunable is the minimum size (in bytes) of the top-most, releasable chunk in an arena that will trigger a system call in order to return memory to the system from that arena.

  If this tunable is not set, the default value is set as 128 KB and the threshold is adjusted dynamically to suit the allocation patterns of the program. If the tunable is set, the dynamic adjustment is disabled and the value is set as static.

Tunable: glibc.malloc.mmap_max ¶

  This tunable supersedes the `MALLOC_MMAP_MAX_` environment variable and is identical in features.

  The value of this tunable is maximum number of chunks to allocate with `mmap`. Setting this to zero disables all use of `mmap`.

  The default value of this tunable is ‘`65536`’.

Tunable: glibc.malloc.arena_test ¶

  This tunable supersedes the `MALLOC_ARENA_TEST` environment variable and is identical in features.

  The `glibc.malloc.arena_test` tunable specifies the number of arenas that can be created before the test on the limit to the number of arenas is conducted. The value is ignored if `glibc.malloc.arena_max` is set.

  The default value of this tunable is 2 for 32-bit systems and 8 for 64-bit systems.

Tunable: glibc.malloc.arena_max ¶

  This tunable supersedes the `MALLOC_ARENA_MAX` environment variable and is identical in features.

  This tunable sets the number of arenas to use in a process regardless of the number of cores in the system.

  The default value of this tunable is `0`, meaning that the limit is the number of CPU cores online reported by get_nprocs().

Tunable: glibc.malloc.tcache_max ¶

  The maximum size of a request (in bytes) which may be met via the per-thread cache. The default (and maximum) value is 1032 bytes on 64-bit systems and 516 bytes on 32-bit systems.

Tunable: glibc.malloc.tcache_count ¶

  The maximum number of chunks of each size to cache. The default is 16. The upper limit is 65535. If set to zero, the per-thread cache is effectively disabled.

  The approximate maximum overhead of the per-thread cache is thus equal to the number of bins times the chunk count in each bin times the size of each chunk.

Tunable: glibc.malloc.mxfast ¶

  This tunable has no effect since the “fastbins” have been removed.

Tunable: glibc.malloc.hugetlb ¶

  This tunable controls the usage of Huge Pages on `malloc` calls. The default value is `0` on most targets. Using `0` disables support that improves use of huge pages in `malloc`. However huge pages may still be created depending on the OS settings.

  Setting its value to `1` enables the use of `madvise` with `MADV_HUGEPAGE` after memory allocation with `mmap`. It is enabled only if the system supports Transparent Huge Page (currently only on Linux). This is the default used for AArch64.

  When `glibc.elf.thp` set to 1, malloc uses the actual kernel THP mode instead of defaulting to madvise mode and madvise_thp will stop issuing MADV_HUGEPAGE if kernel THP mode is always.

  Setting its value to `2` enables the use of Huge Page directly with `mmap` with the use of `MAP_HUGETLB` flag. The huge page size to use will be the default one provided by the system. A value larger than `2` specifies huge page size, which will be matched against the system supported ones. If provided value is invalid, `MAP_HUGETLB` will not be used.