Changes: Ready for 5.13
[man-pages.git] / man3 / mallinfo.3
blob36d791cab8f8d480c9649b73f9d1f2d9b5987ce6
1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
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 .\" %%%LICENSE_END
24 .\"
25 .TH MALLINFO 3  2021-03-22 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 mallinfo, mallinfo2 \- obtain memory allocation information
28 .SH SYNOPSIS
29 .nf
30 .B #include <malloc.h>
31 .PP
32 .B struct mallinfo mallinfo(void);
33 .B struct mallinfo2 mallinfo2(void);
34 .fi
35 .SH DESCRIPTION
36 These functions return a copy of a structure containing information about
37 memory allocations performed by
38 .BR malloc (3)
39 and related functions.
40 The structure returned by each function contains the same fields.
41 However, the older function,
42 .BR mallinfo (),
43 is deprecated since the type used for the fields is too small (see BUGS).
44 .PP
45 Note that not all allocations are visible to these functions;
46 see BUGS and consider using
47 .BR malloc_info (3)
48 instead.
49 .PP
50 The
51 .I mallinfo2
52 structure returned by
53 .BR mallinfo2 ()
54 is defined as follows:
55 .PP
56 .in +4n
57 .EX
58 struct mallinfo2 {
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
64                          (bytes) */
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) */
71 .EE
72 .in
73 .PP
74 The
75 .I mallinfo
76 structure returned by the deprecated
77 .BR mallinfo ()
78 function is exactly the same, except that the fields are typed as
79 .IR int .
80 .PP
81 The structure fields contain the following information:
82 .TP 10
83 .I arena
84 The total amount of memory allocated by means other than
85 .BR mmap (2)
86 (i.e., memory allocated on the heap).
87 This figure includes both in-use blocks and blocks on the free list.
88 .TP
89 .I ordblks
90 The number of ordinary (i.e., non-fastbin) free blocks.
91 .TP
92 .I smblks
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
96 .BR mallopt (3)).
97 .TP
98 .I hblks
99 The number of blocks currently allocated using
100 .BR mmap (2).
101 (See the discussion of
102 .B M_MMAP_THRESHOLD
104 .BR mallopt (3).)
106 .I hblkhd
107 The number of bytes in blocks currently allocated using
108 .BR mmap (2).
110 .I usmblks
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.
117 .I fsmblks
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.
122 .I uordblks
123 The total number of bytes used by in-use allocations.
125 .I fordblks
126 The total number of bytes in free blocks.
128 .I keepcost
129 The total amount of releasable free space at the top
130 of the heap.
131 This is the maximum number of bytes that could ideally
132 (i.e., ignoring page alignment restrictions, and so on) be released by
133 .BR malloc_trim (3).
134 .SH VERSIONS
135 .\" mallinfo(): Available already in glibc 2.0, possibly earlier
137 .BR mallinfo2 ()
138 function was added
139 .\" commit e3960d1c57e57f33e0e846d615788f4ede73b945
140 in glibc 2.33.
141 .SH ATTRIBUTES
142 For an explanation of the terms used in this section, see
143 .BR attributes (7).
144 .ad l
147 allbox;
148 lb lb lbx
149 l l l.
150 Interface       Attribute       Value
152 .BR mallinfo (),
153 .BR mallinfo2 ()
154 T}      Thread safety   T{
155 MT-Unsafe init const:mallopt
160 .sp 1
161 .BR mallinfo ()/
162 .BR mallinfo2 ()
163 would access some global internal objects.
164 If modify them with non-atomically,
165 may get inconsistent results.
166 The identifier
167 .I mallopt
169 .I const:mallopt
170 mean that
171 .BR mallopt ()
172 would modify the global internal objects with atomics, that make sure
173 .BR mallinfo ()/
174 .BR mallinfo2 ()
175 is safe enough, others modify with non-atomically maybe not.
176 .SH CONFORMING TO
177 These functions are not specified by POSIX or the C standards.
179 .BR mallinfo ()
180 function exists on many System V derivatives,
181 and was specified in the SVID.
182 .SH BUGS
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.
190 .BR malloc_stats (3)
192 .BR malloc_info (3)
193 for alternatives that include information about other arenas.
195 The fields of the
196 .I mallinfo
197 structure that is returned by the older
198 .BR mallinfo ()
199 function are typed as
200 .IR int .
201 However, because some internal bookkeeping values may be of type
202 .IR long ,
203 the reported values may wrap around zero and thus be inaccurate.
204 .SH EXAMPLES
205 The program below employs
206 .BR mallinfo2 ()
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
213 .BR malloc (3).
215 The remaining three arguments specify which of the allocated blocks
216 should be freed with
217 .BR free (3).
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:
233 .in +4n
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
273 .SS Program source
276 #include <malloc.h>
277 #include <stdlib.h>
278 #include <string.h>
280 static void
281 display_mallinfo2(void)
283     struct mallinfo2 mi;
285     mi = mallinfo2();
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;
305     size_t blockSize;
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]);
310         exit(EXIT_FAILURE);
311     }
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");
320     display_mallinfo2();
322     for (int j = 0; j < numBlocks; j++) {
323         if (numBlocks >= MAX_ALLOCS) {
324             fprintf(stderr, "Too many allocations\en");
325             exit(EXIT_FAILURE);
326         }
328         alloc[j] = malloc(blockSize);
329         if (alloc[j] == NULL) {
330             perror("malloc");
331             exit(EXIT_FAILURE);
332         }
333     }
335     printf("\en============== After allocating blocks ==============\en");
336     display_mallinfo2();
338     for (int j = freeBegin; j < freeEnd; j += freeStep)
339         free(alloc[j]);
341     printf("\en============== After freeing blocks ==============\en");
342     display_mallinfo2();
344     exit(EXIT_SUCCESS);
347 .SH SEE ALSO
348 .ad l
350 .BR mmap (2),
351 .BR malloc (3),
352 .BR malloc_info (3),
353 .BR malloc_stats (3),
354 .BR malloc_trim (3),
355 .BR mallopt (3)