1MALLINFO(3) Linux Programmer's Manual MALLINFO(3)
2
3
4
6 mallinfo - obtain memory allocation information
7
9 #include <malloc.h>
10
11 struct mallinfo mallinfo(void);
12
14 The mallinfo() function returns a copy of a structure containing infor‐
15 mation about memory allocations performed by malloc(3) and related
16 functions. This structure is defined as follows:
17
18 struct mallinfo {
19 int arena; /* Non-mmapped space allocated (bytes) */
20 int ordblks; /* Number of free chunks */
21 int smblks; /* Number of free fastbin blocks */
22 int hblks; /* Number of mmapped regions */
23 int hblkhd; /* Space allocated in mmapped regions (bytes) */
24 int usmblks; /* Maximum total allocated space (bytes) */
25 int fsmblks; /* Space in freed fastbin blocks (bytes) */
26 int uordblks; /* Total allocated space (bytes) */
27 int fordblks; /* Total free space (bytes) */
28 int keepcost; /* Top-most, releasable space (bytes) */
29 };
30
31 The fields of the mallinfo structure contain the following information:
32
33 arena The total amount of memory allocated by means other than
34 mmap(2) (i.e., memory allocated on the heap). This figure
35 includes both in-use blocks and blocks on the free list.
36
37 ordblks The number of ordinary (i.e., non-fastbin) free blocks.
38
39 smblks The number of fastbin free blocks (see mallopt(3)).
40
41 hblks The number of blocks currently allocated using mmap(2). (See
42 the discussion of M_MMAP_THRESHOLD in mallopt(3).)
43
44 hblkhd The number of bytes in blocks currently allocated using
45 mmap(2).
46
47 usmblks The "highwater mark" for allocated space—that is, the maximum
48 amount of space that was ever allocated. This field is main‐
49 tained only in nonthreading environments.
50
51 fsmblks The total number of bytes in fastbin free blocks.
52
53 uordblks The total number of bytes used by in-use allocations.
54
55 fordblks The total number of bytes in free blocks.
56
57 keepcost The total amount of releasable free space at the top of the
58 heap. This is the maximum number of bytes that could ideally
59 (i.e., ignoring page alignment restrictions, and so on) be
60 released by malloc_trim(3).
61
63 For an explanation of the terms used in this section, see
64 attributes(7).
65
66 ┌───────────┬───────────────┬──────────────────────────────┐
67 │Interface │ Attribute │ Value │
68 ├───────────┼───────────────┼──────────────────────────────┤
69 │mallinfo() │ Thread safety │ MT-Unsafe init const:mallopt │
70 └───────────┴───────────────┴──────────────────────────────┘
71 mallinfo() would access some global internal objects. If modify them
72 with non-atomically, may get inconsistent results. The identifier mal‐
73 lopt in const:mallopt mean that mallopt() would modify the global
74 internal objects with atomics, that make sure mallinfo() is safe
75 enough, others modify with non-atomically maybe not.
76
78 This function is not specified by POSIX or the C standards. A similar
79 function exists on many System V derivatives, and was specified in the
80 SVID.
81
83 Information is returned for only the main memory allocation area.
84 Allocations in other arenas are excluded. See malloc_stats(3) and mal‐
85 loc_info(3) for alternatives that include information about other are‐
86 nas.
87
88 The fields of the mallinfo structure are typed as int. However,
89 because some internal bookkeeping values may be of type long, the
90 reported values may wrap around zero and thus be inaccurate.
91
93 The program below employs mallinfo() to retrieve memory allocation sta‐
94 tistics before and after allocating and freeing some blocks of memory.
95 The statistics are displayed on standard output.
96
97 The first two command-line arguments specify the number and size of
98 blocks to be allocated with malloc(3).
99
100 The remaining three arguments specify which of the allocated blocks
101 should be freed with free(3). These three arguments are optional, and
102 specify (in order): the step size to be used in the loop that frees
103 blocks (the default is 1, meaning free all blocks in the range); the
104 ordinal position of the first block to be freed (default 0, meaning the
105 first allocated block); and a number one greater than the ordinal posi‐
106 tion of the last block to be freed (default is one greater than the
107 maximum block number). If these three arguments are omitted, then the
108 defaults cause all allocated blocks to be freed.
109
110 In the following example run of the program, 1000 allocations of 100
111 bytes are performed, and then every second allocated block is freed:
112
113 $ ./a.out 1000 100 2
114 ============== Before allocating blocks ==============
115 Total non-mmapped bytes (arena): 0
116 # of free chunks (ordblks): 1
117 # of free fastbin blocks (smblks): 0
118 # of mapped regions (hblks): 0
119 Bytes in mapped regions (hblkhd): 0
120 Max. total allocated space (usmblks): 0
121 Free bytes held in fastbins (fsmblks): 0
122 Total allocated space (uordblks): 0
123 Total free space (fordblks): 0
124 Topmost releasable block (keepcost): 0
125
126 ============== After allocating blocks ==============
127 Total non-mmapped bytes (arena): 135168
128 # of free chunks (ordblks): 1
129 # of free fastbin blocks (smblks): 0
130 # of mapped regions (hblks): 0
131 Bytes in mapped regions (hblkhd): 0
132 Max. total allocated space (usmblks): 0
133 Free bytes held in fastbins (fsmblks): 0
134 Total allocated space (uordblks): 104000
135 Total free space (fordblks): 31168
136 Topmost releasable block (keepcost): 31168
137
138 ============== After freeing blocks ==============
139 Total non-mmapped bytes (arena): 135168
140 # of free chunks (ordblks): 501
141 # of free fastbin blocks (smblks): 0
142 # of mapped regions (hblks): 0
143 Bytes in mapped regions (hblkhd): 0
144 Max. total allocated space (usmblks): 0
145 Free bytes held in fastbins (fsmblks): 0
146 Total allocated space (uordblks): 52000
147 Total free space (fordblks): 83168
148 Topmost releasable block (keepcost): 31168
149
150 Program source
151
152 #include <malloc.h>
153 #include <stdlib.h>
154 #include <string.h>
155
156 static void
157 display_mallinfo(void)
158 {
159 struct mallinfo mi;
160
161 mi = mallinfo();
162
163 printf("Total non-mmapped bytes (arena): %d\n", mi.arena);
164 printf("# of free chunks (ordblks): %d\n", mi.ordblks);
165 printf("# of free fastbin blocks (smblks): %d\n", mi.smblks);
166 printf("# of mapped regions (hblks): %d\n", mi.hblks);
167 printf("Bytes in mapped regions (hblkhd): %d\n", mi.hblkhd);
168 printf("Max. total allocated space (usmblks): %d\n", mi.usmblks);
169 printf("Free bytes held in fastbins (fsmblks): %d\n", mi.fsmblks);
170 printf("Total allocated space (uordblks): %d\n", mi.uordblks);
171 printf("Total free space (fordblks): %d\n", mi.fordblks);
172 printf("Topmost releasable block (keepcost): %d\n", mi.keepcost);
173 }
174
175 int
176 main(int argc, char *argv[])
177 {
178 #define MAX_ALLOCS 2000000
179 char *alloc[MAX_ALLOCS];
180 int numBlocks, j, freeBegin, freeEnd, freeStep;
181 size_t blockSize;
182
183 if (argc < 3 || strcmp(argv[1], "--help") == 0) {
184 fprintf(stderr, "%s num-blocks block-size [free-step "
185 "[start-free [end-free]]]\n", argv[0]);
186 exit(EXIT_FAILURE);
187 }
188
189 numBlocks = atoi(argv[1]);
190 blockSize = atoi(argv[2]);
191 freeStep = (argc > 3) ? atoi(argv[3]) : 1;
192 freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
193 freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
194
195 printf("============== Before allocating blocks ==============\n");
196 display_mallinfo();
197
198 for (j = 0; j < numBlocks; j++) {
199 if (numBlocks >= MAX_ALLOCS) {
200 fprintf(stderr, "Too many allocations\n");
201 exit(EXIT_FAILURE);
202 }
203
204 alloc[j] = malloc(blockSize);
205 if (alloc[j] == NULL) {
206 perror("malloc");
207 exit(EXIT_FAILURE);
208 }
209 }
210
211 printf("\n============== After allocating blocks ==============\n");
212 display_mallinfo();
213
214 for (j = freeBegin; j < freeEnd; j += freeStep)
215 free(alloc[j]);
216
217 printf("\n============== After freeing blocks ==============\n");
218 display_mallinfo();
219
220 exit(EXIT_SUCCESS);
221 }
222
224 mmap(2), malloc(3), malloc_info(3), malloc_stats(3), malloc_trim(3),
225 mallopt(3)
226
228 This page is part of release 4.16 of the Linux man-pages project. A
229 description of the project, information about reporting bugs, and the
230 latest version of this page, can be found at
231 https://www.kernel.org/doc/man-pages/.
232
233
234
235Linux 2017-09-15 MALLINFO(3)