]>
Commit | Line | Data |
---|---|---|
62d874a0 MK |
1 | '\" t |
2 | .\" Copyright (c) 2010 by Michael Kerrisk <mtk.manpages@gmail.com> | |
3 | .\" | |
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 | .\" | |
f704bc68 | 24 | .TH MALLINFO 3 2012-02-27 "Linux" "Linux Programmer's Manual" |
62d874a0 MK |
25 | .SH NAME |
26 | mallinfo \- obtain memory allocation information | |
27 | .SH SYNOPSIS | |
28 | .B #include <malloc.h> | |
29 | ||
30 | .B struct mallinfo mallinfo(void); | |
31 | .SH DESCRIPTION | |
32 | The | |
33 | .BR mallinfo () | |
34 | function returns a copy of a structure containing information about | |
35 | memory allocations performed by | |
36 | .BR malloc (3) | |
37 | and related functions. | |
38 | This structure is defined as follows: | |
39 | .PP | |
40 | .in +4n | |
41 | .nf | |
42 | struct mallinfo { | |
43 | int arena; /* Non-mmapped space allocated (bytes) */ | |
44 | int ordblks; /* Number of free chunks */ | |
45 | int smblks; /* Number of free fastbin blocks */ | |
46 | int hblks; /* Number of mmapped regions */ | |
47 | int hblkhd; /* Space allocated in mmapped regions (bytes) */ | |
48 | int usmblks; /* Maximum total allocated space (bytes) */ | |
49 | int fsmblks; /* Space in freed fastbin blocks (bytes) */ | |
50 | int uordblks; /* Total allocated space (bytes) */ | |
51 | int fordblks; /* Total free space (bytes) */ | |
52 | int keepcost; /* Top-most, releasable space (bytes) */ | |
53 | }; | |
54 | .fi | |
55 | .in | |
56 | .PP | |
57 | The fields of the | |
58 | .I mallinfo | |
59 | structure contain the following information: | |
60 | ||
61 | .TP 10 | |
62 | .I arena | |
63 | The total amount of memory allocated by means other than | |
64 | .BR mmap (2) | |
65 | (i.e., memory allocated on the heap). | |
66 | This figure includes both in-use blocks and blocks on the free list. | |
67 | .TP | |
68 | .I ordblks | |
69 | The number of ordinary (i.e., non-fastbin) free blocks. | |
70 | .TP | |
71 | .I smblks | |
72 | The number of fastbin free blocks (see | |
73 | .BR mallopt (3)). | |
74 | .TP | |
75 | .I hblks | |
76 | The number of blocks currently allocated using | |
77 | .BR mmap (2). | |
78 | .TP | |
79 | .I hblkhd | |
80 | The number of bytes in blocks currently allocated using | |
81 | .BR mmap (2). | |
82 | .TP | |
83 | .I usmblks | |
84 | The "highwater mark" for allocated space\(emthat is, | |
85 | the maximum amount of space that was ever allocated. | |
86 | This field is maintained only in nonthreading environments. | |
87 | .TP | |
88 | .I fsmblks | |
89 | The total number of bytes in fastbin free blocks. | |
90 | .TP | |
91 | .I uordblks | |
92 | The total number of bytes used by in-use allocations. | |
93 | .TP | |
94 | .I fordblks | |
95 | The total number of bytes in free blocks. | |
96 | .TP | |
97 | .I keepcost | |
98 | The total amount of releasable free space at the top | |
99 | of the heap. | |
100 | This is the maximum number of bytes that could ideally | |
101 | (i.e., ignoring page alignment restrictions, and so on) be released by | |
102 | .BR malloc_trim (3). | |
103 | .\" .SH VERSIONS | |
104 | .\" Available already in glibc 2.0, possibly earlier | |
105 | .SH CONFORMING TO | |
106 | This function is not specified by POSIX or the C standards. | |
107 | A similar function exists on many System V derivatives, | |
108 | and was specified in the SVID. | |
f704bc68 MK |
109 | .SH BUGS |
110 | .\" See the 24 Aug 2011 mail by Paul Pluzhnikov: | |
111 | .\" "[patch] Fix mallinfo() to accumulate results for all arenas" | |
112 | .\" on libc-alpha@sourceware.org | |
62d874a0 MK |
113 | Information is returned only for the main arena |
114 | (the main memory allocation area). | |
f704bc68 | 115 | |
62d874a0 MK |
116 | The fields of the |
117 | .I mallinfo | |
118 | structure are typed as | |
119 | .IR int . | |
120 | However, because some internal bookkeeping values may be of type | |
121 | .IR long , | |
122 | the reported values may wrap around zero and thus be inaccurate. | |
123 | .SH EXAMPLE | |
124 | The program below employs | |
125 | .BR mallinfo () | |
126 | to retrieve memory allocation statistics before and after | |
127 | allocating and freeing some blocks of memory. | |
128 | The statistics are displayed on standard output. | |
129 | ||
130 | The first two command-line arguments specify the number and size of | |
131 | blocks to be allocated with | |
132 | .BR malloc (3). | |
133 | ||
134 | The remaining three arguments specify which of the allocated blocks | |
135 | should be freed with | |
136 | .BR free (3). | |
137 | These three arguments are optional, and specify (in order): | |
138 | the step size to be used in the loop that frees blocks | |
139 | (the default is 1, meaning free all blocks in the range); | |
140 | the ordinal position of the first block to be freed | |
141 | (default 0, meaning the first allocated block); | |
142 | and a number one greater than the ordinal position | |
143 | of the last block to be freed | |
144 | (default is one greater than the maximum block number). | |
145 | If these three arguments are omitted, | |
146 | then the defaults cause all allocated blocks to be freed. | |
147 | ||
148 | In the following example run of the program, | |
149 | 1000 allocations of 100 bytes are performed, | |
150 | and then every second allocated block is freed: | |
151 | .PP | |
152 | .in +4n | |
153 | .nf | |
154 | $ \fB./a.out 1000 100 2\fP | |
155 | ============== Before allocating blocks ============== | |
156 | Total non\-mmapped bytes (arena): 0 | |
157 | # of free chunks (ordblks): 1 | |
158 | # of free fastbin blocks (smblks): 0 | |
159 | # of mapped regions (hblks): 0 | |
160 | Bytes in mapped regions (hblkhd): 0 | |
161 | Max. total allocated space (usmblks): 0 | |
162 | Free bytes held in fastbins (fsmblks): 0 | |
163 | Total allocated space (uordblks): 0 | |
164 | Total free space (fordblks): 0 | |
165 | Topmost releasable block (keepcost): 0 | |
166 | ||
167 | ============== After allocating blocks ============== | |
168 | Total non\-mmapped bytes (arena): 135168 | |
169 | # of free chunks (ordblks): 1 | |
170 | # of free fastbin blocks (smblks): 0 | |
171 | # of mapped regions (hblks): 0 | |
172 | Bytes in mapped regions (hblkhd): 0 | |
173 | Max. total allocated space (usmblks): 0 | |
174 | Free bytes held in fastbins (fsmblks): 0 | |
175 | Total allocated space (uordblks): 104000 | |
176 | Total free space (fordblks): 31168 | |
177 | Topmost releasable block (keepcost): 31168 | |
178 | ||
179 | ============== After freeing blocks ============== | |
180 | Total non\-mmapped bytes (arena): 135168 | |
181 | # of free chunks (ordblks): 501 | |
182 | # of free fastbin blocks (smblks): 0 | |
183 | # of mapped regions (hblks): 0 | |
184 | Bytes in mapped regions (hblkhd): 0 | |
185 | Max. total allocated space (usmblks): 0 | |
186 | Free bytes held in fastbins (fsmblks): 0 | |
187 | Total allocated space (uordblks): 52000 | |
188 | Total free space (fordblks): 83168 | |
189 | Topmost releasable block (keepcost): 31168 | |
190 | .fi | |
191 | .in | |
192 | .SS Program source | |
193 | \& | |
194 | .nf | |
195 | #include <malloc.h> | |
196 | #include "tlpi_hdr.h" | |
197 | ||
198 | static void | |
199 | display_mallinfo(void) | |
200 | { | |
201 | struct mallinfo mi; | |
202 | ||
203 | mi = mallinfo(); | |
204 | ||
205 | printf("Total non\-mmapped bytes (arena): %d\\n", mi.arena); | |
206 | printf("# of free chunks (ordblks): %d\\n", mi.ordblks); | |
207 | printf("# of free fastbin blocks (smblks): %d\\n", mi.smblks); | |
208 | printf("# of mapped regions (hblks): %d\\n", mi.hblks); | |
209 | printf("Bytes in mapped regions (hblkhd): %d\\n", mi.hblkhd); | |
210 | printf("Max. total allocated space (usmblks): %d\\n", mi.usmblks); | |
211 | printf("Free bytes held in fastbins (fsmblks): %d\\n", mi.fsmblks); | |
212 | printf("Total allocated space (uordblks): %d\\n", mi.uordblks); | |
213 | printf("Total free space (fordblks): %d\\n", mi.fordblks); | |
214 | printf("Topmost releasable block (keepcost): %d\\n", mi.keepcost); | |
215 | } | |
216 | ||
217 | int | |
218 | main(int argc, char *argv[]) | |
219 | { | |
220 | #define MAX_ALLOCS 2000000 | |
221 | char *alloc[MAX_ALLOCS]; | |
222 | int numBlocks, j, freeBegin, freeEnd, freeStep; | |
223 | size_t blockSize; | |
224 | ||
225 | if (argc < 3 || strcmp(argv[1], "\-\-help") == 0) | |
226 | usageErr("%s num\-blocks block\-size [free\-step [start\-free " | |
227 | "[end\-free]]]\\n", argv[0]); | |
228 | ||
229 | numBlocks = atoi(argv[1]); | |
230 | blockSize = atoi(argv[2]); | |
231 | freeStep = (argc > 3) ? atoi(argv[3]) : 1; | |
232 | freeBegin = (argc > 4) ? atoi(argv[4]) : 0; | |
233 | freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks; | |
234 | ||
235 | printf("============== Before allocating blocks ==============\\n"); | |
236 | display_mallinfo(); | |
237 | ||
238 | for (j = 0; j < numBlocks; j++) { | |
239 | if (numBlocks >= MAX_ALLOCS) | |
240 | fatal("Too many allocations"); | |
241 | ||
242 | alloc[j] = malloc(blockSize); | |
243 | if (alloc[j] == NULL) | |
244 | errExit("malloc"); | |
245 | } | |
246 | ||
247 | printf("\\n============== After allocating blocks ==============\\n"); | |
248 | display_mallinfo(); | |
249 | ||
250 | for (j = freeBegin; j < freeEnd; j += freeStep) | |
251 | free(alloc[j]); | |
252 | ||
253 | printf("\\n============== After freeing blocks ==============\\n"); | |
254 | display_mallinfo(); | |
255 | ||
256 | exit(EXIT_SUCCESS); | |
257 | } | |
258 | .fi | |
259 | .SH SEE ALSO | |
260 | .ad l | |
261 | .nh | |
262 | .BR mmap (2), | |
263 | .BR malloc (3), | |
264 | .BR malloc_stats (3), | |
265 | .BR malloc_trim (3), | |
266 | .BR mallopt (3) |