share/mk/: build-html: Don't build mbind.2 and set_mempolicy.2
[man-pages.git] / man3 / mallopt.3
blob8224872511e726aa3ee2fb555e5a442876cad250
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .TH mallopt 3 (date) "Linux man-pages (unreleased)"
6 .SH NAME
7 mallopt \- set memory allocation parameters
8 .SH LIBRARY
9 Standard C library
10 .RI ( libc ", " \-lc )
11 .SH SYNOPSIS
12 .nf
13 .B #include <malloc.h>
15 .BI "int mallopt(int " param ", int " value );
16 .fi
17 .SH DESCRIPTION
18 The
19 .BR mallopt ()
20 function adjusts parameters that control the behavior of the
21 memory-allocation functions (see
22 .BR malloc (3)).
23 The
24 .I param
25 argument specifies the parameter to be modified, and
26 .I value
27 specifies the new value for that parameter.
29 The following values can be specified for
30 .IR param :
31 .TP
32 .B M_ARENA_MAX
33 If this parameter has a nonzero value,
34 it defines a hard limit on the maximum number of arenas that can be created.
35 An arena represents a pool of memory that can be used by
36 .BR malloc (3)
37 (and similar) calls to service allocation requests.
38 Arenas are thread safe and
39 therefore may have multiple concurrent memory requests.
40 The trade-off is between the number of threads and the number of arenas.
41 The more arenas you have, the lower the per-thread contention,
42 but the higher the memory usage.
43 .IP
44 The default value of this parameter is 0,
45 meaning that the limit on the number of arenas is determined
46 according to the setting of
47 .BR M_ARENA_TEST .
48 .IP
49 This parameter has been available since glibc 2.10 via
50 .BR \-\-enable\-experimental\-malloc ,
51 and since glibc 2.15 by default.
52 In some versions of the allocator there was no limit on the number
53 of created arenas (e.g., CentOS 5, RHEL 5).
54 .IP
55 When employing newer glibc versions, applications may in
56 some cases exhibit high contention when accessing arenas.
57 In these cases, it may be beneficial to increase
58 .B M_ARENA_MAX
59 to match the number of threads.
60 This is similar in behavior to strategies taken by tcmalloc and jemalloc
61 (e.g., per-thread allocation pools).
62 .TP
63 .B M_ARENA_TEST
64 This parameter specifies a value, in number of arenas created,
65 at which point the system configuration will be examined
66 to determine a hard limit on the number of created arenas.
67 (See
68 .B M_ARENA_MAX
69 for the definition of an arena.)
70 .IP
71 The computation of the arena hard limit is implementation-defined
72 and is usually calculated as a multiple of the number of available CPUs.
73 Once the hard limit is computed, the result is final and constrains
74 the total number of arenas.
75 .IP
76 The default value for the
77 .B M_ARENA_TEST
78 parameter is 2 on systems where
79 .I sizeof(long)
80 is 4; otherwise the default value is 8.
81 .IP
82 This parameter has been available since glibc 2.10 via
83 .BR \-\-enable\-experimental\-malloc ,
84 and since glibc 2.15 by default.
85 .IP
86 The value of
87 .B M_ARENA_TEST
88 is not used when
89 .B M_ARENA_MAX
90 has a nonzero value.
91 .TP
92 .B M_CHECK_ACTION
93 Setting this parameter controls how glibc responds when various kinds
94 of programming errors are detected (e.g., freeing the same pointer twice).
95 The 3 least significant bits (2, 1, and 0) of the value assigned
96 to this parameter determine the glibc behavior, as follows:
97 .RS
98 .TP
99 Bit 0
100 If this bit is set, then print a one-line message on
101 .I stderr
102 that provides details about the error.
103 The message starts with the string "***\ glibc detected\ ***",
104 followed by the program name,
105 the name of the memory-allocation function in which the error was detected,
106 a brief description of the error,
107 and the memory address where the error was detected.
109 Bit 1
110 If this bit is set, then,
111 after printing any error message specified by bit 0,
112 the program is terminated by calling
113 .BR abort (3).
114 Since glibc 2.4,
115 if bit 0 is also set,
116 then, between printing the error message and aborting,
117 the program also prints a stack trace in the manner of
118 .BR backtrace (3),
119 and prints the process's memory mapping in the style of
120 .IR /proc/ pid /maps
121 (see
122 .BR proc (5)).
124 Bit 2 (since glibc 2.4)
125 This bit has an effect only if bit 0 is also set.
126 If this bit is set,
127 then the one-line message describing the error is simplified
128 to contain just the name of the function where the error
129 was detected and the brief description of the error.
132 The remaining bits in
133 .I value
134 are ignored.
136 Combining the above details,
137 the following numeric values are meaningful for
138 .BR M_CHECK_ACTION :
139 .RS 12
141 .B 0
142 Ignore error conditions; continue execution (with undefined results).
144 .B 1
145 Print a detailed error message and continue execution.
147 .B 2
148 Abort the program.
150 .B 3
151 Print detailed error message, stack trace, and memory mappings,
152 and abort the program.
154 .B 5
155 Print a simple error message and continue execution.
157 .B 7
158 Print simple error message, stack trace, and memory mappings,
159 and abort the program.
162 Since glibc 2.3.4, the default value for the
163 .B M_CHECK_ACTION
164 parameter is 3.
165 In glibc 2.3.3 and earlier, the default value is 1.
167 Using a nonzero
168 .B M_CHECK_ACTION
169 value can be useful because otherwise a crash may happen much later,
170 and the true cause of the problem is then very hard to track down.
172 .B M_MMAP_MAX
173 .\" The following text adapted from comments in the glibc source:
174 This parameter specifies the maximum number of allocation requests that
175 may be simultaneously serviced using
176 .BR mmap (2).
177 This parameter exists because some systems have a limited number
178 of internal tables for use by
179 .BR mmap (2),
180 and using more than a few of them may degrade performance.
182 The default value is 65,536,
183 a value which has no special significance and
184 which serves only as a safeguard.
185 Setting this parameter to 0 disables the use of
186 .BR mmap (2)
187 for servicing large allocation requests.
189 .B M_MMAP_THRESHOLD
190 For allocations greater than or equal to the limit specified (in bytes) by
191 .B M_MMAP_THRESHOLD
192 that can't be satisfied from the free list,
193 the memory-allocation functions employ
194 .BR mmap (2)
195 instead of increasing the program break using
196 .BR sbrk (2).
198 Allocating memory using
199 .BR mmap (2)
200 has the significant advantage that the allocated memory blocks
201 can always be independently released back to the system.
202 (By contrast,
203 the heap can be trimmed only if memory is freed at the top end.)
204 On the other hand, there are some disadvantages to the use of
205 .BR mmap (2):
206 deallocated space is not placed on the free list
207 for reuse by later allocations;
208 memory may be wasted because
209 .BR mmap (2)
210 allocations must be page-aligned;
211 and the kernel must perform the expensive task of zeroing out
212 memory allocated via
213 .BR mmap (2).
214 Balancing these factors leads to a default setting of 128*1024 for the
215 .B M_MMAP_THRESHOLD
216 parameter.
218 The lower limit for this parameter is 0.
219 The upper limit is
220 .BR DEFAULT_MMAP_THRESHOLD_MAX :
221 512*1024 on 32-bit systems or
222 .I 4*1024*1024*sizeof(long)
223 on 64-bit systems.
225 .IR Note :
226 Nowadays, glibc uses a dynamic mmap threshold by default.
227 The initial value of the threshold is 128*1024,
228 but when blocks larger than the current threshold and less than or equal to
229 .B DEFAULT_MMAP_THRESHOLD_MAX
230 are freed,
231 the threshold is adjusted upward to the size of the freed block.
232 When dynamic mmap thresholding is in effect,
233 the threshold for trimming the heap is also dynamically adjusted
234 to be twice the dynamic mmap threshold.
235 Dynamic adjustment of the mmap threshold is disabled if any of the
236 .BR M_TRIM_THRESHOLD ,
237 .BR M_TOP_PAD ,
238 .BR M_MMAP_THRESHOLD ,
240 .B M_MMAP_MAX
241 parameters is set.
243 .BR M_MXFAST " (since glibc 2.3)"
244 .\" The following text adapted from comments in the glibc sources:
245 Set the upper limit for memory allocation requests that are satisfied
246 using "fastbins".
247 (The measurement unit for this parameter is bytes.)
248 Fastbins are storage areas that hold deallocated blocks of memory
249 of the same size without merging adjacent free blocks.
250 Subsequent reallocation of blocks of the same size can be handled
251 very quickly by allocating from the fastbin,
252 although memory fragmentation and the overall memory footprint
253 of the program can increase.
255 The default value for this parameter is
256 .I 64*sizeof(size_t)/4
257 (i.e., 64 on 32-bit architectures).
258 The range for this parameter is 0 to
259 .IR 80*sizeof(size_t)/4 .
260 Setting
261 .B M_MXFAST
262 to 0 disables the use of fastbins.
264 .BR M_PERTURB " (since glibc 2.4)"
265 If this parameter is set to a nonzero value,
266 then bytes of allocated memory (other than allocations via
267 .BR calloc (3))
268 are initialized to the complement of the value
269 in the least significant byte of
270 .IR value ,
271 and when allocated memory is released using
272 .BR free (3),
273 the freed bytes are set to the least significant byte of
274 .IR value .
275 This can be useful for detecting errors where programs
276 incorrectly rely on allocated memory being initialized to zero,
277 or reuse values in memory that has already been freed.
279 The default value for this parameter is 0.
281 .B M_TOP_PAD
282 This parameter defines the amount of padding to employ when calling
283 .BR sbrk (2)
284 to modify the program break.
285 (The measurement unit for this parameter is bytes.)
286 This parameter has an effect in the following circumstances:
288 .IP \[bu] 3
289 When the program break is increased, then
290 .B M_TOP_PAD
291 bytes are added to the
292 .BR sbrk (2)
293 request.
294 .IP \[bu]
295 When the heap is trimmed as a consequence of calling
296 .BR free (3)
297 (see the discussion of
298 .BR M_TRIM_THRESHOLD )
299 this much free space is preserved at the top of the heap.
302 In either case,
303 the amount of padding is always rounded to a system page boundary.
305 Modifying
306 .B M_TOP_PAD
307 is a trade-off between increasing the number of system calls
308 (when the parameter is set low)
309 and wasting unused memory at the top of the heap
310 (when the parameter is set high).
312 The default value for this parameter is 128*1024.
313 .\" DEFAULT_TOP_PAD in glibc source
315 .B M_TRIM_THRESHOLD
316 When the amount of contiguous free memory at the top of the heap
317 grows sufficiently large,
318 .BR free (3)
319 employs
320 .BR sbrk (2)
321 to release this memory back to the system.
322 (This can be useful in programs that continue to execute for
323 a long period after freeing a significant amount of memory.)
325 .B M_TRIM_THRESHOLD
326 parameter specifies the minimum size (in bytes) that
327 this block of memory must reach before
328 .BR sbrk (2)
329 is used to trim the heap.
331 The default value for this parameter is 128*1024.
332 Setting
333 .B M_TRIM_THRESHOLD
334 to \-1 disables trimming completely.
336 Modifying
337 .B M_TRIM_THRESHOLD
338 is a trade-off between increasing the number of system calls
339 (when the parameter is set low)
340 and wasting unused memory at the top of the heap
341 (when the parameter is set high).
343 .SS Environment variables
344 A number of environment variables can be defined
345 to modify some of the same parameters as are controlled by
346 .BR mallopt ().
347 Using these variables has the advantage that the source code
348 of the program need not be changed.
349 To be effective, these variables must be defined before the
350 first call to a memory-allocation function.
351 (If the same parameters are adjusted via
352 .BR mallopt (),
353 then the
354 .BR mallopt ()
355 settings take precedence.)
356 For security reasons,
357 these variables are ignored in set-user-ID and set-group-ID programs.
359 The environment variables are as follows
360 (note the trailing underscore at the end of the name of some variables):
362 .B MALLOC_ARENA_MAX
363 Controls the same parameter as
364 .BR mallopt ()
365 .BR M_ARENA_MAX .
367 .B MALLOC_ARENA_TEST
368 Controls the same parameter as
369 .BR mallopt ()
370 .BR M_ARENA_TEST .
372 .B MALLOC_CHECK_
373 This environment variable controls the same parameter as
374 .BR mallopt ()
375 .BR M_CHECK_ACTION .
376 If this variable is set to a nonzero value,
377 then a special implementation of the memory-allocation functions is used.
378 (This is accomplished using the
379 .BR malloc_hook (3)
380 feature.)
381 This implementation performs additional error checking,
382 but is slower
383 .\" On glibc 2.12/x86, a simple malloc()+free() loop is about 70% slower
384 .\" when MALLOC_CHECK_ was set.
385 than the standard set of memory-allocation functions.
386 (This implementation does not detect all possible errors;
387 memory leaks can still occur.)
389 The value assigned to this environment variable should be a single digit,
390 whose meaning is as described for
391 .BR M_CHECK_ACTION .
392 Any characters beyond the initial digit are ignored.
394 For security reasons, the effect of
395 .B MALLOC_CHECK_
396 is disabled by default for set-user-ID and set-group-ID programs.
397 However, if the file
398 .I /etc/suid\-debug
399 exists (the content of the file is irrelevant), then
400 .B MALLOC_CHECK_
401 also has an effect for set-user-ID and set-group-ID programs.
403 .B MALLOC_MMAP_MAX_
404 Controls the same parameter as
405 .BR mallopt ()
406 .BR M_MMAP_MAX .
408 .B MALLOC_MMAP_THRESHOLD_
409 Controls the same parameter as
410 .BR mallopt ()
411 .BR M_MMAP_THRESHOLD .
413 .B MALLOC_PERTURB_
414 Controls the same parameter as
415 .BR mallopt ()
416 .BR M_PERTURB .
418 .B MALLOC_TRIM_THRESHOLD_
419 Controls the same parameter as
420 .BR mallopt ()
421 .BR M_TRIM_THRESHOLD .
423 .B MALLOC_TOP_PAD_
424 Controls the same parameter as
425 .BR mallopt ()
426 .BR M_TOP_PAD .
427 .SH RETURN VALUE
428 On success,
429 .BR mallopt ()
430 returns 1.
431 On error, it returns 0.
432 .SH ERRORS
433 On error,
434 .I errno
436 .I not
437 set.
438 .SH VERSIONS
439 A similar function exists on many System V derivatives,
440 but the range of values for
441 .I param
442 varies across systems.
443 The SVID defined options
444 .BR M_MXFAST ,
445 .BR M_NLBLKS ,
446 .BR M_GRAIN ,
448 .BR M_KEEP ,
449 but only the first of these is implemented in glibc.
450 .SH STANDARDS
451 None.
452 .SH HISTORY
453 glibc 2.0.
454 .SH BUGS
455 Specifying an invalid value for
456 .I param
457 does not generate an error.
459 A calculation error within the glibc implementation means that
460 a call of the form:
461 .\" FIXME . This looks buggy:
462 .\" setting the M_MXFAST limit rounds up:    (s + SIZE_SZ) & ~MALLOC_ALIGN_MASK)
463 .\" malloc requests are rounded up:
464 .\"    (req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK
465 .\" https://www.sourceware.org/bugzilla/show_bug.cgi?id=12129
467 .in +4n
469 mallopt(M_MXFAST, n)
473 does not result in fastbins being employed for all allocations of size up to
474 .IR n .
475 To ensure desired results,
476 .I n
477 should be rounded up to the next multiple greater than or equal to
478 .IR (2k+1)*sizeof(size_t) ,
479 where
480 .I k
481 is an integer.
482 .\" Bins are multiples of 2 * sizeof(size_t) + sizeof(size_t)
485 .BR mallopt ()
486 is used to set
487 .BR M_PERTURB ,
488 then, as expected, the bytes of allocated memory are initialized
489 to the complement of the byte in
490 .IR value ,
491 and when that memory is freed,
492 the bytes of the region are initialized to the byte specified in
493 .IR value .
494 However, there is an
495 .RI off-by- sizeof(size_t)
496 error in the implementation:
497 .\" FIXME . https://www.sourceware.org/bugzilla/show_bug.cgi?id=12140
498 instead of initializing precisely the block of memory
499 being freed by the call
500 .IR free(p) ,
501 the block starting at
502 .I p+sizeof(size_t)
503 is initialized.
504 .SH EXAMPLES
505 The program below demonstrates the use of
506 .BR M_CHECK_ACTION .
507 If the program is supplied with an (integer) command-line argument,
508 then that argument is used to set the
509 .B M_CHECK_ACTION
510 parameter.
511 The program then allocates a block of memory,
512 and frees it twice (an error).
514 The following shell session shows what happens when we run this program
515 under glibc, with the default value for
516 .BR M_CHECK_ACTION :
518 .in +4n
520 $ \fB./a.out\fP
521 main(): returned from first free() call
522 *** glibc detected *** ./a.out: double free or corruption (top): 0x09d30008 ***
523 ======= Backtrace: =========
524 /lib/libc.so.6(+0x6c501)[0x523501]
525 /lib/libc.so.6(+0x6dd70)[0x524d70]
526 /lib/libc.so.6(cfree+0x6d)[0x527e5d]
527 \&./a.out[0x80485db]
528 /lib/libc.so.6(__libc_start_main+0xe7)[0x4cdce7]
529 \&./a.out[0x8048471]
530 ======= Memory map: ========
531 001e4000\-001fe000 r\-xp 00000000 08:06 1083555    /lib/libgcc_s.so.1
532 001fe000\-001ff000 r\-\-p 00019000 08:06 1083555    /lib/libgcc_s.so.1
533 [some lines omitted]
534 b7814000\-b7817000 rw\-p 00000000 00:00 0
535 bff53000\-bff74000 rw\-p 00000000 00:00 0          [stack]
536 Aborted (core dumped)
540 The following runs show the results when employing other values for
541 .BR M_CHECK_ACTION :
543 .in +4n
545 $ \fB./a.out 1\fP             # Diagnose error and continue
546 main(): returned from first free() call
547 *** glibc detected *** ./a.out: double free or corruption (top): 0x09cbe008 ***
548 main(): returned from second free() call
549 $ \fB./a.out 2\fP             # Abort without error message
550 main(): returned from first free() call
551 Aborted (core dumped)
552 $ \fB./a.out 0\fP             # Ignore error and continue
553 main(): returned from first free() call
554 main(): returned from second free() call
558 The next run shows how to set the same parameter using the
559 .B MALLOC_CHECK_
560 environment variable:
562 .in +4n
564 $ \fBMALLOC_CHECK_=1 ./a.out\fP
565 main(): returned from first free() call
566 *** glibc detected *** ./a.out: free(): invalid pointer: 0x092c2008 ***
567 main(): returned from second free() call
570 .SS Program source
572 .\" SRC BEGIN (mallopt.c)
574 #include <malloc.h>
575 #include <stdio.h>
576 #include <stdlib.h>
579 main(int argc, char *argv[])
581     char *p;
583     if (argc > 1) {
584         if (mallopt(M_CHECK_ACTION, atoi(argv[1])) != 1) {
585             fprintf(stderr, "mallopt() failed");
586             exit(EXIT_FAILURE);
587         }
588     }
590     p = malloc(1000);
591     if (p == NULL) {
592         fprintf(stderr, "malloc() failed");
593         exit(EXIT_FAILURE);
594     }
596     free(p);
597     printf("%s(): returned from first free() call\en", __func__);
599     free(p);
600     printf("%s(): returned from second free() call\en", __func__);
602     exit(EXIT_SUCCESS);
605 .\" SRC END
606 .SH SEE ALSO
607 .ad l
609 .BR mmap (2),
610 .BR sbrk (2),
611 .BR mallinfo (3),
612 .BR malloc (3),
613 .BR malloc_hook (3),
614 .BR malloc_info (3),
615 .BR malloc_stats (3),
616 .BR malloc_trim (3),
617 .BR mcheck (3),
618 .BR mtrace (3),
619 .BR posix_memalign (3)