1 1.1 christos This document summarizes the common approaches for performance fine tuning with 2 1.1.1.2 christos jemalloc (as of 5.3.0). The default configuration of jemalloc tends to work 3 1.1 christos reasonably well in practice, and most applications should not have to tune any 4 1.1 christos options. However, in order to cover a wide range of applications and avoid 5 1.1 christos pathological cases, the default setting is sometimes kept conservative and 6 1.1 christos suboptimal, even for many common workloads. When jemalloc is properly tuned for 7 1.1 christos a specific application / workload, it is common to improve system level metrics 8 1.1 christos by a few percent, or make favorable trade-offs. 9 1.1 christos 10 1.1 christos 11 1.1 christos ## Notable runtime options for performance tuning 12 1.1 christos 13 1.1 christos Runtime options can be set via 14 1.1 christos [malloc_conf](http://jemalloc.net/jemalloc.3.html#tuning). 15 1.1 christos 16 1.1 christos * [background_thread](http://jemalloc.net/jemalloc.3.html#background_thread) 17 1.1 christos 18 1.1 christos Enabling jemalloc background threads generally improves the tail latency for 19 1.1 christos application threads, since unused memory purging is shifted to the dedicated 20 1.1 christos background threads. In addition, unintended purging delay caused by 21 1.1 christos application inactivity is avoided with background threads. 22 1.1 christos 23 1.1 christos Suggested: `background_thread:true` when jemalloc managed threads can be 24 1.1 christos allowed. 25 1.1 christos 26 1.1 christos * [metadata_thp](http://jemalloc.net/jemalloc.3.html#opt.metadata_thp) 27 1.1 christos 28 1.1 christos Allowing jemalloc to utilize transparent huge pages for its internal 29 1.1 christos metadata usually reduces TLB misses significantly, especially for programs 30 1.1 christos with large memory footprint and frequent allocation / deallocation 31 1.1 christos activities. Metadata memory usage may increase due to the use of huge 32 1.1 christos pages. 33 1.1 christos 34 1.1 christos Suggested for allocation intensive programs: `metadata_thp:auto` or 35 1.1 christos `metadata_thp:always`, which is expected to improve CPU utilization at a 36 1.1 christos small memory cost. 37 1.1 christos 38 1.1 christos * [dirty_decay_ms](http://jemalloc.net/jemalloc.3.html#opt.dirty_decay_ms) and 39 1.1 christos [muzzy_decay_ms](http://jemalloc.net/jemalloc.3.html#opt.muzzy_decay_ms) 40 1.1 christos 41 1.1 christos Decay time determines how fast jemalloc returns unused pages back to the 42 1.1 christos operating system, and therefore provides a fairly straightforward trade-off 43 1.1 christos between CPU and memory usage. Shorter decay time purges unused pages faster 44 1.1 christos to reduces memory usage (usually at the cost of more CPU cycles spent on 45 1.1 christos purging), and vice versa. 46 1.1 christos 47 1.1 christos Suggested: tune the values based on the desired trade-offs. 48 1.1 christos 49 1.1 christos * [narenas](http://jemalloc.net/jemalloc.3.html#opt.narenas) 50 1.1 christos 51 1.1 christos By default jemalloc uses multiple arenas to reduce internal lock contention. 52 1.1 christos However high arena count may also increase overall memory fragmentation, 53 1.1 christos since arenas manage memory independently. When high degree of parallelism 54 1.1 christos is not expected at the allocator level, lower number of arenas often 55 1.1 christos improves memory usage. 56 1.1 christos 57 1.1 christos Suggested: if low parallelism is expected, try lower arena count while 58 1.1 christos monitoring CPU and memory usage. 59 1.1 christos 60 1.1 christos * [percpu_arena](http://jemalloc.net/jemalloc.3.html#opt.percpu_arena) 61 1.1 christos 62 1.1 christos Enable dynamic thread to arena association based on running CPU. This has 63 1.1 christos the potential to improve locality, e.g. when thread to CPU affinity is 64 1.1 christos present. 65 1.1 christos 66 1.1 christos Suggested: try `percpu_arena:percpu` or `percpu_arena:phycpu` if 67 1.1 christos thread migration between processors is expected to be infrequent. 68 1.1 christos 69 1.1 christos Examples: 70 1.1 christos 71 1.1 christos * High resource consumption application, prioritizing CPU utilization: 72 1.1 christos 73 1.1 christos `background_thread:true,metadata_thp:auto` combined with relaxed decay time 74 1.1 christos (increased `dirty_decay_ms` and / or `muzzy_decay_ms`, 75 1.1 christos e.g. `dirty_decay_ms:30000,muzzy_decay_ms:30000`). 76 1.1 christos 77 1.1 christos * High resource consumption application, prioritizing memory usage: 78 1.1 christos 79 1.1.1.2 christos `background_thread:true,tcache_max:4096` combined with shorter decay time 80 1.1.1.2 christos (decreased `dirty_decay_ms` and / or `muzzy_decay_ms`, 81 1.1 christos e.g. `dirty_decay_ms:5000,muzzy_decay_ms:5000`), and lower arena count 82 1.1 christos (e.g. number of CPUs). 83 1.1 christos 84 1.1 christos * Low resource consumption application: 85 1.1 christos 86 1.1.1.2 christos `narenas:1,tcache_max:1024` combined with shorter decay time (decreased 87 1.1 christos `dirty_decay_ms` and / or `muzzy_decay_ms`,e.g. 88 1.1 christos `dirty_decay_ms:1000,muzzy_decay_ms:0`). 89 1.1 christos 90 1.1 christos * Extremely conservative -- minimize memory usage at all costs, only suitable when 91 1.1 christos allocation activity is very rare: 92 1.1 christos 93 1.1 christos `narenas:1,tcache:false,dirty_decay_ms:0,muzzy_decay_ms:0` 94 1.1 christos 95 1.1 christos Note that it is recommended to combine the options with `abort_conf:true` which 96 1.1 christos aborts immediately on illegal options. 97 1.1 christos 98 1.1 christos ## Beyond runtime options 99 1.1 christos 100 1.1 christos In addition to the runtime options, there are a number of programmatic ways to 101 1.1 christos improve application performance with jemalloc. 102 1.1 christos 103 1.1 christos * [Explicit arenas](http://jemalloc.net/jemalloc.3.html#arenas.create) 104 1.1 christos 105 1.1 christos Manually created arenas can help performance in various ways, e.g. by 106 1.1 christos managing locality and contention for specific usages. For example, 107 1.1 christos applications can explicitly allocate frequently accessed objects from a 108 1.1 christos dedicated arena with 109 1.1 christos [mallocx()](http://jemalloc.net/jemalloc.3.html#MALLOCX_ARENA) to improve 110 1.1 christos locality. In addition, explicit arenas often benefit from individually 111 1.1 christos tuned options, e.g. relaxed [decay 112 1.1 christos time](http://jemalloc.net/jemalloc.3.html#arena.i.dirty_decay_ms) if 113 1.1 christos frequent reuse is expected. 114 1.1 christos 115 1.1 christos * [Extent hooks](http://jemalloc.net/jemalloc.3.html#arena.i.extent_hooks) 116 1.1 christos 117 1.1 christos Extent hooks allow customization for managing underlying memory. One use 118 1.1 christos case for performance purpose is to utilize huge pages -- for example, 119 1.1 christos [HHVM](https://github.com/facebook/hhvm/blob/master/hphp/util/alloc.cpp) 120 1.1 christos uses explicit arenas with customized extent hooks to manage 1GB huge pages 121 1.1 christos for frequently accessed data, which reduces TLB misses significantly. 122 1.1 christos 123 1.1 christos * [Explicit thread-to-arena 124 1.1 christos binding](http://jemalloc.net/jemalloc.3.html#thread.arena) 125 1.1 christos 126 1.1 christos It is common for some threads in an application to have different memory 127 1.1 christos access / allocation patterns. Threads with heavy workloads often benefit 128 1.1 christos from explicit binding, e.g. binding very active threads to dedicated arenas 129 1.1 christos may reduce contention at the allocator level. 130