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