1MALLOC_INFO(3)             Linux Programmer's Manual            MALLOC_INFO(3)
2
3
4

NAME

6       malloc_info - export malloc state to a stream
7

SYNOPSIS

9       #include <malloc.h>
10
11       int malloc_info(int options, FILE *fp);
12

DESCRIPTION

14       The  malloc_info()  function  exports  an XML string that describes the
15       current state of the memory-allocation implementation  in  the  caller.
16       The  string  is  printed  on  the  file stream fp.  The exported string
17       includes information about all arenas (see malloc(3)).
18
19       As currently implemented, options must be zero.
20

RETURN VALUE

22       On success, malloc_info() returns 0; on  error,  it  returns  -1,  with
23       errno set to indicate the cause.
24

ERRORS

26       EINVAL options was nonzero.
27

VERSIONS

29       malloc_info() was added to glibc in version 2.10.
30

CONFORMING TO

32       This function is a GNU extension.
33

NOTES

35       The  memory-allocation information is provided as an XML string (rather
36       than a C structure)  because  the  information  may  change  over  time
37       (according  to  changes  in the underlying implementation).  The output
38       XML string includes a version field.
39
40       The open_memstream(3) function can be used to send the output  of  mal‐
41       loc_info() directly into a buffer in memory, rather than to a file.
42
43       The  malloc_info() function is designed to address deficiencies in mal‐
44       loc_stats(3) and mallinfo(3).
45

EXAMPLE

47       The program below takes up to four command-line arguments, of which the
48       first  three are mandatory.  The first argument specifies the number of
49       threads that the program should create.  All of the threads,  including
50       the  main  thread, allocate the number of blocks of memory specified by
51       the second argument.  The third  argument  controls  the  size  of  the
52       blocks  to  be allocated.  The main thread creates blocks of this size,
53       the second thread created by the program allocates blocks of twice this
54       size,  the  third thread allocates blocks of three times this size, and
55       so on.
56
57       The program calls malloc_info() twice to display the  memory-allocation
58       state.   The  first  call takes place before any threads are created or
59       memory allocated.  The second call is performed after all threads  have
60       allocated memory.
61
62       In  the  following example, the command-line arguments specify the cre‐
63       ation of one additional thread, and both the main thread and the  addi‐
64       tional  thread  allocate  10000  blocks of memory.  After the blocks of
65       memory have been allocated, malloc_info() shows the state of two  allo‐
66       cation arenas.
67
68           $ getconf GNU_LIBC_VERSION
69           glibc 2.13
70           $ ./a.out 1 10000 100
71           ============ Before allocating blocks ============
72           <malloc version="1">
73           <heap nr="0">
74           <sizes>
75           </sizes>
76           <total type="fast" count="0" size="0"/>
77           <total type="rest" count="0" size="0"/>
78           <system type="current" size="135168"/>
79           <system type="max" size="135168"/>
80           <aspace type="total" size="135168"/>
81           <aspace type="mprotect" size="135168"/>
82           </heap>
83           <total type="fast" count="0" size="0"/>
84           <total type="rest" count="0" size="0"/>
85           <system type="current" size="135168"/>
86           <system type="max" size="135168"/>
87           <aspace type="total" size="135168"/>
88           <aspace type="mprotect" size="135168"/>
89           </malloc>
90
91           ============ After allocating blocks ============
92           <malloc version="1">
93           <heap nr="0">
94           <sizes>
95           </sizes>
96           <total type="fast" count="0" size="0"/>
97           <total type="rest" count="0" size="0"/>
98           <system type="current" size="1081344"/>
99           <system type="max" size="1081344"/>
100           <aspace type="total" size="1081344"/>
101           <aspace type="mprotect" size="1081344"/>
102           </heap>
103           <heap nr="1">
104           <sizes>
105           </sizes>
106           <total type="fast" count="0" size="0"/>
107           <total type="rest" count="0" size="0"/>
108           <system type="current" size="1032192"/>
109           <system type="max" size="1032192"/>
110           <aspace type="total" size="1032192"/>
111           <aspace type="mprotect" size="1032192"/>
112           </heap>
113           <total type="fast" count="0" size="0"/>
114           <total type="rest" count="0" size="0"/>
115           <system type="current" size="2113536"/>
116           <system type="max" size="2113536"/>
117           <aspace type="total" size="2113536"/>
118           <aspace type="mprotect" size="2113536"/>
119           </malloc>
120
121   Program source
122       #include <unistd.h>
123       #include <stdlib.h>
124       #include <pthread.h>
125       #include <malloc.h>
126       #include <errno.h>
127
128       static size_t blockSize;
129       static int numThreads, numBlocks;
130
131       #define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
132                               } while (0)
133
134       static void *
135       thread_func(void *arg)
136       {
137           int j;
138           int tn = (int) arg;
139
140           /* The multiplier '(2 + tn)' ensures that each thread (including
141              the main thread) allocates a different amount of memory */
142
143           for (j = 0; j < numBlocks; j++)
144               if (malloc(blockSize * (2 + tn)) == NULL)
145                   errExit("malloc-thread");
146
147           sleep(100);         /* Sleep until main thread terminates */
148           return NULL;
149       }
150
151       int
152       main(int argc, char *argv[])
153       {
154           int j, tn, sleepTime;
155           pthread_t *thr;
156
157           if (argc < 4) {
158               fprintf(stderr,
159                       "%s num-threads num-blocks block-size [sleep-time]\n",
160                       argv[0]);
161               exit(EXIT_FAILURE);
162           }
163
164           numThreads = atoi(argv[1]);
165           numBlocks = atoi(argv[2]);
166           blockSize = atoi(argv[3]);
167           sleepTime = (argc > 4) ? atoi(argv[4]) : 0;
168
169           thr = calloc(numThreads, sizeof(pthread_t));
170           if (thr == NULL)
171               errExit("calloc");
172
173           printf("============ Before allocating blocks ============\n");
174           malloc_info(0, stdout);
175
176           /* Create threads that allocate different amounts of memory */
177
178           for (tn = 0; tn < numThreads; tn++) {
179               errno = pthread_create(&thr[tn], NULL, thread_func,
180                                      (void *) tn);
181               if (errno != 0)
182                   errExit("pthread_create");
183
184               /* If we add a sleep interval after the start-up of each
185                  thread, the threads likely won't contend for malloc
186                  mutexes, and therefore additional arenas won't be
187                  allocated (see malloc(3)). */
188
189               if (sleepTime > 0)
190                   sleep(sleepTime);
191           }
192
193           /* The main thread also allocates some memory */
194
195           for (j = 0; j < numBlocks; j++)
196               if (malloc(blockSize) == NULL)
197                   errExit("malloc");
198
199           sleep(2);           /* Give all threads a chance to
200                                  complete allocations */
201
202           printf("\n============ After allocating blocks ============\n");
203           malloc_info(0, stdout);
204
205           exit(EXIT_SUCCESS);
206       }
207

SEE ALSO

209       mallinfo(3), malloc(3), malloc_stats(3), mallopt(3), open_memstream(3)
210

COLOPHON

212       This  page  is  part of release 3.53 of the Linux man-pages project.  A
213       description of the project, and information about reporting  bugs,  can
214       be found at http://www.kernel.org/doc/man-pages/.
215
216
217
218GNU                               2013-04-19                    MALLOC_INFO(3)
Impressum