]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/mallinfo.3
mount_namespaces.7: Minor fix: remove a crufty sentence
[thirdparty/man-pages.git] / man3 / mallinfo.3
CommitLineData
62d874a0 1'\" t
3c81c5a4 2.\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
62d874a0 3.\"
93015253 4.\" %%%LICENSE_START(VERBATIM)
62d874a0
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
62d874a0 25.\"
9ba01802 26.TH MALLINFO 3 2019-03-06 "Linux" "Linux Programmer's Manual"
62d874a0
MK
27.SH NAME
28mallinfo \- obtain memory allocation information
29.SH SYNOPSIS
30.B #include <malloc.h>
f90f031e 31.PP
62d874a0
MK
32.B struct mallinfo mallinfo(void);
33.SH DESCRIPTION
34The
35.BR mallinfo ()
36function returns a copy of a structure containing information about
37memory allocations performed by
38.BR malloc (3)
39and related functions.
40This structure is defined as follows:
41.PP
42.in +4n
b8302363 43.EX
62d874a0
MK
44struct mallinfo {
45 int arena; /* Non-mmapped space allocated (bytes) */
46 int ordblks; /* Number of free chunks */
47 int smblks; /* Number of free fastbin blocks */
48 int hblks; /* Number of mmapped regions */
49 int hblkhd; /* Space allocated in mmapped regions (bytes) */
50 int usmblks; /* Maximum total allocated space (bytes) */
51 int fsmblks; /* Space in freed fastbin blocks (bytes) */
52 int uordblks; /* Total allocated space (bytes) */
53 int fordblks; /* Total free space (bytes) */
54 int keepcost; /* Top-most, releasable space (bytes) */
55};
b8302363 56.EE
62d874a0
MK
57.in
58.PP
59The fields of the
60.I mallinfo
61structure contain the following information:
62d874a0
MK
62.TP 10
63.I arena
64The total amount of memory allocated by means other than
65.BR mmap (2)
66(i.e., memory allocated on the heap).
67This figure includes both in-use blocks and blocks on the free list.
68.TP
69.I ordblks
70The number of ordinary (i.e., non-fastbin) free blocks.
71.TP
72.I smblks
73The number of fastbin free blocks (see
74.BR mallopt (3)).
75.TP
76.I hblks
77The number of blocks currently allocated using
78.BR mmap (2).
35af1345
MK
79(See the discussion of
80.B M_MMAP_THRESHOLD
81in
82.BR mallopt (3).)
62d874a0
MK
83.TP
84.I hblkhd
85The number of bytes in blocks currently allocated using
86.BR mmap (2).
87.TP
88.I usmblks
89The "highwater mark" for allocated space\(emthat is,
90the maximum amount of space that was ever allocated.
91This field is maintained only in nonthreading environments.
92.TP
93.I fsmblks
94The total number of bytes in fastbin free blocks.
95.TP
96.I uordblks
97The total number of bytes used by in-use allocations.
98.TP
99.I fordblks
100The total number of bytes in free blocks.
101.TP
102.I keepcost
103The total amount of releasable free space at the top
104of the heap.
105This is the maximum number of bytes that could ideally
106(i.e., ignoring page alignment restrictions, and so on) be released by
107.BR malloc_trim (3).
108.\" .SH VERSIONS
109.\" Available already in glibc 2.0, possibly earlier
c32cf268
ZL
110.SH ATTRIBUTES
111For an explanation of the terms used in this section, see
112.BR attributes (7).
113.TS
114allbox;
115lb lb lbw28
116l l l.
117Interface Attribute Value
118T{
119.BR mallinfo ()
120T} Thread safety MT-Unsafe init const:mallopt
121.TE
847e0d88 122.sp 1
c32cf268 123.BR mallinfo ()
696bd2da
MK
124would access some global internal objects.
125If modify them with non-atomically,
c32cf268
ZL
126may get inconsistent results.
127The identifier
128.I mallopt
129in
130.I const:mallopt
131mean that
132.BR mallopt ()
3c841730 133would modify the global internal objects with atomics, that make sure
c32cf268
ZL
134.BR mallinfo ()
135is safe enough, others modify with non-atomically maybe not.
62d874a0
MK
136.SH CONFORMING TO
137This function is not specified by POSIX or the C standards.
138A similar function exists on many System V derivatives,
139and was specified in the SVID.
f704bc68 140.SH BUGS
bea08fec 141.\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=208
f704bc68
MK
142.\" See the 24 Aug 2011 mail by Paul Pluzhnikov:
143.\" "[patch] Fix mallinfo() to accumulate results for all arenas"
144.\" on libc-alpha@sourceware.org
3c81c5a4
MK
145.B Information is returned for only the main memory allocation area.
146Allocations in other arenas are excluded.
147See
148.BR malloc_stats (3)
149and
150.BR malloc_info (3)
151for alternatives that include information about other arenas.
847e0d88 152.PP
62d874a0
MK
153The fields of the
154.I mallinfo
155structure are typed as
156.IR int .
157However, because some internal bookkeeping values may be of type
158.IR long ,
159the reported values may wrap around zero and thus be inaccurate.
160.SH EXAMPLE
161The program below employs
162.BR mallinfo ()
163to retrieve memory allocation statistics before and after
164allocating and freeing some blocks of memory.
165The statistics are displayed on standard output.
847e0d88 166.PP
62d874a0
MK
167The first two command-line arguments specify the number and size of
168blocks to be allocated with
169.BR malloc (3).
847e0d88 170.PP
62d874a0
MK
171The remaining three arguments specify which of the allocated blocks
172should be freed with
173.BR free (3).
174These three arguments are optional, and specify (in order):
175the step size to be used in the loop that frees blocks
176(the default is 1, meaning free all blocks in the range);
177the ordinal position of the first block to be freed
178(default 0, meaning the first allocated block);
179and a number one greater than the ordinal position
180of the last block to be freed
181(default is one greater than the maximum block number).
182If these three arguments are omitted,
183then the defaults cause all allocated blocks to be freed.
847e0d88 184.PP
62d874a0
MK
185In the following example run of the program,
1861000 allocations of 100 bytes are performed,
187and then every second allocated block is freed:
188.PP
189.in +4n
b8302363 190.EX
62d874a0
MK
191$ \fB./a.out 1000 100 2\fP
192============== Before allocating blocks ==============
193Total non\-mmapped bytes (arena): 0
194# of free chunks (ordblks): 1
195# of free fastbin blocks (smblks): 0
196# of mapped regions (hblks): 0
197Bytes in mapped regions (hblkhd): 0
198Max. total allocated space (usmblks): 0
199Free bytes held in fastbins (fsmblks): 0
200Total allocated space (uordblks): 0
201Total free space (fordblks): 0
202Topmost releasable block (keepcost): 0
203
204============== After allocating blocks ==============
205Total non\-mmapped bytes (arena): 135168
206# of free chunks (ordblks): 1
207# of free fastbin blocks (smblks): 0
208# of mapped regions (hblks): 0
209Bytes in mapped regions (hblkhd): 0
210Max. total allocated space (usmblks): 0
211Free bytes held in fastbins (fsmblks): 0
212Total allocated space (uordblks): 104000
213Total free space (fordblks): 31168
214Topmost releasable block (keepcost): 31168
215
216============== After freeing blocks ==============
217Total non\-mmapped bytes (arena): 135168
218# of free chunks (ordblks): 501
219# of free fastbin blocks (smblks): 0
220# of mapped regions (hblks): 0
221Bytes in mapped regions (hblkhd): 0
222Max. total allocated space (usmblks): 0
223Free bytes held in fastbins (fsmblks): 0
224Total allocated space (uordblks): 52000
225Total free space (fordblks): 83168
226Topmost releasable block (keepcost): 31168
b8302363 227.EE
62d874a0
MK
228.in
229.SS Program source
230\&
e7d0bb47 231.EX
62d874a0 232#include <malloc.h>
e40be343
JW
233#include <stdlib.h>
234#include <string.h>
62d874a0
MK
235
236static void
237display_mallinfo(void)
238{
239 struct mallinfo mi;
240
241 mi = mallinfo();
242
d1a71985
MK
243 printf("Total non\-mmapped bytes (arena): %d\en", mi.arena);
244 printf("# of free chunks (ordblks): %d\en", mi.ordblks);
245 printf("# of free fastbin blocks (smblks): %d\en", mi.smblks);
246 printf("# of mapped regions (hblks): %d\en", mi.hblks);
247 printf("Bytes in mapped regions (hblkhd): %d\en", mi.hblkhd);
248 printf("Max. total allocated space (usmblks): %d\en", mi.usmblks);
249 printf("Free bytes held in fastbins (fsmblks): %d\en", mi.fsmblks);
250 printf("Total allocated space (uordblks): %d\en", mi.uordblks);
251 printf("Total free space (fordblks): %d\en", mi.fordblks);
252 printf("Topmost releasable block (keepcost): %d\en", mi.keepcost);
62d874a0
MK
253}
254
255int
256main(int argc, char *argv[])
257{
258#define MAX_ALLOCS 2000000
259 char *alloc[MAX_ALLOCS];
260 int numBlocks, j, freeBegin, freeEnd, freeStep;
261 size_t blockSize;
262
e40be343
JW
263 if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) {
264 fprintf(stderr, "%s num\-blocks block\-size [free\-step "
d1a71985 265 "[start\-free [end\-free]]]\en", argv[0]);
e40be343
JW
266 exit(EXIT_FAILURE);
267 }
62d874a0
MK
268
269 numBlocks = atoi(argv[1]);
270 blockSize = atoi(argv[2]);
271 freeStep = (argc > 3) ? atoi(argv[3]) : 1;
272 freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
273 freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
274
d1a71985 275 printf("============== Before allocating blocks ==============\en");
62d874a0
MK
276 display_mallinfo();
277
278 for (j = 0; j < numBlocks; j++) {
e40be343 279 if (numBlocks >= MAX_ALLOCS) {
d1a71985 280 fprintf(stderr, "Too many allocations\en");
e40be343
JW
281 exit(EXIT_FAILURE);
282 }
62d874a0
MK
283
284 alloc[j] = malloc(blockSize);
e40be343
JW
285 if (alloc[j] == NULL) {
286 perror("malloc");
287 exit(EXIT_FAILURE);
288 }
62d874a0
MK
289 }
290
d1a71985 291 printf("\en============== After allocating blocks ==============\en");
62d874a0
MK
292 display_mallinfo();
293
294 for (j = freeBegin; j < freeEnd; j += freeStep)
295 free(alloc[j]);
296
d1a71985 297 printf("\en============== After freeing blocks ==============\en");
62d874a0
MK
298 display_mallinfo();
299
300 exit(EXIT_SUCCESS);
301}
e7d0bb47 302.EE
62d874a0
MK
303.SH SEE ALSO
304.ad l
305.nh
306.BR mmap (2),
307.BR malloc (3),
3c81c5a4 308.BR malloc_info (3),
62d874a0
MK
309.BR malloc_stats (3),
310.BR malloc_trim (3),
311.BR mallopt (3)