]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/mallopt.3
pow.3: Minor tweak to BUGS
[thirdparty/man-pages.git] / man3 / mallopt.3
1 '\" t
2 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH MALLOPT 3 2019-03-06 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 mallopt \- set memory allocation parameters
29 .SH SYNOPSIS
30 .B #include <malloc.h>
31 .PP
32 .BI "int mallopt(int " param ", int " value );
33 .SH DESCRIPTION
34 The
35 .BR mallopt ()
36 function adjusts parameters that control the behavior of the
37 memory-allocation functions (see
38 .BR malloc (3)).
39 The
40 .IR param
41 argument specifies the parameter to be modified, and
42 .I value
43 specifies the new value for that parameter.
44 .PP
45 The following values can be specified for
46 .IR param :
47 .TP
48 .BR M_ARENA_MAX
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
52 .BR malloc (3)
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.
59 .IP
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
63 .BR M_ARENA_TEST .
64 .IP
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).
70 .IP
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
74 .B M_ARENA_MAX
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).
78 .TP
79 .BR M_ARENA_TEST
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.
83 (See
84 .B M_ARENA_MAX
85 for the definition of an arena.)
86 .IP
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.
91 .IP
92 The default value for the
93 .B M_ARENA_TEST
94 parameter is 2 on systems where
95 .IR sizeof(long)
96 is 4; otherwise the default value is 8.
97 .IP
98 This parameter has been available since glibc 2.10 via
99 .BR \-\-enable\-experimental\-malloc ,
100 and since glibc 2.15 by default.
101 .IP
102 The value of
103 .B M_ARENA_TEST
104 is not used when
105 .B M_ARENA_MAX
106 has a nonzero value.
107 .TP
108 .BR M_CHECK_ACTION
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:
113 .RS
114 .TP
115 Bit 0
116 If this bit is set, then print a one-line message on
117 .I stderr
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.
124 .TP
125 Bit 1
126 If this bit is set, then,
127 after printing any error message specified by bit 0,
128 the program is terminated by calling
129 .BR abort (3).
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
134 .BR backtrace (3),
135 and prints the process's memory mapping in the style of
136 .IR /proc/[pid]/maps
137 (see
138 .BR proc (5)).
139 .TP
140 Bit 2 (since glibc 2.4)
141 This bit has an effect only if bit 0 is also set.
142 If this bit is 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.
146 .RE
147 .IP
148 The remaining bits in
149 .I value
150 are ignored.
151 .IP
152 Combining the above details,
153 the following numeric values are meaningful for
154 .BR M_CHECK_ACTION :
155 .RS 12
156 .IP 0 3
157 Ignore error conditions; continue execution (with undefined results).
158 .IP 1
159 Print a detailed error message and continue execution.
160 .IP 2
161 Abort the program.
162 .IP 3
163 Print detailed error message, stack trace, and memory mappings,
164 and abort the program.
165 .IP 5
166 Print a simple error message and continue execution.
167 .IP 7
168 Print simple error message, stack trace, and memory mappings,
169 and abort the program.
170 .RE
171 .IP
172 Since glibc 2.3.4, the default value for the
173 .BR M_CHECK_ACTION
174 parameter is 3.
175 In glibc version 2.3.3 and earlier, the default value is 1.
176 .IP
177 Using a nonzero
178 .B M_CHECK_ACTION
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.
181 .TP
182 .BR M_MMAP_MAX
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
186 .BR mmap (2).
187 This parameter exists because some systems have a limited number
188 of internal tables for use by
189 .BR mmap (2),
190 and using more than a few of them may degrade performance.
191 .IP
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
196 .BR mmap (2)
197 for servicing large allocation requests.
198 .TP
199 .BR M_MMAP_THRESHOLD
200 For allocations greater than or equal to the limit specified (in bytes) by
201 .BR M_MMAP_THRESHOLD
202 that can't be satisfied from the free list,
203 the memory-allocation functions employ
204 .BR mmap (2)
205 instead of increasing the program break using
206 .BR sbrk (2).
207 .IP
208 Allocating memory using
209 .BR mmap (2)
210 has the significant advantage that the allocated memory blocks
211 can always be independently released back to the system.
212 (By contrast,
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
215 .BR mmap (2):
216 deallocated space is not placed on the free list
217 for reuse by later allocations;
218 memory may be wasted because
219 .BR mmap (2)
220 allocations must be page-aligned;
221 and the kernel must perform the expensive task of zeroing out
222 memory allocated via
223 .BR mmap (2).
224 Balancing these factors leads to a default setting of 128*1024 for the
225 .BR M_MMAP_THRESHOLD
226 parameter.
227 .IP
228 The lower limit for this parameter is 0.
229 The upper limit is
230 .BR DEFAULT_MMAP_THRESHOLD_MAX :
231 512*1024 on 32-bit systems or
232 .IR 4*1024*1024*sizeof(long)
233 on 64-bit systems.
234 .IP
235 .IR Note:
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
240 are freed,
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 ,
247 .BR M_TOP_PAD ,
248 .BR M_MMAP_THRESHOLD ,
249 or
250 .BR M_MMAP_MAX
251 parameters is set.
252 .TP
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
256 using "fastbins".
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.
264 .IP
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" .
270 Setting
271 .B M_MXFAST
272 to 0 disables the use of fastbins.
273 .TP
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
277 .BR calloc (3))
278 are initialized to the complement of the value
279 in the least significant byte of
280 .IR value ,
281 and when allocated memory is released using
282 .BR free (3),
283 the freed bytes are set to the least significant byte of
284 .IR value .
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.
288 .IP
289 The default value for this parameter is 0.
290 .TP
291 .BR M_TOP_PAD
292 This parameter defines the amount of padding to employ when calling
293 .BR sbrk (2)
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:
297 .RS
298 .IP * 3
299 When the program break is increased, then
300 .BR M_TOP_PAD
301 bytes are added to the
302 .BR sbrk (2)
303 request.
304 .IP *
305 When the heap is trimmed as a consequence of calling
306 .BR free (3)
307 (see the discussion of
308 .BR M_TRIM_THRESHOLD )
309 this much free space is preserved at the top of the heap.
310 .RE
311 .IP
312 In either case,
313 the amount of padding is always rounded to a system page boundary.
314 .IP
315 Modifying
316 .BR M_TOP_PAD
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).
321 .IP
322 The default value for this parameter is 128*1024.
323 .\" DEFAULT_TOP_PAD in glibc source
324 .TP
325 .BR M_TRIM_THRESHOLD
326 When the amount of contiguous free memory at the top of the heap
327 grows sufficiently large,
328 .BR free (3)
329 employs
330 .BR sbrk (2)
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.)
334 The
335 .BR M_TRIM_THRESHOLD
336 parameter specifies the minimum size (in bytes) that
337 this block of memory must reach before
338 .BR sbrk (2)
339 is used to trim the heap.
340 .IP
341 The default value for this parameter is 128*1024.
342 Setting
343 .BR M_TRIM_THRESHOLD
344 to \-1 disables trimming completely.
345 .IP
346 Modifying
347 .BR M_TRIM_THRESHOLD
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).
352 .\"
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
356 .BR mallopt ().
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
362 .BR mallopt (),
363 then the
364 .BR mallopt ()
365 settings take precedence.)
366 For security reasons,
367 these variables are ignored in set-user-ID and set-group-ID programs.
368 .PP
369 The environment variables are as follows
370 (note the trailing underscore at the end of the name of some variables):
371 .TP
372 .BR MALLOC_ARENA_MAX
373 Controls the same parameter as
374 .BR mallopt ()
375 .BR M_ARENA_MAX .
376 .TP
377 .BR MALLOC_ARENA_TEST
378 Controls the same parameter as
379 .BR mallopt ()
380 .BR M_ARENA_TEST .
381 .TP
382 .BR MALLOC_CHECK_
383 This environment variable controls the same parameter as
384 .BR mallopt ()
385 .BR M_CHECK_ACTION .
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
389 .BR malloc_hook (3)
390 feature.)
391 This implementation performs additional error checking,
392 but is slower
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.)
398 .IP
399 The value assigned to this environment variable should be a single digit,
400 whose meaning is as described for
401 .BR M_CHECK_ACTION .
402 Any characters beyond the initial digit are ignored.
403 .IP
404 For security reasons, the effect of
405 .BR MALLOC_CHECK_
406 is disabled by default for set-user-ID and set-group-ID programs.
407 However, if the file
408 .IR /etc/suid\-debug
409 exists (the content of the file is irrelevant), then
410 .BR MALLOC_CHECK_
411 also has an effect for set-user-ID and set-group-ID programs.
412 .TP
413 .BR MALLOC_MMAP_MAX_
414 Controls the same parameter as
415 .BR mallopt ()
416 .BR M_MMAP_MAX .
417 .TP
418 .BR MALLOC_MMAP_THRESHOLD_
419 Controls the same parameter as
420 .BR mallopt ()
421 .BR M_MMAP_THRESHOLD .
422 .TP
423 .BR MALLOC_PERTURB_
424 Controls the same parameter as
425 .BR mallopt ()
426 .BR M_PERTURB .
427 .TP
428 .BR MALLOC_TRIM_THRESHOLD_
429 Controls the same parameter as
430 .BR mallopt ()
431 .BR M_TRIM_THRESHOLD .
432 .TP
433 .BR MALLOC_TOP_PAD_
434 Controls the same parameter as
435 .BR mallopt ()
436 .BR M_TOP_PAD .
437 .SH RETURN VALUE
438 On success,
439 .BR mallopt ()
440 returns 1.
441 On error, it returns 0.
442 .SH ERRORS
443 On error,
444 .I errno
445 is
446 .I not
447 set.
448 .\" .SH VERSIONS
449 .\" Available already in glibc 2.0, possibly earlier
450 .SH CONFORMING TO
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
454 .IR param
455 varies across systems.
456 The SVID defined options
457 .BR M_MXFAST ,
458 .BR M_NLBLKS ,
459 .BR M_GRAIN ,
460 and
461 .BR M_KEEP ,
462 but only the first of these is implemented in glibc.
463 .\" .SH NOTES
464 .SH BUGS
465 Specifying an invalid value for
466 .I param
467 does not generate an error.
468 .PP
469 A calculation error within the glibc implementation means that
470 a call of the form:
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
476 .PP
477 .in +4n
478 .EX
479 mallopt(M_MXFAST, n)
480 .EE
481 .in
482 .PP
483 does not result in fastbins being employed for all allocations of size up to
484 .IR n .
485 To ensure desired results,
486 .I n
487 should be rounded up to the next multiple greater than or equal to
488 .IR (2k+1)*sizeof(size_t) ,
489 where
490 .I k
491 is an integer.
492 .\" Bins are multiples of 2 * sizeof(size_t) + sizeof(size_t)
493 .PP
494 If
495 .BR mallopt ()
496 is used to set
497 .BR M_PERTURB ,
498 then, as expected, the bytes of allocated memory are initialized
499 to the complement of the byte in
500 .IR value ,
501 and when that memory is freed,
502 the bytes of the region are initialized to the byte specified in
503 .IR value .
504 However, there is an
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
510 .IR free(p) ,
511 the block starting at
512 .I p+sizeof(size_t)
513 is initialized.
514 .SH EXAMPLE
515 The program below demonstrates the use of
516 .BR M_CHECK_ACTION .
517 If the program is supplied with an (integer) command-line argument,
518 then that argument is used to set the
519 .BR M_CHECK_ACTION
520 parameter.
521 The program then allocates a block of memory,
522 and frees it twice (an error).
523 .PP
524 The following shell session shows what happens when we run this program
525 under glibc, with the default value for
526 .BR M_CHECK_ACTION :
527 .PP
528 .in +4n
529 .EX
530 $ \fB./a.out\fP
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]
537 \&./a.out[0x80485db]
538 /lib/libc.so.6(__libc_start_main+0xe7)[0x4cdce7]
539 \&./a.out[0x8048471]
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
543 [some lines omitted]
544 b7814000\-b7817000 rw\-p 00000000 00:00 0
545 bff53000\-bff74000 rw\-p 00000000 00:00 0 [stack]
546 Aborted (core dumped)
547 .EE
548 .in
549 .PP
550 The following runs show the results when employing other values for
551 .BR M_CHECK_ACTION :
552 .PP
553 .in +4n
554 .EX
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
565 .EE
566 .in
567 .PP
568 The next run shows how to set the same parameter using the
569 .B MALLOC_CHECK_
570 environment variable:
571 .PP
572 .in +4n
573 .EX
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
578 .EE
579 .in
580 .SS Program source
581 \&
582 .EX
583 #include <malloc.h>
584 #include <stdio.h>
585 #include <stdlib.h>
586
587 int
588 main(int argc, char *argv[])
589 {
590 char *p;
591
592 if (argc > 1) {
593 if (mallopt(M_CHECK_ACTION, atoi(argv[1])) != 1) {
594 fprintf(stderr, "mallopt() failed");
595 exit(EXIT_FAILURE);
596 }
597 }
598
599 p = malloc(1000);
600 if (p == NULL) {
601 fprintf(stderr, "malloc() failed");
602 exit(EXIT_FAILURE);
603 }
604
605 free(p);
606 printf("main(): returned from first free() call\en");
607
608 free(p);
609 printf("main(): returned from second free() call\en");
610
611 exit(EXIT_SUCCESS);
612 }
613 .EE
614 .SH SEE ALSO
615 .ad l
616 .nh
617 .BR mmap (2),
618 .BR sbrk (2),
619 .BR mallinfo (3),
620 .BR malloc (3),
621 .BR malloc_hook (3),
622 .BR malloc_info (3),
623 .BR malloc_stats (3),
624 .BR malloc_trim (3),
625 .BR mcheck (3),
626 .BR mtrace (3),
627 .BR posix_memalign (3)