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