1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH MALLOPT 3 2021-03-22 "Linux" "Linux Programmer's Manual"
27 mallopt \- set memory allocation parameters
30 .B #include <malloc.h>
32 .BI "int mallopt(int " param ", int " value );
37 function adjusts parameters that control the behavior of the
38 memory-allocation functions (see
42 argument specifies the parameter to be modified, and
44 specifies the new value for that parameter.
46 The following values can be specified for
50 If this parameter has a nonzero value,
51 it defines a hard limit on the maximum number of arenas that can be created.
52 An arena represents a pool of memory that can be used by
54 (and similar) calls to service allocation requests.
55 Arenas are thread safe and
56 therefore may have multiple concurrent memory requests.
57 The trade-off is between the number of threads and the number of arenas.
58 The more arenas you have, the lower the per-thread contention,
59 but the higher the memory usage.
61 The default value of this parameter is 0,
62 meaning that the limit on the number of arenas is determined
63 according to the setting of
66 This parameter has been available since glibc 2.10 via
67 .BR \-\-enable\-experimental\-malloc ,
68 and since glibc 2.15 by default.
69 In some versions of the allocator there was no limit on the number
70 of created arenas (e.g., CentOS 5, RHEL 5).
72 When employing newer glibc versions, applications may in
73 some cases exhibit high contention when accessing arenas.
74 In these cases, it may be beneficial to increase
76 to match the number of threads.
77 This is similar in behavior to strategies taken by tcmalloc and jemalloc
78 (e.g., per-thread allocation pools).
81 This parameter specifies a value, in number of arenas created,
82 at which point the system configuration will be examined
83 to determine a hard limit on the number of created arenas.
86 for the definition of an arena.)
88 The computation of the arena hard limit is implementation-defined
89 and is usually calculated as a multiple of the number of available CPUs.
90 Once the hard limit is computed, the result is final and constrains
91 the total number of arenas.
93 The default value for the
95 parameter is 2 on systems where
97 is 4; otherwise the default value is 8.
99 This parameter has been available since glibc 2.10 via
100 .BR \-\-enable\-experimental\-malloc ,
101 and since glibc 2.15 by default.
110 Setting this parameter controls how glibc responds when various kinds
111 of programming errors are detected (e.g., freeing the same pointer twice).
112 The 3 least significant bits (2, 1, and 0) of the value assigned
113 to this parameter determine the glibc behavior, as follows:
117 If this bit is set, then print a one-line message on
119 that provides details about the error.
120 The message starts with the string "***\ glibc detected\ ***",
121 followed by the program name,
122 the name of the memory-allocation function in which the error was detected,
123 a brief description of the error,
124 and the memory address where the error was detected.
127 If this bit is set, then,
128 after printing any error message specified by bit 0,
129 the program is terminated by calling
131 In glibc versions since 2.4,
132 if bit 0 is also set,
133 then, between printing the error message and aborting,
134 the program also prints a stack trace in the manner of
136 and prints the process's memory mapping in the style of
141 Bit 2 (since glibc 2.4)
142 This bit has an effect only if bit 0 is also set.
144 then the one-line message describing the error is simplified
145 to contain just the name of the function where the error
146 was detected and the brief description of the error.
149 The remaining bits in
153 Combining the above details,
154 the following numeric values are meaningful for
158 Ignore error conditions; continue execution (with undefined results).
160 Print a detailed error message and continue execution.
164 Print detailed error message, stack trace, and memory mappings,
165 and abort the program.
167 Print a simple error message and continue execution.
169 Print simple error message, stack trace, and memory mappings,
170 and abort the program.
173 Since glibc 2.3.4, the default value for the
176 In glibc version 2.3.3 and earlier, the default value is 1.
180 value can be useful because otherwise a crash may happen much later,
181 and the true cause of the problem is then very hard to track down.
184 .\" The following text adapted from comments in the glibc source:
185 This parameter specifies the maximum number of allocation requests that
186 may be simultaneously serviced using
188 This parameter exists because some systems have a limited number
189 of internal tables for use by
191 and using more than a few of them may degrade performance.
193 The default value is 65,536,
194 a value which has no special significance and
195 which serves only as a safeguard.
196 Setting this parameter to 0 disables the use of
198 for servicing large allocation requests.
201 For allocations greater than or equal to the limit specified (in bytes) by
203 that can't be satisfied from the free list,
204 the memory-allocation functions employ
206 instead of increasing the program break using
209 Allocating memory using
211 has the significant advantage that the allocated memory blocks
212 can always be independently released back to the system.
214 the heap can be trimmed only if memory is freed at the top end.)
215 On the other hand, there are some disadvantages to the use of
217 deallocated space is not placed on the free list
218 for reuse by later allocations;
219 memory may be wasted because
221 allocations must be page-aligned;
222 and the kernel must perform the expensive task of zeroing out
225 Balancing these factors leads to a default setting of 128*1024 for the
229 The lower limit for this parameter is 0.
231 .BR DEFAULT_MMAP_THRESHOLD_MAX :
232 512*1024 on 32-bit systems or
233 .IR 4*1024*1024*sizeof(long)
237 Nowadays, glibc uses a dynamic mmap threshold by default.
238 The initial value of the threshold is 128*1024,
239 but when blocks larger than the current threshold and less than or equal to
240 .BR DEFAULT_MMAP_THRESHOLD_MAX
242 the threshold is adjusted upward to the size of the freed block.
243 When dynamic mmap thresholding is in effect,
244 the threshold for trimming the heap is also dynamically adjusted
245 to be twice the dynamic mmap threshold.
246 Dynamic adjustment of the mmap threshold is disabled if any of the
247 .BR M_TRIM_THRESHOLD ,
249 .BR M_MMAP_THRESHOLD ,
254 .BR M_MXFAST " (since glibc 2.3)"
255 .\" The following text adapted from comments in the glibc sources:
256 Set the upper limit for memory allocation requests that are satisfied
258 (The measurement unit for this parameter is bytes.)
259 Fastbins are storage areas that hold deallocated blocks of memory
260 of the same size without merging adjacent free blocks.
261 Subsequent reallocation of blocks of the same size can be handled
262 very quickly by allocating from the fastbin,
263 although memory fragmentation and the overall memory footprint
264 of the program can increase.
266 The default value for this parameter is
267 .IR "64*sizeof(size_t)/4"
268 (i.e., 64 on 32-bit architectures).
269 The range for this parameter is 0 to
270 .IR "80*sizeof(size_t)/4" .
273 to 0 disables the use of fastbins.
275 .BR M_PERTURB " (since glibc 2.4)"
276 If this parameter is set to a nonzero value,
277 then bytes of allocated memory (other than allocations via
279 are initialized to the complement of the value
280 in the least significant byte of
282 and when allocated memory is released using
284 the freed bytes are set to the least significant byte of
286 This can be useful for detecting errors where programs
287 incorrectly rely on allocated memory being initialized to zero,
288 or reuse values in memory that has already been freed.
290 The default value for this parameter is 0.
293 This parameter defines the amount of padding to employ when calling
295 to modify the program break.
296 (The measurement unit for this parameter is bytes.)
297 This parameter has an effect in the following circumstances:
300 When the program break is increased, then
302 bytes are added to the
306 When the heap is trimmed as a consequence of calling
308 (see the discussion of
309 .BR M_TRIM_THRESHOLD )
310 this much free space is preserved at the top of the heap.
314 the amount of padding is always rounded to a system page boundary.
318 is a trade-off between increasing the number of system calls
319 (when the parameter is set low)
320 and wasting unused memory at the top of the heap
321 (when the parameter is set high).
323 The default value for this parameter is 128*1024.
324 .\" DEFAULT_TOP_PAD in glibc source
327 When the amount of contiguous free memory at the top of the heap
328 grows sufficiently large,
332 to release this memory back to the system.
333 (This can be useful in programs that continue to execute for
334 a long period after freeing a significant amount of memory.)
337 parameter specifies the minimum size (in bytes) that
338 this block of memory must reach before
340 is used to trim the heap.
342 The default value for this parameter is 128*1024.
345 to \-1 disables trimming completely.
349 is a trade-off between increasing the number of system calls
350 (when the parameter is set low)
351 and wasting unused memory at the top of the heap
352 (when the parameter is set high).
354 .SS Environment variables
355 A number of environment variables can be defined
356 to modify some of the same parameters as are controlled by
358 Using these variables has the advantage that the source code
359 of the program need not be changed.
360 To be effective, these variables must be defined before the
361 first call to a memory-allocation function.
362 (If the same parameters are adjusted via
366 settings take precedence.)
367 For security reasons,
368 these variables are ignored in set-user-ID and set-group-ID programs.
370 The environment variables are as follows
371 (note the trailing underscore at the end of the name of some variables):
374 Controls the same parameter as
378 .BR MALLOC_ARENA_TEST
379 Controls the same parameter as
384 This environment variable controls the same parameter as
387 If this variable is set to a nonzero value,
388 then a special implementation of the memory-allocation functions is used.
389 (This is accomplished using the
392 This implementation performs additional error checking,
394 .\" On glibc 2.12/x86, a simple malloc()+free() loop is about 70% slower
395 .\" when MALLOC_CHECK_ was set.
396 than the standard set of memory-allocation functions.
397 (This implementation does not detect all possible errors;
398 memory leaks can still occur.)
400 The value assigned to this environment variable should be a single digit,
401 whose meaning is as described for
403 Any characters beyond the initial digit are ignored.
405 For security reasons, the effect of
407 is disabled by default for set-user-ID and set-group-ID programs.
410 exists (the content of the file is irrelevant), then
412 also has an effect for set-user-ID and set-group-ID programs.
415 Controls the same parameter as
419 .BR MALLOC_MMAP_THRESHOLD_
420 Controls the same parameter as
422 .BR M_MMAP_THRESHOLD .
425 Controls the same parameter as
429 .BR MALLOC_TRIM_THRESHOLD_
430 Controls the same parameter as
432 .BR M_TRIM_THRESHOLD .
435 Controls the same parameter as
442 On error, it returns 0.
450 .\" Available already in glibc 2.0, possibly earlier
452 This function is not specified by POSIX or the C standards.
453 A similar function exists on many System V derivatives,
454 but the range of values for
456 varies across systems.
457 The SVID defined options
463 but only the first of these is implemented in glibc.
466 Specifying an invalid value for
468 does not generate an error.
470 A calculation error within the glibc implementation means that
472 .\" FIXME . This looks buggy:
473 .\" setting the M_MXFAST limit rounds up: (s + SIZE_SZ) & ~MALLOC_ALIGN_MASK)
474 .\" malloc requests are rounded up:
475 .\" (req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK
476 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12129
484 does not result in fastbins being employed for all allocations of size up to
486 To ensure desired results,
488 should be rounded up to the next multiple greater than or equal to
489 .IR (2k+1)*sizeof(size_t) ,
493 .\" Bins are multiples of 2 * sizeof(size_t) + sizeof(size_t)
499 then, as expected, the bytes of allocated memory are initialized
500 to the complement of the byte in
502 and when that memory is freed,
503 the bytes of the region are initialized to the byte specified in
506 .RI off-by- sizeof(size_t)
507 error in the implementation:
508 .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12140
509 instead of initializing precisely the block of memory
510 being freed by the call
512 the block starting at
516 The program below demonstrates the use of
518 If the program is supplied with an (integer) command-line argument,
519 then that argument is used to set the
522 The program then allocates a block of memory,
523 and frees it twice (an error).
525 The following shell session shows what happens when we run this program
526 under glibc, with the default value for
532 main(): returned from first free() call
533 *** glibc detected *** ./a.out: double free or corruption (top): 0x09d30008 ***
534 ======= Backtrace: =========
535 /lib/libc.so.6(+0x6c501)[0x523501]
536 /lib/libc.so.6(+0x6dd70)[0x524d70]
537 /lib/libc.so.6(cfree+0x6d)[0x527e5d]
539 /lib/libc.so.6(__libc_start_main+0xe7)[0x4cdce7]
541 ======= Memory map: ========
542 001e4000\-001fe000 r\-xp 00000000 08:06 1083555 /lib/libgcc_s.so.1
543 001fe000\-001ff000 r\-\-p 00019000 08:06 1083555 /lib/libgcc_s.so.1
545 b7814000\-b7817000 rw\-p 00000000 00:00 0
546 bff53000\-bff74000 rw\-p 00000000 00:00 0 [stack]
547 Aborted (core dumped)
551 The following runs show the results when employing other values for
556 $ \fB./a.out 1\fP # Diagnose error and continue
557 main(): returned from first free() call
558 *** glibc detected *** ./a.out: double free or corruption (top): 0x09cbe008 ***
559 main(): returned from second free() call
560 $ \fB./a.out 2\fP # Abort without error message
561 main(): returned from first free() call
562 Aborted (core dumped)
563 $ \fB./a.out 0\fP # Ignore error and continue
564 main(): returned from first free() call
565 main(): returned from second free() call
569 The next run shows how to set the same parameter using the
571 environment variable:
575 $ \fBMALLOC_CHECK_=1 ./a.out\fP
576 main(): returned from first free() call
577 *** glibc detected *** ./a.out: free(): invalid pointer: 0x092c2008 ***
578 main(): returned from second free() call
589 main(int argc, char *argv[])
594 if (mallopt(M_CHECK_ACTION, atoi(argv[1])) != 1) {
595 fprintf(stderr, "mallopt() failed");
602 fprintf(stderr, "malloc() failed");
607 printf("main(): returned from first free() call\en");
610 printf("main(): returned from second free() call\en");
624 .BR malloc_stats (3),
628 .BR posix_memalign (3)