mount_setattr.2: ffix
[man-pages.git] / man3 / mallopt.3
blobf210e55207e1dc80aa1dd53041bea5bc7cb3de9e
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH MALLOPT 3  2021-03-22 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 mallopt \- set memory allocation parameters
28 .SH SYNOPSIS
29 .nf
30 .B #include <malloc.h>
31 .PP
32 .BI "int mallopt(int " param ", int " value );
33 .fi
34 .SH DESCRIPTION
35 The
36 .BR mallopt ()
37 function adjusts parameters that control the behavior of the
38 memory-allocation functions (see
39 .BR malloc (3)).
40 The
41 .IR param
42 argument specifies the parameter to be modified, and
43 .I value
44 specifies the new value for that parameter.
45 .PP
46 The following values can be specified for
47 .IR param :
48 .TP
49 .BR M_ARENA_MAX
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
53 .BR malloc (3)
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.
60 .IP
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
64 .BR M_ARENA_TEST .
65 .IP
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).
71 .IP
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
75 .B M_ARENA_MAX
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).
79 .TP
80 .BR M_ARENA_TEST
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.
84 (See
85 .B M_ARENA_MAX
86 for the definition of an arena.)
87 .IP
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.
92 .IP
93 The default value for the
94 .B M_ARENA_TEST
95 parameter is 2 on systems where
96 .IR sizeof(long)
97 is 4; otherwise the default value is 8.
98 .IP
99 This parameter has been available since glibc 2.10 via
100 .BR \-\-enable\-experimental\-malloc ,
101 and since glibc 2.15 by default.
103 The value of
104 .B M_ARENA_TEST
105 is not used when
106 .B M_ARENA_MAX
107 has a nonzero value.
109 .BR M_CHECK_ACTION
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:
116 Bit 0
117 If this bit is set, then print a one-line message on
118 .I stderr
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.
126 Bit 1
127 If this bit is set, then,
128 after printing any error message specified by bit 0,
129 the program is terminated by calling
130 .BR abort (3).
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
135 .BR backtrace (3),
136 and prints the process's memory mapping in the style of
137 .IR /proc/[pid]/maps
138 (see
139 .BR proc (5)).
141 Bit 2 (since glibc 2.4)
142 This bit has an effect only if bit 0 is also set.
143 If this bit is 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
150 .I value
151 are ignored.
153 Combining the above details,
154 the following numeric values are meaningful for
155 .BR M_CHECK_ACTION :
156 .RS 12
157 .IP 0 3
158 Ignore error conditions; continue execution (with undefined results).
159 .IP 1
160 Print a detailed error message and continue execution.
161 .IP 2
162 Abort the program.
163 .IP 3
164 Print detailed error message, stack trace, and memory mappings,
165 and abort the program.
166 .IP 5
167 Print a simple error message and continue execution.
168 .IP 7
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
174 .BR M_CHECK_ACTION
175 parameter is 3.
176 In glibc version 2.3.3 and earlier, the default value is 1.
178 Using a nonzero
179 .B M_CHECK_ACTION
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.
183 .BR M_MMAP_MAX
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
187 .BR mmap (2).
188 This parameter exists because some systems have a limited number
189 of internal tables for use by
190 .BR mmap (2),
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
197 .BR mmap (2)
198 for servicing large allocation requests.
200 .BR M_MMAP_THRESHOLD
201 For allocations greater than or equal to the limit specified (in bytes) by
202 .BR M_MMAP_THRESHOLD
203 that can't be satisfied from the free list,
204 the memory-allocation functions employ
205 .BR mmap (2)
206 instead of increasing the program break using
207 .BR sbrk (2).
209 Allocating memory using
210 .BR mmap (2)
211 has the significant advantage that the allocated memory blocks
212 can always be independently released back to the system.
213 (By contrast,
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
216 .BR mmap (2):
217 deallocated space is not placed on the free list
218 for reuse by later allocations;
219 memory may be wasted because
220 .BR mmap (2)
221 allocations must be page-aligned;
222 and the kernel must perform the expensive task of zeroing out
223 memory allocated via
224 .BR mmap (2).
225 Balancing these factors leads to a default setting of 128*1024 for the
226 .BR M_MMAP_THRESHOLD
227 parameter.
229 The lower limit for this parameter is 0.
230 The upper limit is
231 .BR DEFAULT_MMAP_THRESHOLD_MAX :
232 512*1024 on 32-bit systems or
233 .IR 4*1024*1024*sizeof(long)
234 on 64-bit systems.
236 .IR Note:
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
241 are freed,
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 ,
248 .BR M_TOP_PAD ,
249 .BR M_MMAP_THRESHOLD ,
251 .BR M_MMAP_MAX
252 parameters is set.
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
257 using "fastbins".
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" .
271 Setting
272 .B M_MXFAST
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
278 .BR calloc (3))
279 are initialized to the complement of the value
280 in the least significant byte of
281 .IR value ,
282 and when allocated memory is released using
283 .BR free (3),
284 the freed bytes are set to the least significant byte of
285 .IR value .
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.
292 .BR M_TOP_PAD
293 This parameter defines the amount of padding to employ when calling
294 .BR sbrk (2)
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:
299 .IP * 3
300 When the program break is increased, then
301 .BR M_TOP_PAD
302 bytes are added to the
303 .BR sbrk (2)
304 request.
305 .IP *
306 When the heap is trimmed as a consequence of calling
307 .BR free (3)
308 (see the discussion of
309 .BR M_TRIM_THRESHOLD )
310 this much free space is preserved at the top of the heap.
313 In either case,
314 the amount of padding is always rounded to a system page boundary.
316 Modifying
317 .BR M_TOP_PAD
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
326 .BR M_TRIM_THRESHOLD
327 When the amount of contiguous free memory at the top of the heap
328 grows sufficiently large,
329 .BR free (3)
330 employs
331 .BR sbrk (2)
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.)
336 .BR M_TRIM_THRESHOLD
337 parameter specifies the minimum size (in bytes) that
338 this block of memory must reach before
339 .BR sbrk (2)
340 is used to trim the heap.
342 The default value for this parameter is 128*1024.
343 Setting
344 .BR M_TRIM_THRESHOLD
345 to \-1 disables trimming completely.
347 Modifying
348 .BR M_TRIM_THRESHOLD
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
357 .BR mallopt ().
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
363 .BR mallopt (),
364 then the
365 .BR mallopt ()
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):
373 .BR MALLOC_ARENA_MAX
374 Controls the same parameter as
375 .BR mallopt ()
376 .BR M_ARENA_MAX .
378 .BR MALLOC_ARENA_TEST
379 Controls the same parameter as
380 .BR mallopt ()
381 .BR M_ARENA_TEST .
383 .BR MALLOC_CHECK_
384 This environment variable controls the same parameter as
385 .BR mallopt ()
386 .BR M_CHECK_ACTION .
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
390 .BR malloc_hook (3)
391 feature.)
392 This implementation performs additional error checking,
393 but is slower
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
402 .BR M_CHECK_ACTION .
403 Any characters beyond the initial digit are ignored.
405 For security reasons, the effect of
406 .BR MALLOC_CHECK_
407 is disabled by default for set-user-ID and set-group-ID programs.
408 However, if the file
409 .IR /etc/suid\-debug
410 exists (the content of the file is irrelevant), then
411 .BR MALLOC_CHECK_
412 also has an effect for set-user-ID and set-group-ID programs.
414 .BR MALLOC_MMAP_MAX_
415 Controls the same parameter as
416 .BR mallopt ()
417 .BR M_MMAP_MAX .
419 .BR MALLOC_MMAP_THRESHOLD_
420 Controls the same parameter as
421 .BR mallopt ()
422 .BR M_MMAP_THRESHOLD .
424 .BR MALLOC_PERTURB_
425 Controls the same parameter as
426 .BR mallopt ()
427 .BR M_PERTURB .
429 .BR MALLOC_TRIM_THRESHOLD_
430 Controls the same parameter as
431 .BR mallopt ()
432 .BR M_TRIM_THRESHOLD .
434 .BR MALLOC_TOP_PAD_
435 Controls the same parameter as
436 .BR mallopt ()
437 .BR M_TOP_PAD .
438 .SH RETURN VALUE
439 On success,
440 .BR mallopt ()
441 returns 1.
442 On error, it returns 0.
443 .SH ERRORS
444 On error,
445 .I errno
447 .I not
448 set.
449 .\" .SH VERSIONS
450 .\" Available already in glibc 2.0, possibly earlier
451 .SH CONFORMING TO
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
455 .IR param
456 varies across systems.
457 The SVID defined options
458 .BR M_MXFAST ,
459 .BR M_NLBLKS ,
460 .BR M_GRAIN ,
462 .BR M_KEEP ,
463 but only the first of these is implemented in glibc.
464 .\" .SH NOTES
465 .SH BUGS
466 Specifying an invalid value for
467 .I param
468 does not generate an error.
470 A calculation error within the glibc implementation means that
471 a call of the form:
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
478 .in +4n
480 mallopt(M_MXFAST, n)
484 does not result in fastbins being employed for all allocations of size up to
485 .IR n .
486 To ensure desired results,
487 .I n
488 should be rounded up to the next multiple greater than or equal to
489 .IR (2k+1)*sizeof(size_t) ,
490 where
491 .I k
492 is an integer.
493 .\" Bins are multiples of 2 * sizeof(size_t) + sizeof(size_t)
496 .BR mallopt ()
497 is used to set
498 .BR M_PERTURB ,
499 then, as expected, the bytes of allocated memory are initialized
500 to the complement of the byte in
501 .IR value ,
502 and when that memory is freed,
503 the bytes of the region are initialized to the byte specified in
504 .IR value .
505 However, there is an
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
511 .IR free(p) ,
512 the block starting at
513 .I p+sizeof(size_t)
514 is initialized.
515 .SH EXAMPLES
516 The program below demonstrates the use of
517 .BR M_CHECK_ACTION .
518 If the program is supplied with an (integer) command-line argument,
519 then that argument is used to set the
520 .BR M_CHECK_ACTION
521 parameter.
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
527 .BR M_CHECK_ACTION :
529 .in +4n
531 $ \fB./a.out\fP
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]
538 \&./a.out[0x80485db]
539 /lib/libc.so.6(__libc_start_main+0xe7)[0x4cdce7]
540 \&./a.out[0x8048471]
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
544 [some lines omitted]
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
552 .BR M_CHECK_ACTION :
554 .in +4n
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
570 .B MALLOC_CHECK_
571 environment variable:
573 .in +4n
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
581 .SS Program source
584 #include <malloc.h>
585 #include <stdio.h>
586 #include <stdlib.h>
589 main(int argc, char *argv[])
591     char *p;
593     if (argc > 1) {
594         if (mallopt(M_CHECK_ACTION, atoi(argv[1])) != 1) {
595             fprintf(stderr, "mallopt() failed");
596             exit(EXIT_FAILURE);
597         }
598     }
600     p = malloc(1000);
601     if (p == NULL) {
602         fprintf(stderr, "malloc() failed");
603         exit(EXIT_FAILURE);
604     }
606     free(p);
607     printf("main(): returned from first free() call\en");
609     free(p);
610     printf("main(): returned from second free() call\en");
612     exit(EXIT_SUCCESS);
615 .SH SEE ALSO
616 .ad l
618 .BR mmap (2),
619 .BR sbrk (2),
620 .BR mallinfo (3),
621 .BR malloc (3),
622 .BR malloc_hook (3),
623 .BR malloc_info (3),
624 .BR malloc_stats (3),
625 .BR malloc_trim (3),
626 .BR mcheck (3),
627 .BR mtrace (3),
628 .BR posix_memalign (3)