1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
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.
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.
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH MALLOC_INFO 3 2019-03-06 "GNU" "Linux Programmer's Manual"
27 malloc_info \- export malloc state to a stream
30 .B #include <malloc.h>
32 .BI "int malloc_info(int " options ", FILE *" stream );
37 function exports an XML string that describes the current state
38 of the memory-allocation
39 implementation in the caller.
40 The string is printed on the file stream
42 The exported string includes information about all arenas (see
45 As currently implemented,
52 on error, it returns \-1, with
54 set to indicate the cause.
62 was added to glibc in version 2.10.
64 For an explanation of the terms used in this section, see
70 Interface Attribute Value
73 T} Thread safety MT-Safe
77 This function is a GNU extension.
79 The memory-allocation information is provided as an XML string
80 (rather than a C structure)
81 because the information may change over time
82 (according to changes in the underlying implementation).
83 The output XML string includes a version field.
86 .BR open_memstream (3)
87 function can be used to send the output of
89 directly into a buffer in memory, rather than to a file.
93 function is designed to address deficiencies in
98 The program below takes up to four command-line arguments,
99 of which the first three are mandatory.
100 The first argument specifies the number of threads that
101 the program should create.
102 All of the threads, including the main thread,
103 allocate the number of blocks of memory specified by the second argument.
104 The third argument controls the size of the blocks to be allocated.
105 The main thread creates blocks of this size,
106 the second thread created by the program allocates blocks of twice this size,
107 the third thread allocates blocks of three times this size, and so on.
111 twice to display the memory-allocation state.
112 The first call takes place before any threads
113 are created or memory allocated.
114 The second call is performed after all threads have allocated memory.
116 In the following example,
117 the command-line arguments specify the creation of one additional thread,
118 and both the main thread and the additional thread
119 allocate 10000 blocks of memory.
120 After the blocks of memory have been allocated,
122 shows the state of two allocation arenas.
126 .RB "$ " "getconf GNU_LIBC_VERSION"
128 .RB "$ " "./a.out 1 10000 100"
129 ============ Before allocating blocks ============
134 <total type="fast" count="0" size="0"/>
135 <total type="rest" count="0" size="0"/>
136 <system type="current" size="135168"/>
137 <system type="max" size="135168"/>
138 <aspace type="total" size="135168"/>
139 <aspace type="mprotect" size="135168"/>
141 <total type="fast" count="0" size="0"/>
142 <total type="rest" count="0" size="0"/>
143 <system type="current" size="135168"/>
144 <system type="max" size="135168"/>
145 <aspace type="total" size="135168"/>
146 <aspace type="mprotect" size="135168"/>
149 ============ After allocating blocks ============
154 <total type="fast" count="0" size="0"/>
155 <total type="rest" count="0" size="0"/>
156 <system type="current" size="1081344"/>
157 <system type="max" size="1081344"/>
158 <aspace type="total" size="1081344"/>
159 <aspace type="mprotect" size="1081344"/>
164 <total type="fast" count="0" size="0"/>
165 <total type="rest" count="0" size="0"/>
166 <system type="current" size="1032192"/>
167 <system type="max" size="1032192"/>
168 <aspace type="total" size="1032192"/>
169 <aspace type="mprotect" size="1032192"/>
171 <total type="fast" count="0" size="0"/>
172 <total type="rest" count="0" size="0"/>
173 <system type="current" size="2113536"/>
174 <system type="max" size="2113536"/>
175 <aspace type="total" size="2113536"/>
176 <aspace type="mprotect" size="2113536"/>
188 static size_t blockSize;
189 static int numThreads, numBlocks;
191 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \e
195 thread_func(void *arg)
200 /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
201 the main thread) allocates a different amount of memory */
203 for (j = 0; j < numBlocks; j++)
204 if (malloc(blockSize * (2 + tn)) == NULL)
205 errExit("malloc\-thread");
207 sleep(100); /* Sleep until main thread terminates */
212 main(int argc, char *argv[])
214 int j, tn, sleepTime;
219 "%s num\-threads num\-blocks block\-size [sleep\-time]\en",
224 numThreads = atoi(argv[1]);
225 numBlocks = atoi(argv[2]);
226 blockSize = atoi(argv[3]);
227 sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
229 thr = calloc(numThreads, sizeof(pthread_t));
233 printf("============ Before allocating blocks ============\en");
234 malloc_info(0, stdout);
236 /* Create threads that allocate different amounts of memory */
238 for (tn = 0; tn < numThreads; tn++) {
239 errno = pthread_create(&thr[tn], NULL, thread_func,
242 errExit("pthread_create");
244 /* If we add a sleep interval after the start\-up of each
245 thread, the threads likely won\(aqt contend for malloc
246 mutexes, and therefore additional arenas won\(aqt be
247 allocated (see malloc(3)). */
253 /* The main thread also allocates some memory */
255 for (j = 0; j < numBlocks; j++)
256 if (malloc(blockSize) == NULL)
259 sleep(2); /* Give all threads a chance to
260 complete allocations */
262 printf("\en============ After allocating blocks ============\en");
263 malloc_info(0, stdout);
271 .BR malloc_stats (3),
273 .BR open_memstream (3)