1 <!doctype html public
"-//w3c//dtd html 4.01 transitional//en">
5 <title>TCMalloc : Thread-Caching Malloc
</title>
6 <link rel=
"stylesheet" href=
"designstyle.css">
7 <style type=
"text/css">
16 <h1>TCMalloc : Thread-Caching Malloc
</h1>
18 <address>Sanjay Ghemawat
</address>
20 <h2><A name=motivation
>Motivation
</A></h2>
22 <p>TCMalloc is faster than the glibc
2.3 malloc (available as a
23 separate library called ptmalloc2) and other mallocs that I have
24 tested. ptmalloc2 takes approximately
300 nanoseconds to execute a
25 malloc/free pair on a
2.8 GHz P4 (for small objects). The TCMalloc
26 implementation takes approximately
50 nanoseconds for the same
27 operation pair. Speed is important for a malloc implementation
28 because if malloc is not fast enough, application writers are inclined
29 to write their own custom free lists on top of malloc. This can lead
30 to extra complexity, and more memory usage unless the application
31 writer is very careful to appropriately size the free lists and
32 scavenge idle objects out of the free list.
</p>
34 <p>TCMalloc also reduces lock contention for multi-threaded programs.
35 For small objects, there is virtually zero contention. For large
36 objects, TCMalloc tries to use fine grained and efficient spinlocks.
37 ptmalloc2 also reduces lock contention by using per-thread arenas but
38 there is a big problem with ptmalloc2's use of per-thread arenas. In
39 ptmalloc2 memory can never move from one arena to another. This can
40 lead to huge amounts of wasted space. For example, in one Google
41 application, the first phase would allocate approximately
300MB of
42 memory for its URL canonicalization data structures. When the first
43 phase finished, a second phase would be started in the same address
44 space. If this second phase was assigned a different arena than the
45 one used by the first phase, this phase would not reuse any of the
46 memory left after the first phase and would add another
300MB to the
47 address space. Similar memory blowup problems were also noticed in
48 other applications.
</p>
50 <p>Another benefit of TCMalloc is space-efficient representation of
51 small objects. For example, N
8-byte objects can be allocated while
52 using space approximately
<code>8N *
1.01</code> bytes. I.e., a
53 one-percent space overhead. ptmalloc2 uses a four-byte header for
54 each object and (I think) rounds up the size to a multiple of
8 bytes
55 and ends up using
<code>16N
</code> bytes.
</p>
58 <h2><A NAME=
"Usage">Usage
</A></h2>
60 <p>To use TCMalloc, just link TCMalloc into your application via the
61 "-ltcmalloc" linker flag.
</p>
63 <p>You can use TCMalloc in applications you didn't compile yourself,
64 by using LD_PRELOAD:
</p>
66 $
LD_PRELOAD=
"/usr/lib/libtcmalloc.so" <binary>
68 <p>LD_PRELOAD is tricky, and we don't necessarily recommend this mode
71 <p>TCMalloc includes a
<A HREF=
"heap_checker.html">heap checker
</A>
72 and
<A HREF=
"heapprofile.html">heap profiler
</A> as well.
</p>
74 <p>If you'd rather link in a version of TCMalloc that does not include
75 the heap profiler and checker (perhaps to reduce binary size for a
76 static binary), you can link in
<code>libtcmalloc_minimal
</code>
80 <h2><A NAME=
"Overview">Overview
</A></h2>
82 <p>TCMalloc assigns each thread a thread-local cache. Small
83 allocations are satisfied from the thread-local cache. Objects are
84 moved from central data structures into a thread-local cache as
85 needed, and periodic garbage collections are used to migrate memory
86 back from a thread-local cache into the central data structures.
</p>
87 <center><img src=
"overview.gif"></center>
89 <p>TCMalloc treats objects with size
<=
32K (
"small" objects)
90 differently from larger objects. Large objects are allocated directly
91 from the central heap using a page-level allocator (a page is a
4K
92 aligned region of memory). I.e., a large object is always
93 page-aligned and occupies an integral number of pages.
</p>
95 <p>A run of pages can be carved up into a sequence of small objects,
96 each equally sized. For example a run of one page (
4K) can be carved
97 up into
32 objects of size
128 bytes each.
</p>
100 <h2><A NAME=
"Small_Object_Allocation">Small Object Allocation
</A></h2>
102 <p>Each small object size maps to one of approximately
60 allocatable
103 size-classes. For example, all allocations in the range
833 to
1024
104 bytes are rounded up to
1024. The size-classes are spaced so that
105 small sizes are separated by
8 bytes, larger sizes by
16 bytes, even
106 larger sizes by
32 bytes, and so forth. The maximal spacing is
107 controlled so that not too much space is wasted when an allocation
108 request falls just past the end of a size class and has to be rounded
109 up to the next class.
</p>
111 <p>A thread cache contains a singly linked list of free objects per
113 <center><img src=
"threadheap.gif"></center>
115 <p>When allocating a small object: (
1) We map its size to the
116 corresponding size-class. (
2) Look in the corresponding free list in
117 the thread cache for the current thread. (
3) If the free list is not
118 empty, we remove the first object from the list and return it. When
119 following this fast path, TCMalloc acquires no locks at all. This
120 helps speed-up allocation significantly because a lock/unlock pair
121 takes approximately
100 nanoseconds on a
2.8 GHz Xeon.
</p>
123 <p>If the free list is empty: (
1) We fetch a bunch of objects from a
124 central free list for this size-class (the central free list is shared
125 by all threads). (
2) Place them in the thread-local free list. (
3)
126 Return one of the newly fetched objects to the applications.
</p>
128 <p>If the central free list is also empty: (
1) We allocate a run of
129 pages from the central page allocator. (
2) Split the run into a set
130 of objects of this size-class. (
3) Place the new objects on the
131 central free list. (
4) As before, move some of these objects to the
132 thread-local free list.
</p>
134 <h3><A NAME=
"Sizing_Thread_Cache_Free_Lists">
135 Sizing Thread Cache Free Lists
</A></h3>
137 <p>It is important to size the thread cache free lists correctly. If
138 the free list is too small, we'll need to go to the central free list
139 too often. If the free list is too big, we'll waste memory as objects
140 sit idle in the free list.
</p>
142 <p>Note that the thread caches are just as important for deallocation
143 as they are for allocation. Without a cache, each deallocation would
144 require moving the memory to the central free list. Also, some threads
145 have asymmetric alloc/free behavior (e.g. producer and consumer threads),
146 so sizing the free list correctly gets trickier.
</p>
148 <p>To size the free lists appropriately, we use a slow-start algorithm
149 to determine the maximum length of each individual free list. As the
150 free list is used more frequently, its maximum length grows. However,
151 if a free list is used more for deallocation than allocation, its
152 maximum length will grow only up to a point where the whole list can
153 be efficiently moved to the central free list at once.
</p>
155 <p>The psuedo-code below illustrates this slow-start algorithm. Note
156 that
<code>num_objects_to_move
</code> is specific to each size class.
157 By moving a list of objects with a well-known length, the central
158 cache can efficiently pass these lists between thread caches. If
159 a thread cache wants fewer than
<code>num_objects_to_move
</code>,
160 the operation on the central free list has linear time complexity.
161 The downside of always using
<code>num_objects_to_move
</code> as
162 the number of objects to transfer to and from the central cache is
163 that it wastes memory in threads that don't need all of those objects.
166 Start each freelist max_length at
1.
170 fetch min(max_length, num_objects_to_move) from central list;
171 if max_length < num_objects_to_move { // slow-start
174 max_length += num_objects_to_move;
179 if length
> max_length {
180 // Don't try to release num_objects_to_move if we don't have that many.
181 release min(max_length, num_objects_to_move) objects to central list
182 if max_length < num_objects_to_move {
183 // Slow-start up to num_objects_to_move.
185 } else if max_length
> num_objects_to_move {
186 // If we consistently go over max_length, shrink max_length.
188 if overages
> kMaxOverages {
189 max_length -= num_objects_to_move;
196 See also the section on
<a href=
"#Garbage_Collection">Garbage Collection
</a>
197 to see how it affects the
<code>max_length
</code>.
199 <h2><A NAME=
"Large_Object_Allocation">Large Object Allocation
</A></h2>
201 <p>A large object size (
> 32K) is rounded up to a page size (
4K)
202 and is handled by a central page heap. The central page heap is again
203 an array of free lists. For
<code>i
< 256</code>, the
204 <code>k
</code>th entry is a free list of runs that consist of
205 <code>k
</code> pages. The
<code>256</code>th entry is a free list of
206 runs that have length
<code>>=
256</code> pages:
</p>
207 <center><img src=
"pageheap.gif"></center>
209 <p>An allocation for
<code>k
</code> pages is satisfied by looking in
210 the
<code>k
</code>th free list. If that free list is empty, we look
211 in the next free list, and so forth. Eventually, we look in the last
212 free list if necessary. If that fails, we fetch memory from the
213 system (using
<code>sbrk
</code>,
<code>mmap
</code>, or by mapping in
214 portions of
<code>/dev/mem
</code>).
</p>
216 <p>If an allocation for
<code>k
</code> pages is satisfied by a run
217 of pages of length
> <code>k
</code>, the remainder of the
218 run is re-inserted back into the appropriate free list in the
222 <h2><A NAME=
"Spans">Spans
</A></h2>
224 <p>The heap managed by TCMalloc consists of a set of pages. A run of
225 contiguous pages is represented by a
<code>Span
</code> object. A span
226 can either be
<em>allocated
</em>, or
<em>free
</em>. If free, the span
227 is one of the entries in a page heap linked-list. If allocated, it is
228 either a large object that has been handed off to the application, or
229 a run of pages that have been split up into a sequence of small
230 objects. If split into small objects, the size-class of the objects
231 is recorded in the span.
</p>
233 <p>A central array indexed by page number can be used to find the span to
234 which a page belongs. For example, span
<em>a
</em> below occupies
2
235 pages, span
<em>b
</em> occupies
1 page, span
<em>c
</em> occupies
5
236 pages and span
<em>d
</em> occupies
3 pages.
</p>
237 <center><img src=
"spanmap.gif"></center>
239 <p>In a
32-bit address space, the central array is represented by a a
240 2-level radix tree where the root contains
32 entries and each leaf
241 contains
2^
15 entries (a
32-bit address spave has
2^
20 4K pages, and
242 the first level of tree divides the
2^
20 pages by
2^
5). This leads to
243 a starting memory usage of
128KB of space (
2^
15*
4 bytes) for the
244 central array, which seems acceptable.
</p>
246 <p>On
64-bit machines, we use a
3-level radix tree.
</p>
249 <h2><A NAME=
"Deallocation">Deallocation
</A></h2>
251 <p>When an object is deallocated, we compute its page number and look
252 it up in the central array to find the corresponding span object. The
253 span tells us whether or not the object is small, and its size-class
254 if it is small. If the object is small, we insert it into the
255 appropriate free list in the current thread's thread cache. If the
256 thread cache now exceeds a predetermined size (
2MB by default), we run
257 a garbage collector that moves unused objects from the thread cache
258 into central free lists.
</p>
260 <p>If the object is large, the span tells us the range of pages covered
261 by the object. Suppose this range is
<code>[p,q]
</code>. We also
262 lookup the spans for pages
<code>p-
1</code> and
<code>q+
1</code>. If
263 either of these neighboring spans are free, we coalesce them with the
264 <code>[p,q]
</code> span. The resulting span is inserted into the
265 appropriate free list in the page heap.
</p>
268 <h2>Central Free Lists for Small Objects
</h2>
270 <p>As mentioned before, we keep a central free list for each
271 size-class. Each central free list is organized as a two-level data
272 structure: a set of spans, and a linked list of free objects per
275 <p>An object is allocated from a central free list by removing the
276 first entry from the linked list of some span. (If all spans have
277 empty linked lists, a suitably sized span is first allocated from the
278 central page heap.)
</p>
280 <p>An object is returned to a central free list by adding it to the
281 linked list of its containing span. If the linked list length now
282 equals the total number of small objects in the span, this span is now
283 completely free and is returned to the page heap.
</p>
286 <h2><A NAME=
"Garbage_Collection">Garbage Collection of Thread Caches
</A></h2>
288 <p>Garbage collecting objects from a thread cache keeps the size of
289 the cache under control and returns unused objects to the central free
290 lists. Some threads need large caches to perform well while others
291 can get by with little or no cache at all. When a thread cache goes
292 over its
<code>max_size
</code>, garbage collection kicks in and then the
293 thread competes with the other threads for a larger cache.
</p>
295 <p>Garbage collection is run only during a deallocation. We walk over
296 all free lists in the cache and move some number of objects from the
297 free list to the corresponding central list.
</p>
299 <p>The number of objects to be moved from a free list is determined
300 using a per-list low-water-mark
<code>L
</code>.
<code>L
</code>
301 records the minimum length of the list since the last garbage
302 collection. Note that we could have shortened the list by
303 <code>L
</code> objects at the last garbage collection without
304 requiring any extra accesses to the central list. We use this past
305 history as a predictor of future accesses and move
<code>L/
2</code>
306 objects from the thread cache free list to the corresponding central
307 free list. This algorithm has the nice property that if a thread
308 stops using a particular size, all objects of that size will quickly
309 move from the thread cache to the central free list where they can be
310 used by other threads.
</p>
312 <p>If a thread consistently deallocates more objects of a certain size
313 than it allocates, this
<code>L/
2</code> behavior will cause at least
314 <code>L/
2</code> objects to always sit in the free list. To avoid
315 wasting memory this way, we shrink the maximum length of the freelist
316 to converge on
<code>num_objects_to_move
</code> (see also
317 <a href=
"#Sizing_Thread_Cache_Free_Lists">Sizing Thread Cache Free Lists
</a>).
321 if (L !=
0 && max_length
> num_objects_to_move) {
322 max_length = max(max_length - num_objects_to_move, num_objects_to_move)
326 <p>The fact that the thread cache went over its
<code>max_size
</code> is
327 an indication that the thread would benefit from a larger cache. Simply
328 increasing
<code>max_size
</code> would use an inordinate amount of memory
329 in programs that have lots of active threads. Developers can bound the
330 memory used with the flag --tcmalloc_max_total_thread_cache_bytes.
</p>
332 <p>Each thread cache starts with a small
<code>max_size
</code>
333 (e.g.
64KB) so that idle threads won't pre-allocate memory they don't
334 need. Each time the cache runs a garbage collection, it will also try
335 to grow its
<code>max_size
</code>. If the sum of the thread cache
336 sizes is less than --tcmalloc_max_total_thread_cache_bytes,
337 <code>max_size
</code> grows easily. If not, thread cache
1 will try
338 to steal from thread cache
2 (picked round-robin) by decreasing thread
339 cache
2's
<code>max_size
</code>. In this way, threads that are more
340 active will steal memory from other threads more often than they are
341 have memory stolen from themselves. Mostly idle threads end up with
342 small caches and active threads end up with big caches. Note that
343 this stealing can cause the sum of the thread cache sizes to be
344 greater than --tcmalloc_max_total_thread_cache_bytes until thread
345 cache
2 deallocates some memory to trigger a garbage collection.
</p>
347 <h2><A NAME=
"performance">Performance Notes
</A></h2>
349 <h3>PTMalloc2 unittest
</h3>
351 <p>The PTMalloc2 package (now part of glibc) contains a unittest
352 program
<code>t-test1.c
</code>. This forks a number of threads and
353 performs a series of allocations and deallocations in each thread; the
354 threads do not communicate other than by synchronization in the memory
357 <p><code>t-test1
</code> (included in
358 <code>tests/tcmalloc/
</code>, and compiled as
359 <code>ptmalloc_unittest1
</code>) was run with a varying numbers of
360 threads (
1-
20) and maximum allocation sizes (
64 bytes -
361 32Kbytes). These tests were run on a
2.4GHz dual Xeon system with
362 hyper-threading enabled, using Linux glibc-
2.3.2 from RedHat
9, with
363 one million operations per thread in each test. In each case, the test
364 was run once normally, and once with
365 <code>LD_PRELOAD=libtcmalloc.so
</code>.
367 <p>The graphs below show the performance of TCMalloc vs PTMalloc2 for
368 several different metrics. Firstly, total operations (millions) per
369 elapsed second vs max allocation size, for varying numbers of
370 threads. The raw data used to generate these graphs (the output of the
371 <code>time
</code> utility) is available in
372 <code>t-test1.times.txt
</code>.
</p>
376 <td><img src=
"tcmalloc-opspersec.vs.size.1.threads.png"></td>
377 <td><img src=
"tcmalloc-opspersec.vs.size.2.threads.png"></td>
378 <td><img src=
"tcmalloc-opspersec.vs.size.3.threads.png"></td>
381 <td><img src=
"tcmalloc-opspersec.vs.size.4.threads.png"></td>
382 <td><img src=
"tcmalloc-opspersec.vs.size.5.threads.png"></td>
383 <td><img src=
"tcmalloc-opspersec.vs.size.8.threads.png"></td>
386 <td><img src=
"tcmalloc-opspersec.vs.size.12.threads.png"></td>
387 <td><img src=
"tcmalloc-opspersec.vs.size.16.threads.png"></td>
388 <td><img src=
"tcmalloc-opspersec.vs.size.20.threads.png"></td>
394 <li> TCMalloc is much more consistently scalable than PTMalloc2 - for
395 all thread counts
>1 it achieves ~
7-
9 million ops/sec for small
396 allocations, falling to ~
2 million ops/sec for larger
397 allocations. The single-thread case is an obvious outlier,
398 since it is only able to keep a single processor busy and hence
399 can achieve fewer ops/sec. PTMalloc2 has a much higher variance
400 on operations/sec - peaking somewhere around
4 million ops/sec
401 for small allocations and falling to
<1 million ops/sec for
404 <li> TCMalloc is faster than PTMalloc2 in the vast majority of
405 cases, and particularly for small allocations. Contention
406 between threads is less of a problem in TCMalloc.
408 <li> TCMalloc's performance drops off as the allocation size
409 increases. This is because the per-thread cache is
410 garbage-collected when it hits a threshold (defaulting to
411 2MB). With larger allocation sizes, fewer objects can be stored
412 in the cache before it is garbage-collected.
414 <li> There is a noticeable drop in TCMalloc's performance at ~
32K
415 maximum allocation size; at larger sizes performance drops less
416 quickly. This is due to the
32K maximum size of objects in the
417 per-thread caches; for objects larger than this TCMalloc
418 allocates from the central page heap.
421 <p>Next, operations (millions) per second of CPU time vs number of
422 threads, for max allocation size
64 bytes -
128 Kbytes.
</p>
426 <td><img src=
"tcmalloc-opspercpusec.vs.threads.64.bytes.png"></td>
427 <td><img src=
"tcmalloc-opspercpusec.vs.threads.256.bytes.png"></td>
428 <td><img src=
"tcmalloc-opspercpusec.vs.threads.1024.bytes.png"></td>
431 <td><img src=
"tcmalloc-opspercpusec.vs.threads.4096.bytes.png"></td>
432 <td><img src=
"tcmalloc-opspercpusec.vs.threads.8192.bytes.png"></td>
433 <td><img src=
"tcmalloc-opspercpusec.vs.threads.16384.bytes.png"></td>
436 <td><img src=
"tcmalloc-opspercpusec.vs.threads.32768.bytes.png"></td>
437 <td><img src=
"tcmalloc-opspercpusec.vs.threads.65536.bytes.png"></td>
438 <td><img src=
"tcmalloc-opspercpusec.vs.threads.131072.bytes.png"></td>
442 <p>Here we see again that TCMalloc is both more consistent and more
443 efficient than PTMalloc2. For max allocation sizes
<32K, TCMalloc
444 typically achieves ~
2-
2.5 million ops per second of CPU time with a
445 large number of threads, whereas PTMalloc achieves generally
0.5-
1
446 million ops per second of CPU time, with a lot of cases achieving much
447 less than this figure. Above
32K max allocation size, TCMalloc drops
448 to
1-
1.5 million ops per second of CPU time, and PTMalloc drops almost
449 to zero for large numbers of threads (i.e. with PTMalloc, lots of CPU
450 time is being burned spinning waiting for locks in the heavily
451 multi-threaded case).
</p>
454 <H2><A NAME=
"runtime">Modifying Runtime Behavior
</A></H2>
456 <p>You can more finely control the behavior of the tcmalloc via
457 environment variables.
</p>
459 <p>Generally useful flags:
</p>
461 <table frame=box rules=sides cellpadding=
5 width=
100%
>
464 <td><code>TCMALLOC_SAMPLE_PARAMETER
</code></td>
467 The approximate gap between sampling actions. That is, we
468 take one sample approximately once every
469 <code>tcmalloc_sample_parmeter
</code> bytes of allocation.
470 This sampled heap information is available via
471 <code>MallocExtension::GetHeapSample()
</code> or
472 <code>MallocExtension::ReadStackTraces()
</code>. A reasonable
478 <td><code>TCMALLOC_RELEASE_RATE
</code></td>
479 <td>default:
1.0</td>
481 Rate at which we release unused memory to the system, via
482 <code>madvise(MADV_DONTNEED)
</code>, on systems that support
483 it. Zero means we never release memory back to the system.
484 Increase this flag to return memory faster; decrease it
485 to return memory slower. Reasonable rates are in the
491 <td><code>TCMALLOC_LARGE_ALLOC_REPORT_THRESHOLD
</code></td>
492 <td>default:
1073741824</td>
494 Allocations larger than this value cause a stack trace to be
495 dumped to stderr. The threshold for dumping stack traces is
496 increased by a factor of
1.125 every time we print a message so
497 that the threshold automatically goes up by a factor of ~
1000
498 every
60 messages. This bounds the amount of extra logging
499 generated by this flag. Default value of this flag is very large
500 and therefore you should see no extra logging unless the flag is
506 <td><code>TCMALLOC_MAX_TOTAL_THREAD_CACHE_BYTES
</code></td>
507 <td>default:
16777216</td>
509 Bound on the total amount of bytes allocated to thread caches. This
510 bound is not strict, so it is possible for the cache to go over this
511 bound in certain circumstances. This value defaults to
16MB. For
512 applications with many threads, this may not be a large enough cache,
513 which can affect performance. If you suspect your application is not
514 scaling to many threads due to lock contention in TCMalloc, you can
515 try increasing this value. This may improve performance, at a cost
516 of extra memory use by TCMalloc. See
<a href=
"#Garbage_Collection">
517 Garbage Collection
</a> for more details.
523 <p>Advanced
"tweaking" flags, that control more precisely how tcmalloc
524 tries to allocate memory from the kernel.
</p>
526 <table frame=box rules=sides cellpadding=
5 width=
100%
>
529 <td><code>TCMALLOC_SKIP_MMAP
</code></td>
530 <td>default: false
</td>
532 If true, do not try to use
<code>mmap
</code> to obtain memory
538 <td><code>TCMALLOC_SKIP_SBRK
</code></td>
539 <td>default: false
</td>
541 If true, do not try to use
<code>sbrk
</code> to obtain memory
547 <td><code>TCMALLOC_DEVMEM_START
</code></td>
550 Physical memory starting location in MB for
<code>/dev/mem
</code>
551 allocation. Setting this to
0 disables
<code>/dev/mem
</code>
557 <td><code>TCMALLOC_DEVMEM_LIMIT
</code></td>
560 Physical memory limit location in MB for
<code>/dev/mem
</code>
561 allocation. Setting this to
0 means no limit.
566 <td><code>TCMALLOC_DEVMEM_DEVICE
</code></td>
567 <td>default: /dev/mem
</td>
569 Device to use for allocating unmanaged memory.
574 <td><code>TCMALLOC_MEMFS_MALLOC_PATH
</code></td>
577 If set, specify a path where hugetlbfs or tmpfs is mounted.
578 This may allow for speedier allocations.
583 <td><code>TCMALLOC_MEMFS_LIMIT_MB
</code></td>
586 Limit total memfs allocation size to specified number of MB.
592 <td><code>TCMALLOC_MEMFS_ABORT_ON_FAIL
</code></td>
593 <td>default: false
</td>
595 If true, abort() whenever memfs_malloc fails to satisfy an allocation.
600 <td><code>TCMALLOC_MEMFS_IGNORE_MMAP_FAIL
</code></td>
601 <td>default: false
</td>
603 If true, ignore failures from mmap.
608 <td><code>TCMALLOC_MEMFS_MAP_PRVIATE
</code></td>
609 <td>default: false
</td>
611 If true, use MAP_PRIVATE when mapping via memfs, not MAP_SHARED.
618 <H2><A NAME=
"compiletime">Modifying Behavior In Code
</A></H2>
620 <p>The
<code>MallocExtension
</code> class, in
621 <code>malloc_extension.h
</code>, provides a few knobs that you can
622 tweak in your program, to affect tcmalloc's behavior.
</p>
624 <h3>Releasing Memory Back to the System
</h3>
626 <p>By default, tcmalloc will release no-longer-used memory back to the
627 kernel gradually, over time. The
<a
628 href=
"#runtime">tcmalloc_release_rate
</a> flag controls how quickly
629 this happens. You can also force a release at a given point in the
630 progam execution like so:
</p>
632 MallocExtension::instance()-
>ReleaseFreeMemory();
635 <p>You can also call
<code>SetMemoryReleaseRate()
</code> to change the
636 <code>tcmalloc_release_rate
</code> value at runtime, or
637 <code>GetMemoryReleaseRate
</code> to see what the current release rate
640 <h3>Memory Introspection
</h3>
642 <p>There are several routines for getting a human-readable form of the
643 current memory usage:
</p>
645 MallocExtension::instance()-
>GetStats(buffer, buffer_length);
646 MallocExtension::instance()-
>GetHeapSample(&string);
647 MallocExtension::instance()-
>GetHeapGrowthStacks(&string);
650 <p>The last two create files in the same format as the heap-profiler,
651 and can be passed as data files to pprof. The first is human-readable
652 and is meant for debugging.
</p>
654 <h3>Generic Tcmalloc Status
</h3>
656 <p>TCMalloc has support for setting and retrieving arbitrary
659 MallocExtension::instance()-
>SetNumericProperty(property_name, value);
660 MallocExtension::instance()-
>GetNumericProperty(property_name, &value);
663 <p>It is possible for an application to set and get these properties,
664 but the most useful is when a library sets the properties so the
665 application can read them. Here are the properties TCMalloc defines;
666 you can access them with a call like
667 <code>MallocExtension::instance()-
>GetNumericProperty(
"generic.heap_size",
670 <table frame=box rules=sides cellpadding=
5 width=
100%
>
673 <td><code>generic.current_allocated_bytes
</code></td>
675 Number of bytes used by the application. This will not typically
676 match the memory use reported by the OS, because it does not
677 include TCMalloc overhead or memory fragmentation.
682 <td><code>generic.heap_size
</code></td>
684 Bytes of system memory reserved by TCMalloc.
689 <td><code>tcmalloc.pageheap_free_bytes
</code></td>
691 Number of bytes in free, mapped pages in page heap. These bytes
692 can be used to fulfill allocation requests. They always count
693 towards virtual memory usage, and unless the underlying memory is
694 swapped out by the OS, they also count towards physical memory
700 <td><code>tcmalloc.pageheap_unmapped_bytes
</code></td>
702 Number of bytes in free, unmapped pages in page heap. These are
703 bytes that have been released back to the OS, possibly by one of
704 the MallocExtension
"Release" calls. They can be used to fulfill
705 allocation requests, but typically incur a page fault. They
706 always count towards virtual memory usage, and depending on the
707 OS, typically do not count towards physical memory usage.
712 <td><code>tcmalloc.slack_bytes
</code></td>
714 Sum of pageheap_free_bytes and pageheap_unmapped_bytes. Provided
715 for backwards compatibility only. Do not use.
720 <td><code>tcmalloc.max_total_thread_cache_bytes
</code></td>
722 A limit to how much memory TCMalloc dedicates for small objects.
723 Higher numbers trade off more memory use for -- in some situations
724 -- improved efficiency.
729 <td><code>tcmalloc.current_total_thread_cache_bytes
</code></td>
731 A measure of some of the memory TCMalloc is using (for
738 <h2><A NAME=
"caveats">Caveats
</A></h2>
740 <p>For some systems, TCMalloc may not work correctly with
741 applications that aren't linked against
<code>libpthread.so
</code> (or
742 the equivalent on your OS). It should work on Linux using glibc
2.3,
743 but other OS/libc combinations have not been tested.
</p>
745 <p>TCMalloc may be somewhat more memory hungry than other mallocs,
746 (but tends not to have the huge blowups that can happen with other
747 mallocs). In particular, at startup TCMalloc allocates approximately
748 240KB of internal memory.
</p>
750 <p>Don't try to load TCMalloc into a running binary (e.g., using JNI
751 in Java programs). The binary will have allocated some objects using
752 the system malloc, and may try to pass them to TCMalloc for
753 deallocation. TCMalloc will not be able to handle such objects.
</p>
757 <address>Sanjay Ghemawat, Paul Menage
<br>
758 <!-- Created: Tue Dec 19 10:43:14 PST 2000 -->
760 Last modified: Sat Feb
24 13:
11:
38 PST
2007 (csilvers)