1 .\" Copyright (c) 2012 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .TH MALLOC_INFO 3 2012-04-28 "GNU" "Linux Programmer's Manual"
25 malloc_info \- export malloc state to a stream
28 .B #include <malloc.h>
30 .BI "int malloc_info(int " options ", FILE *" fp );
35 function exports an XML string that describes the current state
36 of the memory-allocation
37 implementation in the caller.
38 The string is printed on the file stream
40 The exported string includes information about all arenas (see
43 As currently implemented,
50 on error, it returns \-1.
58 was added to glibc in version 2.10.
60 This function is a GNU extension.
62 The memory-allocation information is provided as an XML string
63 (rather than a C structure)
64 because the information may change over time
65 (according to changes in the underlying implementation).
66 The output XML string includes a version field.
70 function is designed to address deficiencies in
75 The program below takes up to four command-line arguments,
76 of which the first three are mandatory.
77 The first argument specifies the number of threads that
78 the program should create.
79 All of the threads, including the main thread,
80 allocate the number of blocks of memory specified by the second argument.
81 The third argument controls the size of the blocks to be allocated.
82 The main thread creates blocks of this size,
83 the second thread created by the program allocates blocks of twice this size,
84 the third thread allocates blocks of three times this size, and so on.
88 twice to display the memory-allocation state.
89 The first call takes place before any threads
90 are created or memory allocated.
91 The second call is performed after all threads have allocated memory.
93 In the following example,
94 the command-line arguments specify the creation of one additional thread,
95 and both the main thread and the additional thread
96 allocate 10000 blocks of memory.
97 After the blocks of memory have been allocated,
99 shows the state of two allocation arenas.
103 .RB "$ " "getconf GNU_LIBC_VERSION"
105 .RB "$ " "./a.out 1 10000 100"
106 ============ Before allocating blocks ============
111 <total type="fast" count="0" size="0"/>
112 <total type="rest" count="0" size="0"/>
113 <system type="current" size="135168"/>
114 <system type="max" size="135168"/>
115 <aspace type="total" size="135168"/>
116 <aspace type="mprotect" size="135168"/>
118 <total type="fast" count="0" size="0"/>
119 <total type="rest" count="0" size="0"/>
120 <system type="current" size="135168"/>
121 <system type="max" size="135168"/>
122 <aspace type="total" size="135168"/>
123 <aspace type="mprotect" size="135168"/>
126 ============ After allocating blocks ============
131 <total type="fast" count="0" size="0"/>
132 <total type="rest" count="0" size="0"/>
133 <system type="current" size="1081344"/>
134 <system type="max" size="1081344"/>
135 <aspace type="total" size="1081344"/>
136 <aspace type="mprotect" size="1081344"/>
141 <total type="fast" count="0" size="0"/>
142 <total type="rest" count="0" size="0"/>
143 <system type="current" size="1032192"/>
144 <system type="max" size="1032192"/>
145 <aspace type="total" size="1032192"/>
146 <aspace type="mprotect" size="1032192"/>
148 <total type="fast" count="0" size="0"/>
149 <total type="rest" count="0" size="0"/>
150 <system type="current" size="2113536"/>
151 <system type="max" size="2113536"/>
152 <aspace type="total" size="2113536"/>
153 <aspace type="mprotect" size="2113536"/>
166 static size_t blockSize;
167 static int numThreads, numBlocks;
169 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \\
173 thread_func(void *arg)
178 /* The multiplier \(aq(2 + tn)\(aq ensures that each thread (including
179 the main thread) allocates a different amount of memory */
181 for (j = 0; j < numBlocks; j++)
182 if (malloc(blockSize * (2 + tn)) == NULL)
183 errExit("malloc\-thread");
185 sleep(100); /* Sleep until main thread terminates */
190 main(int argc, char *argv[])
192 int j, tn, sleepTime;
197 "%s num\-threads num\-blocks block\-size [sleep\-time]\\n",
202 numThreads = atoi(argv[1]);
203 numBlocks = atoi(argv[2]);
204 blockSize = atoi(argv[3]);
205 sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
207 thr = calloc(numThreads, sizeof(pthread_t));
211 printf("============ Before allocating blocks ============\\n");
212 malloc_info(0, stdout);
214 /* Create threads that allocate different amounts of memory */
216 for (tn = 0; tn < numThreads; tn++) {
217 errno = pthread_create(&thr[tn], NULL, thread_func,
220 errExit("pthread_create");
222 /* If we add a sleep interval after the start\-up of each
223 thread, the threads likely won\(aqt contend for malloc
224 mutexes, and therefore additional arenas won\(aqt be
225 allocated (see malloc(3)). */
231 /* The main thread also allocates some memory */
233 for (j = 0; j < numBlocks; j++)
234 if (malloc(blockSize) == NULL)
237 sleep(2); /* Give all threads a chance to
238 complete allocations */
240 printf("\\n============ After allocating blocks ============\\n");
241 malloc_info(0, stdout);
250 .BR malloc_stats (3),