2 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH MALLOPT 3 2019-03-06 "Linux" "Linux Programmer's Manual"
28 mallopt \- set memory allocation parameters
30 .B #include <malloc.h>
32 .BI "int mallopt(int " param ", int " value );
36 function adjusts parameters that control the behavior of the
37 memory-allocation functions (see
41 argument specifies the parameter to be modified, and
43 specifies the new value for that parameter.
45 The following values can be specified for
49 If this parameter has a nonzero value,
50 it defines a hard limit on the maximum number of arenas that can be created.
51 An arena represents a pool of memory that can be used by
53 (and similar) calls to service allocation requests.
54 Arenas are thread safe and
55 therefore may have multiple concurrent memory requests.
56 The trade-off is between the number of threads and the number of arenas.
57 The more arenas you have, the lower the per-thread contention,
58 but the higher the memory usage.
60 The default value of this parameter is 0,
61 meaning that the limit on the number of arenas is determined
62 according to the setting of
65 This parameter has been available since glibc 2.10 via
66 .BR \-\-enable\-experimental\-malloc ,
67 and since glibc 2.15 by default.
68 In some versions of the allocator there was no limit on the number
69 of created arenas (e.g., CentOS 5, RHEL 5).
71 When employing newer glibc versions, applications may in
72 some cases exhibit high contention when accessing arenas.
73 In these cases, it may be beneficial to increase
75 to match the number of threads.
76 This is similar in behavior to strategies taken by tcmalloc and jemalloc
77 (e.g., per-thread allocation pools).
80 This parameter specifies a value, in number of arenas created,
81 at which point the system configuration will be examined
82 to determine a hard limit on the number of created arenas.
85 for the definition of an arena.)
87 The computation of the arena hard limit is implementation-defined
88 and is usually calculated as a multiple of the number of available CPUs.
89 Once the hard limit is computed, the result is final and constrains
90 the total number of arenas.
92 The default value for the
94 parameter is 2 on systems where
96 is 4; otherwise the default value is 8.
98 This parameter has been available since glibc 2.10 via
99 .BR \-\-enable\-experimental\-malloc ,
100 and since glibc 2.15 by default.
109 Setting this parameter controls how glibc responds when various kinds
110 of programming errors are detected (e.g., freeing the same pointer twice).
111 The 3 least significant bits (2, 1, and 0) of the value assigned
112 to this parameter determine the glibc behavior, as follows:
116 If this bit is set, then print a one-line message on
118 that provides details about the error.
119 The message starts with the string "***\ glibc detected\ ***",
120 followed by the program name,
121 the name of the memory-allocation function in which the error was detected,
122 a brief description of the error,
123 and the memory address where the error was detected.
126 If this bit is set, then,
127 after printing any error message specified by bit 0,
128 the program is terminated by calling
130 In glibc versions since 2.4,
131 if bit 0 is also set,
132 then, between printing the error message and aborting,
133 the program also prints a stack trace in the manner of
135 and prints the process's memory mapping in the style of
140 Bit 2 (since glibc 2.4)
141 This bit has an effect only if bit 0 is also set.
143 then the one-line message describing the error is simplified
144 to contain just the name of the function where the error
145 was detected and the brief description of the error.
148 The remaining bits in
152 Combining the above details,
153 the following numeric values are meaningful for
157 Ignore error conditions; continue execution (with undefined results).
159 Print a detailed error message and continue execution.
163 Print detailed error message, stack trace, and memory mappings,
164 and abort the program.
166 Print a simple error message and continue execution.
168 Print simple error message, stack trace, and memory mappings,
169 and abort the program.
172 Since glibc 2.3.4, the default value for the
175 In glibc version 2.3.3 and earlier, the default value is 1.
179 value can be useful because otherwise a crash may happen much later,
180 and the true cause of the problem is then very hard to track down.
183 .\" The following text adapted from comments in the glibc source:
184 This parameter specifies the maximum number of allocation requests that
185 may be simultaneously serviced using
187 This parameter exists because some systems have a limited number
188 of internal tables for use by
190 and using more than a few of them may degrade performance.
192 The default value is 65,536,
193 a value which has no special significance and
194 which serves only as a safeguard.
195 Setting this parameter to 0 disables the use of
197 for servicing large allocation requests.
200 For allocations greater than or equal to the limit specified (in bytes) by
202 that can't be satisfied from the free list,
203 the memory-allocation functions employ
205 instead of increasing the program break using
208 Allocating memory using
210 has the significant advantage that the allocated memory blocks
211 can always be independently released back to the system.
213 the heap can be trimmed only if memory is freed at the top end.)
214 On the other hand, there are some disadvantages to the use of
216 deallocated space is not placed on the free list
217 for reuse by later allocations;
218 memory may be wasted because
220 allocations must be page-aligned;
221 and the kernel must perform the expensive task of zeroing out
224 Balancing these factors leads to a default setting of 128*1024 for the
228 The lower limit for this parameter is 0.
230 .BR DEFAULT_MMAP_THRESHOLD_MAX :
231 512*1024 on 32-bit systems or
232 .IR 4*1024*1024*sizeof(long)
236 Nowadays, glibc uses a dynamic mmap threshold by default.
237 The initial value of the threshold is 128*1024,
238 but when blocks larger than the current threshold and less than or equal to
239 .BR DEFAULT_MMAP_THRESHOLD_MAX
241 the threshold is adjusted upward to the size of the freed block.
242 When dynamic mmap thresholding is in effect,
243 the threshold for trimming the heap is also dynamically adjusted
244 to be twice the dynamic mmap threshold.
245 Dynamic adjustment of the mmap threshold is disabled if any of the
246 .BR M_TRIM_THRESHOLD ,
248 .BR M_MMAP_THRESHOLD ,
253 .BR M_MXFAST " (since glibc 2.3)"
254 .\" The following text adapted from comments in the glibc sources:
255 Set the upper limit for memory allocation requests that are satisfied
257 (The measurement unit for this parameter is bytes.)
258 Fastbins are storage areas that hold deallocated blocks of memory
259 of the same size without merging adjacent free blocks.
260 Subsequent reallocation of blocks of the same size can be handled
261 very quickly by allocating from the fastbin,
262 although memory fragmentation and the overall memory footprint
263 of the program can increase.
265 The default value for this parameter is
266 .IR "64*sizeof(size_t)/4"
267 (i.e., 64 on 32-bit architectures).
268 The range for this parameter is 0 to
269 .IR "80*sizeof(size_t)/4" .
272 to 0 disables the use of fastbins.
274 .BR M_PERTURB " (since glibc 2.4)"
275 If this parameter is set to a nonzero value,
276 then bytes of allocated memory (other than allocations via
278 are initialized to the complement of the value
279 in the least significant byte of
281 and when allocated memory is released using
283 the freed bytes are set to the least significant byte of
285 This can be useful for detecting errors where programs
286 incorrectly rely on allocated memory being initialized to zero,
287 or reuse values in memory that has already been freed.
289 The default value for this parameter is 0.
292 This parameter defines the amount of padding to employ when calling
294 to modify the program break.
295 (The measurement unit for this parameter is bytes.)
296 This parameter has an effect in the following circumstances:
299 When the program break is increased, then
301 bytes are added to the
305 When the heap is trimmed as a consequence of calling
307 (see the discussion of
308 .BR M_TRIM_THRESHOLD )
309 this much free space is preserved at the top of the heap.
313 the amount of padding is always rounded to a system page boundary.
317 is a trade-off between increasing the number of system calls
318 (when the parameter is set low)
319 and wasting unused memory at the top of the heap
320 (when the parameter is set high).
322 The default value for this parameter is 128*1024.
323 .\" DEFAULT_TOP_PAD in glibc source
326 When the amount of contiguous free memory at the top of the heap
327 grows sufficiently large,
331 to release this memory back to the system.
332 (This can be useful in programs that continue to execute for
333 a long period after freeing a significant amount of memory.)
336 parameter specifies the minimum size (in bytes) that
337 this block of memory must reach before
339 is used to trim the heap.
341 The default value for this parameter is 128*1024.
344 to \-1 disables trimming completely.
348 is a trade-off between increasing the number of system calls
349 (when the parameter is set low)
350 and wasting unused memory at the top of the heap
351 (when the parameter is set high).
353 .SS Environment variables
354 A number of environment variables can be defined
355 to modify some of the same parameters as are controlled by
357 Using these variables has the advantage that the source code
358 of the program need not be changed.
359 To be effective, these variables must be defined before the
360 first call to a memory-allocation function.
361 (If the same parameters are adjusted via
365 settings take precedence.)
366 For security reasons,
367 these variables are ignored in set-user-ID and set-group-ID programs.
369 The environment variables are as follows
370 (note the trailing underscore at the end of the name of some variables):
373 Controls the same parameter as
377 .BR MALLOC_ARENA_TEST
378 Controls the same parameter as
383 This environment variable controls the same parameter as
386 If this variable is set to a nonzero value,
387 then a special implementation of the memory-allocation functions is used.
388 (This is accomplished using the
391 This implementation performs additional error checking,
393 .\" On glibc 2.12/x86, a simple malloc()+free() loop is about 70% slower
394 .\" when MALLOC_CHECK_ was set.
395 than the standard set of memory-allocation functions.
396 (This implementation does not detect all possible errors;
397 memory leaks can still occur.)
399 The value assigned to this environment variable should be a single digit,
400 whose meaning is as described for
402 Any characters beyond the initial digit are ignored.
404 For security reasons, the effect of
406 is disabled by default for set-user-ID and set-group-ID programs.
409 exists (the content of the file is irrelevant), then
411 also has an effect for set-user-ID and set-group-ID programs.
414 Controls the same parameter as
418 .BR MALLOC_MMAP_THRESHOLD_
419 Controls the same parameter as
421 .BR M_MMAP_THRESHOLD .
424 Controls the same parameter as
428 .BR MALLOC_TRIM_THRESHOLD_
429 Controls the same parameter as
431 .BR M_TRIM_THRESHOLD .
434 Controls the same parameter as
441 On error, it returns 0.
449 .\" Available already in glibc 2.0, possibly earlier
451 This function is not specified by POSIX or the C standards.
452 A similar function exists on many System V derivatives,
453 but the range of values for
455 varies across systems.
456 The SVID defined options
462 but only the first of these is implemented in glibc.
465 Specifying an invalid value for
467 does not generate an error.
469 A calculation error within the glibc implementation means that
471 .\" FIXME . This looks buggy:
472 .\" setting the M_MXFAST limit rounds up: (s + SIZE_SZ) & ~MALLOC_ALIGN_MASK)
473 .\" malloc requests are rounded up:
474 .\" (req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK
475 .\" http://sources.redhat.com/bugzilla/show_bug.cgi?id=12129
483 does not result in fastbins being employed for all allocations of size up to
485 To ensure desired results,
487 should be rounded up to the next multiple greater than or equal to
488 .IR (2k+1)*sizeof(size_t) ,
492 .\" Bins are multiples of 2 * sizeof(size_t) + sizeof(size_t)
498 then, as expected, the bytes of allocated memory are initialized
499 to the complement of the byte in
501 and when that memory is freed,
502 the bytes of the region are initialized to the byte specified in
505 .RI off-by- sizeof(size_t)
506 error in the implementation:
507 .\" FIXME . http://sources.redhat.com/bugzilla/show_bug.cgi?id=12140
508 instead of initializing precisely the block of memory
509 being freed by the call
511 the block starting at
515 The program below demonstrates the use of
517 If the program is supplied with an (integer) command-line argument,
518 then that argument is used to set the
521 The program then allocates a block of memory,
522 and frees it twice (an error).
524 The following shell session shows what happens when we run this program
525 under glibc, with the default value for
531 main(): returned from first free() call
532 *** glibc detected *** ./a.out: double free or corruption (top): 0x09d30008 ***
533 ======= Backtrace: =========
534 /lib/libc.so.6(+0x6c501)[0x523501]
535 /lib/libc.so.6(+0x6dd70)[0x524d70]
536 /lib/libc.so.6(cfree+0x6d)[0x527e5d]
538 /lib/libc.so.6(__libc_start_main+0xe7)[0x4cdce7]
540 ======= Memory map: ========
541 001e4000\-001fe000 r\-xp 00000000 08:06 1083555 /lib/libgcc_s.so.1
542 001fe000\-001ff000 r\-\-p 00019000 08:06 1083555 /lib/libgcc_s.so.1
544 b7814000\-b7817000 rw\-p 00000000 00:00 0
545 bff53000\-bff74000 rw\-p 00000000 00:00 0 [stack]
546 Aborted (core dumped)
550 The following runs show the results when employing other values for
555 $ \fB./a.out 1\fP # Diagnose error and continue
556 main(): returned from first free() call
557 *** glibc detected *** ./a.out: double free or corruption (top): 0x09cbe008 ***
558 main(): returned from second free() call
559 $ \fB./a.out 2\fP # Abort without error message
560 main(): returned from first free() call
561 Aborted (core dumped)
562 $ \fB./a.out 0\fP # Ignore error and continue
563 main(): returned from first free() call
564 main(): returned from second free() call
568 The next run shows how to set the same parameter using the
570 environment variable:
574 $ \fBMALLOC_CHECK_=1 ./a.out\fP
575 main(): returned from first free() call
576 *** glibc detected *** ./a.out: free(): invalid pointer: 0x092c2008 ***
577 main(): returned from second free() call
588 main(int argc, char *argv[])
593 if (mallopt(M_CHECK_ACTION, atoi(argv[1])) != 1) {
594 fprintf(stderr, "mallopt() failed");
601 fprintf(stderr, "malloc() failed");
606 printf("main(): returned from first free() call\en");
609 printf("main(): returned from second free() call\en");
623 .BR malloc_stats (3),
627 .BR posix_memalign (3)