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