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