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