2
0

tcmalloc.html 30 KB

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