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 This function is not specified by POSIX or the C standards. A similar
64 function exists on many System V derivatives, and was specified in the
65 SVID.
66
68 Information is returned for only the main memory allocation area.
69 Allocations in other arenas are excluded. See malloc_stats(3) and mal‐
70 loc_info(3) for alternatives that include information about other are‐
71 nas.
72
73 The fields of the mallinfo structure are typed as int. However,
74 because some internal bookkeeping values may be of type long, the
75 reported values may wrap around zero and thus be inaccurate.
76
78 The program below employs mallinfo() to retrieve memory allocation sta‐
79 tistics before and after allocating and freeing some blocks of memory.
80 The statistics are displayed on standard output.
81
82 The first two command-line arguments specify the number and size of
83 blocks to be allocated with malloc(3).
84
85 The remaining three arguments specify which of the allocated blocks
86 should be freed with free(3). These three arguments are optional, and
87 specify (in order): the step size to be used in the loop that frees
88 blocks (the default is 1, meaning free all blocks in the range); the
89 ordinal position of the first block to be freed (default 0, meaning the
90 first allocated block); and a number one greater than the ordinal posi‐
91 tion of the last block to be freed (default is one greater than the
92 maximum block number). If these three arguments are omitted, then the
93 defaults cause all allocated blocks to be freed.
94
95 In the following example run of the program, 1000 allocations of 100
96 bytes are performed, and then every second allocated block is freed:
97
98 $ ./a.out 1000 100 2
99 ============== Before allocating blocks ==============
100 Total non-mmapped bytes (arena): 0
101 # of free chunks (ordblks): 1
102 # of free fastbin blocks (smblks): 0
103 # of mapped regions (hblks): 0
104 Bytes in mapped regions (hblkhd): 0
105 Max. total allocated space (usmblks): 0
106 Free bytes held in fastbins (fsmblks): 0
107 Total allocated space (uordblks): 0
108 Total free space (fordblks): 0
109 Topmost releasable block (keepcost): 0
110
111 ============== After allocating blocks ==============
112 Total non-mmapped bytes (arena): 135168
113 # of free chunks (ordblks): 1
114 # of free fastbin blocks (smblks): 0
115 # of mapped regions (hblks): 0
116 Bytes in mapped regions (hblkhd): 0
117 Max. total allocated space (usmblks): 0
118 Free bytes held in fastbins (fsmblks): 0
119 Total allocated space (uordblks): 104000
120 Total free space (fordblks): 31168
121 Topmost releasable block (keepcost): 31168
122
123 ============== After freeing blocks ==============
124 Total non-mmapped bytes (arena): 135168
125 # of free chunks (ordblks): 501
126 # of free fastbin blocks (smblks): 0
127 # of mapped regions (hblks): 0
128 Bytes in mapped regions (hblkhd): 0
129 Max. total allocated space (usmblks): 0
130 Free bytes held in fastbins (fsmblks): 0
131 Total allocated space (uordblks): 52000
132 Total free space (fordblks): 83168
133 Topmost releasable block (keepcost): 31168
134
135 Program source
136
137 #include <malloc.h>
138 #include "tlpi_hdr.h"
139
140 static void
141 display_mallinfo(void)
142 {
143 struct mallinfo mi;
144
145 mi = mallinfo();
146
147 printf("Total non-mmapped bytes (arena): %d\n", mi.arena);
148 printf("# of free chunks (ordblks): %d\n", mi.ordblks);
149 printf("# of free fastbin blocks (smblks): %d\n", mi.smblks);
150 printf("# of mapped regions (hblks): %d\n", mi.hblks);
151 printf("Bytes in mapped regions (hblkhd): %d\n", mi.hblkhd);
152 printf("Max. total allocated space (usmblks): %d\n", mi.usmblks);
153 printf("Free bytes held in fastbins (fsmblks): %d\n", mi.fsmblks);
154 printf("Total allocated space (uordblks): %d\n", mi.uordblks);
155 printf("Total free space (fordblks): %d\n", mi.fordblks);
156 printf("Topmost releasable block (keepcost): %d\n", mi.keepcost);
157 }
158
159 int
160 main(int argc, char *argv[])
161 {
162 #define MAX_ALLOCS 2000000
163 char *alloc[MAX_ALLOCS];
164 int numBlocks, j, freeBegin, freeEnd, freeStep;
165 size_t blockSize;
166
167 if (argc < 3 || strcmp(argv[1], "--help") == 0)
168 usageErr("%s num-blocks block-size [free-step [start-free "
169 "[end-free]]]\n", argv[0]);
170
171 numBlocks = atoi(argv[1]);
172 blockSize = atoi(argv[2]);
173 freeStep = (argc > 3) ? atoi(argv[3]) : 1;
174 freeBegin = (argc > 4) ? atoi(argv[4]) : 0;
175 freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
176
177 printf("============== Before allocating blocks ==============\n");
178 display_mallinfo();
179
180 for (j = 0; j < numBlocks; j++) {
181 if (numBlocks >= MAX_ALLOCS)
182 fatal("Too many allocations");
183
184 alloc[j] = malloc(blockSize);
185 if (alloc[j] == NULL)
186 errExit("malloc");
187 }
188
189 printf("\n============== After allocating blocks ==============\n");
190 display_mallinfo();
191
192 for (j = freeBegin; j < freeEnd; j += freeStep)
193 free(alloc[j]);
194
195 printf("\n============== After freeing blocks ==============\n");
196 display_mallinfo();
197
198 exit(EXIT_SUCCESS);
199 }
200
202 mmap(2), malloc(3), malloc_info(3), malloc_stats(3), malloc_trim(3),
203 mallopt(3)
204
206 This page is part of release 3.53 of the Linux man-pages project. A
207 description of the project, and information about reporting bugs, can
208 be found at http://www.kernel.org/doc/man-pages/.
209
210
211
212Linux 2012-05-06 MALLINFO(3)