]>
Commit | Line | Data |
---|---|---|
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 |
28 | mallinfo \- 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 | |
34 | The | |
35 | .BR mallinfo () | |
36 | function returns a copy of a structure containing information about | |
37 | memory allocations performed by | |
38 | .BR malloc (3) | |
39 | and related functions. | |
2eb8835a EH |
40 | .PP |
41 | Note that not all allocations are visible to | |
42 | .BR mallinfo (); | |
43 | see BUGS and consider using | |
44 | .BR malloc_info (3) | |
45 | instead. | |
46 | .PP | |
47 | The returned structure is defined as follows: | |
62d874a0 MK |
48 | .PP |
49 | .in +4n | |
b8302363 | 50 | .EX |
62d874a0 MK |
51 | struct 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 | |
66 | The fields of the | |
67 | .I mallinfo | |
68 | structure contain the following information: | |
62d874a0 MK |
69 | .TP 10 |
70 | .I arena | |
71 | The total amount of memory allocated by means other than | |
72 | .BR mmap (2) | |
73 | (i.e., memory allocated on the heap). | |
74 | This figure includes both in-use blocks and blocks on the free list. | |
75 | .TP | |
76 | .I ordblks | |
77 | The number of ordinary (i.e., non-fastbin) free blocks. | |
78 | .TP | |
79 | .I smblks | |
80 | The number of fastbin free blocks (see | |
81 | .BR mallopt (3)). | |
82 | .TP | |
83 | .I hblks | |
84 | The number of blocks currently allocated using | |
85 | .BR mmap (2). | |
35af1345 MK |
86 | (See the discussion of |
87 | .B M_MMAP_THRESHOLD | |
88 | in | |
89 | .BR mallopt (3).) | |
62d874a0 MK |
90 | .TP |
91 | .I hblkhd | |
92 | The number of bytes in blocks currently allocated using | |
93 | .BR mmap (2). | |
94 | .TP | |
95 | .I usmblks | |
96 | The "highwater mark" for allocated space\(emthat is, | |
97 | the maximum amount of space that was ever allocated. | |
98 | This field is maintained only in nonthreading environments. | |
99 | .TP | |
100 | .I fsmblks | |
101 | The total number of bytes in fastbin free blocks. | |
102 | .TP | |
103 | .I uordblks | |
104 | The total number of bytes used by in-use allocations. | |
105 | .TP | |
106 | .I fordblks | |
107 | The total number of bytes in free blocks. | |
108 | .TP | |
109 | .I keepcost | |
110 | The total amount of releasable free space at the top | |
111 | of the heap. | |
112 | This 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 |
118 | For an explanation of the terms used in this section, see | |
119 | .BR attributes (7). | |
120 | .TS | |
121 | allbox; | |
122 | lb lb lbw28 | |
123 | l l l. | |
124 | Interface Attribute Value | |
125 | T{ | |
126 | .BR mallinfo () | |
127 | T} Thread safety MT-Unsafe init const:mallopt | |
128 | .TE | |
847e0d88 | 129 | .sp 1 |
c32cf268 | 130 | .BR mallinfo () |
696bd2da MK |
131 | would access some global internal objects. |
132 | If modify them with non-atomically, | |
c32cf268 ZL |
133 | may get inconsistent results. |
134 | The identifier | |
135 | .I mallopt | |
136 | in | |
137 | .I const:mallopt | |
138 | mean that | |
139 | .BR mallopt () | |
3c841730 | 140 | would modify the global internal objects with atomics, that make sure |
c32cf268 ZL |
141 | .BR mallinfo () |
142 | is safe enough, others modify with non-atomically maybe not. | |
62d874a0 MK |
143 | .SH CONFORMING TO |
144 | This function is not specified by POSIX or the C standards. | |
145 | A similar function exists on many System V derivatives, | |
146 | and 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. |
153 | Allocations in other arenas are excluded. | |
154 | See | |
155 | .BR malloc_stats (3) | |
156 | and | |
157 | .BR malloc_info (3) | |
158 | for alternatives that include information about other arenas. | |
847e0d88 | 159 | .PP |
62d874a0 MK |
160 | The fields of the |
161 | .I mallinfo | |
162 | structure are typed as | |
163 | .IR int . | |
164 | However, because some internal bookkeeping values may be of type | |
165 | .IR long , | |
166 | the reported values may wrap around zero and thus be inaccurate. | |
167 | .SH EXAMPLE | |
168 | The program below employs | |
169 | .BR mallinfo () | |
170 | to retrieve memory allocation statistics before and after | |
171 | allocating and freeing some blocks of memory. | |
172 | The statistics are displayed on standard output. | |
847e0d88 | 173 | .PP |
62d874a0 MK |
174 | The first two command-line arguments specify the number and size of |
175 | blocks to be allocated with | |
176 | .BR malloc (3). | |
847e0d88 | 177 | .PP |
62d874a0 MK |
178 | The remaining three arguments specify which of the allocated blocks |
179 | should be freed with | |
180 | .BR free (3). | |
181 | These three arguments are optional, and specify (in order): | |
182 | the step size to be used in the loop that frees blocks | |
183 | (the default is 1, meaning free all blocks in the range); | |
184 | the ordinal position of the first block to be freed | |
185 | (default 0, meaning the first allocated block); | |
186 | and a number one greater than the ordinal position | |
187 | of the last block to be freed | |
188 | (default is one greater than the maximum block number). | |
189 | If these three arguments are omitted, | |
190 | then the defaults cause all allocated blocks to be freed. | |
847e0d88 | 191 | .PP |
62d874a0 MK |
192 | In the following example run of the program, |
193 | 1000 allocations of 100 bytes are performed, | |
194 | and 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 ============== | |
200 | Total non\-mmapped bytes (arena): 0 | |
201 | # of free chunks (ordblks): 1 | |
202 | # of free fastbin blocks (smblks): 0 | |
203 | # of mapped regions (hblks): 0 | |
204 | Bytes in mapped regions (hblkhd): 0 | |
205 | Max. total allocated space (usmblks): 0 | |
206 | Free bytes held in fastbins (fsmblks): 0 | |
207 | Total allocated space (uordblks): 0 | |
208 | Total free space (fordblks): 0 | |
209 | Topmost releasable block (keepcost): 0 | |
210 | ||
211 | ============== After allocating blocks ============== | |
212 | Total non\-mmapped bytes (arena): 135168 | |
213 | # of free chunks (ordblks): 1 | |
214 | # of free fastbin blocks (smblks): 0 | |
215 | # of mapped regions (hblks): 0 | |
216 | Bytes in mapped regions (hblkhd): 0 | |
217 | Max. total allocated space (usmblks): 0 | |
218 | Free bytes held in fastbins (fsmblks): 0 | |
219 | Total allocated space (uordblks): 104000 | |
220 | Total free space (fordblks): 31168 | |
221 | Topmost releasable block (keepcost): 31168 | |
222 | ||
223 | ============== After freeing blocks ============== | |
224 | Total non\-mmapped bytes (arena): 135168 | |
225 | # of free chunks (ordblks): 501 | |
226 | # of free fastbin blocks (smblks): 0 | |
227 | # of mapped regions (hblks): 0 | |
228 | Bytes in mapped regions (hblkhd): 0 | |
229 | Max. total allocated space (usmblks): 0 | |
230 | Free bytes held in fastbins (fsmblks): 0 | |
231 | Total allocated space (uordblks): 52000 | |
232 | Total free space (fordblks): 83168 | |
233 | Topmost 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 | |
243 | static void | |
244 | display_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 | ||
262 | int | |
263 | main(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) |