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