]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/mallinfo.3
mallinfo.3: Note mallinfo() bug for multiple arenas
[thirdparty/man-pages.git] / man3 / mallinfo.3
CommitLineData
62d874a0
MK
1'\" t
2.\" Copyright (c) 2010 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.\"
f704bc68 24.TH MALLINFO 3 2012-02-27 "Linux" "Linux Programmer's Manual"
62d874a0
MK
25.SH NAME
26mallinfo \- obtain memory allocation information
27.SH SYNOPSIS
28.B #include <malloc.h>
29
30.B struct mallinfo mallinfo(void);
31.SH DESCRIPTION
32The
33.BR mallinfo ()
34function returns a copy of a structure containing information about
35memory allocations performed by
36.BR malloc (3)
37and related functions.
38This structure is defined as follows:
39.PP
40.in +4n
41.nf
42struct mallinfo {
43 int arena; /* Non-mmapped space allocated (bytes) */
44 int ordblks; /* Number of free chunks */
45 int smblks; /* Number of free fastbin blocks */
46 int hblks; /* Number of mmapped regions */
47 int hblkhd; /* Space allocated in mmapped regions (bytes) */
48 int usmblks; /* Maximum total allocated space (bytes) */
49 int fsmblks; /* Space in freed fastbin blocks (bytes) */
50 int uordblks; /* Total allocated space (bytes) */
51 int fordblks; /* Total free space (bytes) */
52 int keepcost; /* Top-most, releasable space (bytes) */
53};
54.fi
55.in
56.PP
57The fields of the
58.I mallinfo
59structure contain the following information:
60
61.TP 10
62.I arena
63The total amount of memory allocated by means other than
64.BR mmap (2)
65(i.e., memory allocated on the heap).
66This figure includes both in-use blocks and blocks on the free list.
67.TP
68.I ordblks
69The number of ordinary (i.e., non-fastbin) free blocks.
70.TP
71.I smblks
72The number of fastbin free blocks (see
73.BR mallopt (3)).
74.TP
75.I hblks
76The number of blocks currently allocated using
77.BR mmap (2).
78.TP
79.I hblkhd
80The number of bytes in blocks currently allocated using
81.BR mmap (2).
82.TP
83.I usmblks
84The "highwater mark" for allocated space\(emthat is,
85the maximum amount of space that was ever allocated.
86This field is maintained only in nonthreading environments.
87.TP
88.I fsmblks
89The total number of bytes in fastbin free blocks.
90.TP
91.I uordblks
92The total number of bytes used by in-use allocations.
93.TP
94.I fordblks
95The total number of bytes in free blocks.
96.TP
97.I keepcost
98The total amount of releasable free space at the top
99of the heap.
100This is the maximum number of bytes that could ideally
101(i.e., ignoring page alignment restrictions, and so on) be released by
102.BR malloc_trim (3).
103.\" .SH VERSIONS
104.\" Available already in glibc 2.0, possibly earlier
105.SH CONFORMING TO
106This function is not specified by POSIX or the C standards.
107A similar function exists on many System V derivatives,
108and was specified in the SVID.
f704bc68
MK
109.SH BUGS
110.\" See the 24 Aug 2011 mail by Paul Pluzhnikov:
111.\" "[patch] Fix mallinfo() to accumulate results for all arenas"
112.\" on libc-alpha@sourceware.org
62d874a0
MK
113Information is returned only for the main arena
114(the main memory allocation area).
f704bc68 115
62d874a0
MK
116The fields of the
117.I mallinfo
118structure are typed as
119.IR int .
120However, because some internal bookkeeping values may be of type
121.IR long ,
122the reported values may wrap around zero and thus be inaccurate.
123.SH EXAMPLE
124The program below employs
125.BR mallinfo ()
126to retrieve memory allocation statistics before and after
127allocating and freeing some blocks of memory.
128The statistics are displayed on standard output.
129
130The first two command-line arguments specify the number and size of
131blocks to be allocated with
132.BR malloc (3).
133
134The remaining three arguments specify which of the allocated blocks
135should be freed with
136.BR free (3).
137These three arguments are optional, and specify (in order):
138the step size to be used in the loop that frees blocks
139(the default is 1, meaning free all blocks in the range);
140the ordinal position of the first block to be freed
141(default 0, meaning the first allocated block);
142and a number one greater than the ordinal position
143of the last block to be freed
144(default is one greater than the maximum block number).
145If these three arguments are omitted,
146then the defaults cause all allocated blocks to be freed.
147
148In the following example run of the program,
1491000 allocations of 100 bytes are performed,
150and then every second allocated block is freed:
151.PP
152.in +4n
153.nf
154$ \fB./a.out 1000 100 2\fP
155============== Before allocating blocks ==============
156Total non\-mmapped bytes (arena): 0
157# of free chunks (ordblks): 1
158# of free fastbin blocks (smblks): 0
159# of mapped regions (hblks): 0
160Bytes in mapped regions (hblkhd): 0
161Max. total allocated space (usmblks): 0
162Free bytes held in fastbins (fsmblks): 0
163Total allocated space (uordblks): 0
164Total free space (fordblks): 0
165Topmost releasable block (keepcost): 0
166
167============== After allocating blocks ==============
168Total non\-mmapped bytes (arena): 135168
169# of free chunks (ordblks): 1
170# of free fastbin blocks (smblks): 0
171# of mapped regions (hblks): 0
172Bytes in mapped regions (hblkhd): 0
173Max. total allocated space (usmblks): 0
174Free bytes held in fastbins (fsmblks): 0
175Total allocated space (uordblks): 104000
176Total free space (fordblks): 31168
177Topmost releasable block (keepcost): 31168
178
179============== After freeing blocks ==============
180Total non\-mmapped bytes (arena): 135168
181# of free chunks (ordblks): 501
182# of free fastbin blocks (smblks): 0
183# of mapped regions (hblks): 0
184Bytes in mapped regions (hblkhd): 0
185Max. total allocated space (usmblks): 0
186Free bytes held in fastbins (fsmblks): 0
187Total allocated space (uordblks): 52000
188Total free space (fordblks): 83168
189Topmost releasable block (keepcost): 31168
190.fi
191.in
192.SS Program source
193\&
194.nf
195#include <malloc.h>
196#include "tlpi_hdr.h"
197
198static void
199display_mallinfo(void)
200{
201 struct mallinfo mi;
202
203 mi = mallinfo();
204
205 printf("Total non\-mmapped bytes (arena): %d\\n", mi.arena);
206 printf("# of free chunks (ordblks): %d\\n", mi.ordblks);
207 printf("# of free fastbin blocks (smblks): %d\\n", mi.smblks);
208 printf("# of mapped regions (hblks): %d\\n", mi.hblks);
209 printf("Bytes in mapped regions (hblkhd): %d\\n", mi.hblkhd);
210 printf("Max. total allocated space (usmblks): %d\\n", mi.usmblks);
211 printf("Free bytes held in fastbins (fsmblks): %d\\n", mi.fsmblks);
212 printf("Total allocated space (uordblks): %d\\n", mi.uordblks);
213 printf("Total free space (fordblks): %d\\n", mi.fordblks);
214 printf("Topmost releasable block (keepcost): %d\\n", mi.keepcost);
215}
216
217int
218main(int argc, char *argv[])
219{
220#define MAX_ALLOCS 2000000
221 char *alloc[MAX_ALLOCS];
222 int numBlocks, j, freeBegin, freeEnd, freeStep;
223 size_t blockSize;
224
225 if (argc < 3 || strcmp(argv[1], "\-\-help") == 0)
226 usageErr("%s num\-blocks block\-size [free\-step [start\-free "
227 "[end\-free]]]\\n", argv[0]);
228
229 numBlocks = atoi(argv[1]);
230 blockSize = atoi(argv[2]);
231 freeStep = (argc > 3) ? atoi(argv[3]) : 1;
232 freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
233 freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
234
235 printf("============== Before allocating blocks ==============\\n");
236 display_mallinfo();
237
238 for (j = 0; j < numBlocks; j++) {
239 if (numBlocks >= MAX_ALLOCS)
240 fatal("Too many allocations");
241
242 alloc[j] = malloc(blockSize);
243 if (alloc[j] == NULL)
244 errExit("malloc");
245 }
246
247 printf("\\n============== After allocating blocks ==============\\n");
248 display_mallinfo();
249
250 for (j = freeBegin; j < freeEnd; j += freeStep)
251 free(alloc[j]);
252
253 printf("\\n============== After freeing blocks ==============\\n");
254 display_mallinfo();
255
256 exit(EXIT_SUCCESS);
257}
258.fi
259.SH SEE ALSO
260.ad l
261.nh
262.BR mmap (2),
263.BR malloc (3),
264.BR malloc_stats (3),
265.BR malloc_trim (3),
266.BR mallopt (3)