1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH MALLINFO 3 2021-03-22 "Linux man-pages (unreleased)"
7 mallinfo, mallinfo2 \- obtain memory allocation information
10 .RI ( libc ", " \-lc )
13 .B #include <malloc.h>
15 .B struct mallinfo mallinfo(void);
16 .B struct mallinfo2 mallinfo2(void);
19 These functions return a copy of a structure containing information about
20 memory allocations performed by
22 and related functions.
23 The structure returned by each function contains the same fields.
24 However, the older function,
26 is deprecated since the type used for the fields is too small (see BUGS).
28 Note that not all allocations are visible to these functions;
29 see BUGS and consider using
37 is defined as follows:
42 size_t arena; /* Non\-mmapped space allocated (bytes) */
43 size_t ordblks; /* Number of free chunks */
44 size_t smblks; /* Number of free fastbin blocks */
45 size_t hblks; /* Number of mmapped regions */
46 size_t hblkhd; /* Space allocated in mmapped regions
48 size_t usmblks; /* See below */
49 size_t fsmblks; /* Space in freed fastbin blocks (bytes) */
50 size_t uordblks; /* Total allocated space (bytes) */
51 size_t fordblks; /* Total free space (bytes) */
52 size_t keepcost; /* Top\-most, releasable space (bytes) */
59 structure returned by the deprecated
61 function is exactly the same, except that the fields are typed as
64 The structure fields contain the following information:
67 The total amount of memory allocated by means other than
69 (i.e., memory allocated on the heap).
70 This figure includes both in-use blocks and blocks on the free list.
73 The number of ordinary (i.e., non-fastbin) free blocks.
76 .\" the glibc info page wrongly says this field is unused
77 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=26746
78 The number of fastbin free blocks (see
82 The number of blocks currently allocated using
84 (See the discussion of
90 The number of bytes in blocks currently allocated using
94 This field is unused, and is always 0.
95 .\" It seems to have been zero since at least as far back as glibc 2.15
96 Historically, it was the "highwater mark" for allocated space\(emthat is,
97 the maximum amount of space that was ever allocated (in bytes);
98 this field was maintained only in nonthreading environments.
101 .\" the glibc info page wrongly says this field is unused
102 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=26746
103 The total number of bytes in fastbin free blocks.
106 The total number of bytes used by in-use allocations.
109 The total number of bytes in free blocks.
112 The total amount of releasable free space at the top
114 This is the maximum number of bytes that could ideally
115 (i.e., ignoring page alignment restrictions, and so on) be released by
118 .\" mallinfo(): Available already in glibc 2.0, possibly earlier
122 .\" commit e3960d1c57e57f33e0e846d615788f4ede73b945
125 For an explanation of the terms used in this section, see
133 Interface Attribute Value
138 MT-Unsafe init const:mallopt
146 would access some global internal objects.
147 If modify them with non-atomically,
148 may get inconsistent results.
155 would modify the global internal objects with atomics, that make sure
158 is safe enough, others modify with non-atomically maybe not.
160 These functions are not specified by POSIX or the C standards.
163 function exists on many System V derivatives,
164 and was specified in the SVID.
166 .\" FIXME . http://sourceware.org/bugzilla/show_bug.cgi?id=208
167 .\" See the 24 Aug 2011 mail by Paul Pluzhnikov:
168 .\" "[patch] Fix mallinfo() to accumulate results for all arenas"
169 .\" on libc-alpha@sourceware.org
170 .B Information is returned for only the main memory allocation area.
171 Allocations in other arenas are excluded.
176 for alternatives that include information about other arenas.
180 structure that is returned by the older
182 function are typed as
184 However, because some internal bookkeeping values may be of type
186 the reported values may wrap around zero and thus be inaccurate.
188 The program below employs
190 to retrieve memory allocation statistics before and after
191 allocating and freeing some blocks of memory.
192 The statistics are displayed on standard output.
194 The first two command-line arguments specify the number and size of
195 blocks to be allocated with
198 The remaining three arguments specify which of the allocated blocks
201 These three arguments are optional, and specify (in order):
202 the step size to be used in the loop that frees blocks
203 (the default is 1, meaning free all blocks in the range);
204 the ordinal position of the first block to be freed
205 (default 0, meaning the first allocated block);
206 and a number one greater than the ordinal position
207 of the last block to be freed
208 (default is one greater than the maximum block number).
209 If these three arguments are omitted,
210 then the defaults cause all allocated blocks to be freed.
212 In the following example run of the program,
213 1000 allocations of 100 bytes are performed,
214 and then every second allocated block is freed:
218 $ \fB./a.out 1000 100 2\fP
219 ============== Before allocating blocks ==============
220 Total non\-mmapped bytes (arena): 0
221 # of free chunks (ordblks): 1
222 # of free fastbin blocks (smblks): 0
223 # of mapped regions (hblks): 0
224 Bytes in mapped regions (hblkhd): 0
225 Max. total allocated space (usmblks): 0
226 Free bytes held in fastbins (fsmblks): 0
227 Total allocated space (uordblks): 0
228 Total free space (fordblks): 0
229 Topmost releasable block (keepcost): 0
231 ============== After allocating blocks ==============
232 Total non\-mmapped bytes (arena): 135168
233 # of free chunks (ordblks): 1
234 # of free fastbin blocks (smblks): 0
235 # of mapped regions (hblks): 0
236 Bytes in mapped regions (hblkhd): 0
237 Max. total allocated space (usmblks): 0
238 Free bytes held in fastbins (fsmblks): 0
239 Total allocated space (uordblks): 104000
240 Total free space (fordblks): 31168
241 Topmost releasable block (keepcost): 31168
243 ============== After freeing blocks ==============
244 Total non\-mmapped bytes (arena): 135168
245 # of free chunks (ordblks): 501
246 # of free fastbin blocks (smblks): 0
247 # of mapped regions (hblks): 0
248 Bytes in mapped regions (hblkhd): 0
249 Max. total allocated space (usmblks): 0
250 Free bytes held in fastbins (fsmblks): 0
251 Total allocated space (uordblks): 52000
252 Total free space (fordblks): 83168
253 Topmost releasable block (keepcost): 31168
264 display_mallinfo2(void)
270 printf("Total non\-mmapped bytes (arena): %zu\en", mi.arena);
271 printf("# of free chunks (ordblks): %zu\en", mi.ordblks);
272 printf("# of free fastbin blocks (smblks): %zu\en", mi.smblks);
273 printf("# of mapped regions (hblks): %zu\en", mi.hblks);
274 printf("Bytes in mapped regions (hblkhd): %zu\en", mi.hblkhd);
275 printf("Max. total allocated space (usmblks): %zu\en", mi.usmblks);
276 printf("Free bytes held in fastbins (fsmblks): %zu\en", mi.fsmblks);
277 printf("Total allocated space (uordblks): %zu\en", mi.uordblks);
278 printf("Total free space (fordblks): %zu\en", mi.fordblks);
279 printf("Topmost releasable block (keepcost): %zu\en", mi.keepcost);
283 main(int argc, char *argv[])
285 #define MAX_ALLOCS 2000000
286 char *alloc[MAX_ALLOCS];
287 int numBlocks, freeBegin, freeEnd, freeStep;
290 if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) {
291 fprintf(stderr, "%s num\-blocks block\-size [free\-step "
292 "[start\-free [end\-free]]]\en", argv[0]);
296 numBlocks = atoi(argv[1]);
297 blockSize = atoi(argv[2]);
298 freeStep = (argc > 3) ? atoi(argv[3]) : 1;
299 freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
300 freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
302 printf("============== Before allocating blocks ==============\en");
305 for (int j = 0; j < numBlocks; j++) {
306 if (numBlocks >= MAX_ALLOCS) {
307 fprintf(stderr, "Too many allocations\en");
311 alloc[j] = malloc(blockSize);
312 if (alloc[j] == NULL) {
318 printf("\en============== After allocating blocks ==============\en");
321 for (int j = freeBegin; j < freeEnd; j += freeStep)
324 printf("\en============== After freeing blocks ==============\en");
336 .BR malloc_stats (3),