1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH MALLINFO 3 2021-03-22 "Linux" "Linux Programmer's Manual"
27 mallinfo, mallinfo2 \- obtain memory allocation information
30 .B #include <malloc.h>
32 .B struct mallinfo mallinfo(void);
33 .B struct mallinfo2 mallinfo2(void);
36 These functions return a copy of a structure containing information about
37 memory allocations performed by
39 and related functions.
40 The structure returned by each function contains the same fields.
41 However, the older function,
43 is deprecated since the type used for the fields is too small (see BUGS).
45 Note that not all allocations are visible to these functions;
46 see BUGS and consider using
54 is defined as follows:
59 size_t arena; /* Non\-mmapped space allocated (bytes) */
60 size_t ordblks; /* Number of free chunks */
61 size_t smblks; /* Number of free fastbin blocks */
62 size_t hblks; /* Number of mmapped regions */
63 size_t hblkhd; /* Space allocated in mmapped regions
65 size_t usmblks; /* See below */
66 size_t fsmblks; /* Space in freed fastbin blocks (bytes) */
67 size_t uordblks; /* Total allocated space (bytes) */
68 size_t fordblks; /* Total free space (bytes) */
69 size_t keepcost; /* Top\-most, releasable space (bytes) */
76 structure returned by the deprecated
78 function is exactly the same, except that the fields are typed as
81 The structure fields contain the following information:
84 The total amount of memory allocated by means other than
86 (i.e., memory allocated on the heap).
87 This figure includes both in-use blocks and blocks on the free list.
90 The number of ordinary (i.e., non-fastbin) free blocks.
93 .\" the glibc info page wrongly says this field is unused
94 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=26746
95 The number of fastbin free blocks (see
99 The number of blocks currently allocated using
101 (See the discussion of
107 The number of bytes in blocks currently allocated using
111 This field is unused, and is always 0.
112 .\" It seems to have been zero since at least as far back as glibc 2.15
113 Historically, it was the "highwater mark" for allocated space\(emthat is,
114 the maximum amount of space that was ever allocated (in bytes);
115 this field was maintained only in nonthreading environments.
118 .\" the glibc info page wrongly says this field is unused
119 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=26746
120 The total number of bytes in fastbin free blocks.
123 The total number of bytes used by in-use allocations.
126 The total number of bytes in free blocks.
129 The total amount of releasable free space at the top
131 This is the maximum number of bytes that could ideally
132 (i.e., ignoring page alignment restrictions, and so on) be released by
135 .\" mallinfo(): Available already in glibc 2.0, possibly earlier
139 .\" commit e3960d1c57e57f33e0e846d615788f4ede73b945
142 For an explanation of the terms used in this section, see
150 Interface Attribute Value
155 MT-Unsafe init const:mallopt
163 would access some global internal objects.
164 If modify them with non-atomically,
165 may get inconsistent results.
172 would modify the global internal objects with atomics, that make sure
175 is safe enough, others modify with non-atomically maybe not.
177 These functions are not specified by POSIX or the C standards.
180 function exists on many System V derivatives,
181 and was specified in the SVID.
183 .\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=208
184 .\" See the 24 Aug 2011 mail by Paul Pluzhnikov:
185 .\" "[patch] Fix mallinfo() to accumulate results for all arenas"
186 .\" on libc-alpha@sourceware.org
187 .B Information is returned for only the main memory allocation area.
188 Allocations in other arenas are excluded.
193 for alternatives that include information about other arenas.
197 structure that is returned by the older
199 function are typed as
201 However, because some internal bookkeeping values may be of type
203 the reported values may wrap around zero and thus be inaccurate.
205 The program below employs
207 to retrieve memory allocation statistics before and after
208 allocating and freeing some blocks of memory.
209 The statistics are displayed on standard output.
211 The first two command-line arguments specify the number and size of
212 blocks to be allocated with
215 The remaining three arguments specify which of the allocated blocks
218 These three arguments are optional, and specify (in order):
219 the step size to be used in the loop that frees blocks
220 (the default is 1, meaning free all blocks in the range);
221 the ordinal position of the first block to be freed
222 (default 0, meaning the first allocated block);
223 and a number one greater than the ordinal position
224 of the last block to be freed
225 (default is one greater than the maximum block number).
226 If these three arguments are omitted,
227 then the defaults cause all allocated blocks to be freed.
229 In the following example run of the program,
230 1000 allocations of 100 bytes are performed,
231 and then every second allocated block is freed:
235 $ \fB./a.out 1000 100 2\fP
236 ============== Before allocating blocks ==============
237 Total non\-mmapped bytes (arena): 0
238 # of free chunks (ordblks): 1
239 # of free fastbin blocks (smblks): 0
240 # of mapped regions (hblks): 0
241 Bytes in mapped regions (hblkhd): 0
242 Max. total allocated space (usmblks): 0
243 Free bytes held in fastbins (fsmblks): 0
244 Total allocated space (uordblks): 0
245 Total free space (fordblks): 0
246 Topmost releasable block (keepcost): 0
248 ============== After allocating blocks ==============
249 Total non\-mmapped bytes (arena): 135168
250 # of free chunks (ordblks): 1
251 # of free fastbin blocks (smblks): 0
252 # of mapped regions (hblks): 0
253 Bytes in mapped regions (hblkhd): 0
254 Max. total allocated space (usmblks): 0
255 Free bytes held in fastbins (fsmblks): 0
256 Total allocated space (uordblks): 104000
257 Total free space (fordblks): 31168
258 Topmost releasable block (keepcost): 31168
260 ============== After freeing blocks ==============
261 Total non\-mmapped bytes (arena): 135168
262 # of free chunks (ordblks): 501
263 # of free fastbin blocks (smblks): 0
264 # of mapped regions (hblks): 0
265 Bytes in mapped regions (hblkhd): 0
266 Max. total allocated space (usmblks): 0
267 Free bytes held in fastbins (fsmblks): 0
268 Total allocated space (uordblks): 52000
269 Total free space (fordblks): 83168
270 Topmost releasable block (keepcost): 31168
281 display_mallinfo2(void)
287 printf("Total non\-mmapped bytes (arena): %zu\en", mi.arena);
288 printf("# of free chunks (ordblks): %zu\en", mi.ordblks);
289 printf("# of free fastbin blocks (smblks): %zu\en", mi.smblks);
290 printf("# of mapped regions (hblks): %zu\en", mi.hblks);
291 printf("Bytes in mapped regions (hblkhd): %zu\en", mi.hblkhd);
292 printf("Max. total allocated space (usmblks): %zu\en", mi.usmblks);
293 printf("Free bytes held in fastbins (fsmblks): %zu\en", mi.fsmblks);
294 printf("Total allocated space (uordblks): %zu\en", mi.uordblks);
295 printf("Total free space (fordblks): %zu\en", mi.fordblks);
296 printf("Topmost releasable block (keepcost): %zu\en", mi.keepcost);
300 main(int argc, char *argv[])
302 #define MAX_ALLOCS 2000000
303 char *alloc[MAX_ALLOCS];
304 int numBlocks, freeBegin, freeEnd, freeStep;
307 if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) {
308 fprintf(stderr, "%s num\-blocks block\-size [free\-step "
309 "[start\-free [end\-free]]]\en", argv[0]);
313 numBlocks = atoi(argv[1]);
314 blockSize = atoi(argv[2]);
315 freeStep = (argc > 3) ? atoi(argv[3]) : 1;
316 freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
317 freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
319 printf("============== Before allocating blocks ==============\en");
322 for (int j = 0; j < numBlocks; j++) {
323 if (numBlocks >= MAX_ALLOCS) {
324 fprintf(stderr, "Too many allocations\en");
328 alloc[j] = malloc(blockSize);
329 if (alloc[j] == NULL) {
335 printf("\en============== After allocating blocks ==============\en");
338 for (int j = freeBegin; j < freeEnd; j += freeStep)
341 printf("\en============== After freeing blocks ==============\en");
353 .BR malloc_stats (3),