<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><div>On Nov 14, 2013, at 1:17 PM, Nikhil Bhatia <<a href="mailto:nbhatia@vmware.com">nbhatia@vmware.com</a>> wrote:</div><blockquote type="cite">I am observing a huge gap between the "total allocated" & <br>"active" counts in the jemalloc stats. The "active" & "mapped"<br>correctly point to the RSS and VIRT counters in top. Below<br>is a snippet of the stats output. <br><br>How should I infer this gap? Is this the fragmentation caused<br>by the chunk metadata & unused dirty pages?</blockquote><div><br></div><div>The gap is due to external fragmentation of small object page runs. I computed per size class fragmentation and overall blame for the fragmented memory:</div><div><br></div><div><div><font face="Courier New">bin<span class="Apple-tab-span" style="white-space:pre"> </span>size<span class="Apple-tab-span" style="white-space:pre"> </span>regs<span class="Apple-tab-span" style="white-space:pre"> </span>pgs<span class="Apple-tab-span" style="white-space:pre"> </span>allocated<span class="Apple-tab-span" style="white-space:pre"> </span>cur runs<span class="Apple-tab-span" style="white-space:pre"> </span>% of small<span class="Apple-tab-span" style="white-space:pre"> </span></font><span class="Apple-tab-span" style="font-family: 'Courier New'; white-space: pre; "> </span><span style="font-family: 'Courier New'; ">% of blame</span></div><div><font face="Courier New"><span class="Apple-tab-span" style="white-space:pre"> </span>utilization<span class="Apple-tab-span" style="white-space:pre"> </span></font><span style="font-family: 'Courier New'; ">frag memory</span></div><div><font face="Courier New">0<span class="Apple-tab-span" style="white-space:pre"> </span>8<span class="Apple-tab-span" style="white-space:pre"> </span>501<span class="Apple-tab-span" style="white-space:pre"> </span>1<span class="Apple-tab-span" style="white-space:pre"> </span>50937728<span class="Apple-tab-span" style="white-space:pre"> </span>40745<span class="Apple-tab-span" style="white-space:pre"> </span>31%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>112368232<span class="Apple-tab-span" style="white-space:pre"> </span>4%</font></div><div><font face="Courier New">1<span class="Apple-tab-span" style="white-space:pre"> </span>16<span class="Apple-tab-span" style="white-space:pre"> </span>252<span class="Apple-tab-span" style="white-space:pre"> </span>1<span class="Apple-tab-span" style="white-space:pre"> </span>77020144<span class="Apple-tab-span" style="white-space:pre"> </span>21604<span class="Apple-tab-span" style="white-space:pre"> </span>88%<span class="Apple-tab-span" style="white-space:pre"> </span>2%<span class="Apple-tab-span" style="white-space:pre"> </span>10087184<span class="Apple-tab-span" style="white-space:pre"> </span>0%</font></div><div><font face="Courier New">2<span class="Apple-tab-span" style="white-space:pre"> </span>32<span class="Apple-tab-span" style="white-space:pre"> </span>126<span class="Apple-tab-span" style="white-space:pre"> </span>1<span class="Apple-tab-span" style="white-space:pre"> </span>429852096<span class="Apple-tab-span" style="white-space:pre"> </span>231731<span class="Apple-tab-span" style="white-space:pre"> </span>46%<span class="Apple-tab-span" style="white-space:pre"> </span>12%<span class="Apple-tab-span" style="white-space:pre"> </span>504487296<span class="Apple-tab-span" style="white-space:pre"> </span>20%</font></div><div><font face="Courier New">3<span class="Apple-tab-span" style="white-space:pre"> </span>48<span class="Apple-tab-span" style="white-space:pre"> </span>84<span class="Apple-tab-span" style="white-space:pre"> </span>1<span class="Apple-tab-span" style="white-space:pre"> </span>774254160<span class="Apple-tab-span" style="white-space:pre"> </span>344983<span class="Apple-tab-span" style="white-space:pre"> </span>56%<span class="Apple-tab-span" style="white-space:pre"> </span>22%<span class="Apple-tab-span" style="white-space:pre"> </span>616717296<span class="Apple-tab-span" style="white-space:pre"> </span>24%</font></div><div><font face="Courier New">4<span class="Apple-tab-span" style="white-space:pre"> </span>64<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>1<span class="Apple-tab-span" style="white-space:pre"> </span>270561344<span class="Apple-tab-span" style="white-space:pre"> </span>102283<span class="Apple-tab-span" style="white-space:pre"> </span>66%<span class="Apple-tab-span" style="white-space:pre"> </span>8%<span class="Apple-tab-span" style="white-space:pre"> </span>141843712<span class="Apple-tab-span" style="white-space:pre"> </span>6%</font></div><div><font face="Courier New">5<span class="Apple-tab-span" style="white-space:pre"> </span>80<span class="Apple-tab-span" style="white-space:pre"> </span>50<span class="Apple-tab-span" style="white-space:pre"> </span>1<span class="Apple-tab-span" style="white-space:pre"> </span>526179760<span class="Apple-tab-span" style="white-space:pre"> </span>163248<span class="Apple-tab-span" style="white-space:pre"> </span>81%<span class="Apple-tab-span" style="white-space:pre"> </span>15%<span class="Apple-tab-span" style="white-space:pre"> </span>126812240<span class="Apple-tab-span" style="white-space:pre"> </span>5%</font></div><div><font face="Courier New">6<span class="Apple-tab-span" style="white-space:pre"> </span>96<span class="Apple-tab-span" style="white-space:pre"> </span>84<span class="Apple-tab-span" style="white-space:pre"> </span>2<span class="Apple-tab-span" style="white-space:pre"> </span>66918048<span class="Apple-tab-span" style="white-space:pre"> </span>20469<span class="Apple-tab-span" style="white-space:pre"> </span>41%<span class="Apple-tab-span" style="white-space:pre"> </span>2%<span class="Apple-tab-span" style="white-space:pre"> </span>98143968<span class="Apple-tab-span" style="white-space:pre"> </span>4%</font></div><div><font face="Courier New">7<span class="Apple-tab-span" style="white-space:pre"> </span>112<span class="Apple-tab-span" style="white-space:pre"> </span>72<span class="Apple-tab-span" style="white-space:pre"> </span>2<span class="Apple-tab-span" style="white-space:pre"> </span>141823360<span class="Apple-tab-span" style="white-space:pre"> </span>31895<span class="Apple-tab-span" style="white-space:pre"> </span>55%<span class="Apple-tab-span" style="white-space:pre"> </span>4%<span class="Apple-tab-span" style="white-space:pre"> </span>115377920<span class="Apple-tab-span" style="white-space:pre"> </span>4%</font></div><div><font face="Courier New">8<span class="Apple-tab-span" style="white-space:pre"> </span>128<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>2<span class="Apple-tab-span" style="white-space:pre"> </span>117911808<span class="Apple-tab-span" style="white-space:pre"> </span>22666<span class="Apple-tab-span" style="white-space:pre"> </span>65%<span class="Apple-tab-span" style="white-space:pre"> </span>3%<span class="Apple-tab-span" style="white-space:pre"> </span>64866816<span class="Apple-tab-span" style="white-space:pre"> </span>3%</font></div><div><font face="Courier New">9<span class="Apple-tab-span" style="white-space:pre"> </span>160<span class="Apple-tab-span" style="white-space:pre"> </span>51<span class="Apple-tab-span" style="white-space:pre"> </span>2<span class="Apple-tab-span" style="white-space:pre"> </span>104119200<span class="Apple-tab-span" style="white-space:pre"> </span>22748<span class="Apple-tab-span" style="white-space:pre"> </span>56%<span class="Apple-tab-span" style="white-space:pre"> </span>3%<span class="Apple-tab-span" style="white-space:pre"> </span>81504480<span class="Apple-tab-span" style="white-space:pre"> </span>3%</font></div><div><font face="Courier New">10<span class="Apple-tab-span" style="white-space:pre"> </span>192<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>3<span class="Apple-tab-span" style="white-space:pre"> </span>178081344<span class="Apple-tab-span" style="white-space:pre"> </span>20630<span class="Apple-tab-span" style="white-space:pre"> </span>71%<span class="Apple-tab-span" style="white-space:pre"> </span>5%<span class="Apple-tab-span" style="white-space:pre"> </span>71459136<span class="Apple-tab-span" style="white-space:pre"> </span>3%</font></div><div><font face="Courier New">11<span class="Apple-tab-span" style="white-space:pre"> </span>224<span class="Apple-tab-span" style="white-space:pre"> </span>72<span class="Apple-tab-span" style="white-space:pre"> </span>4<span class="Apple-tab-span" style="white-space:pre"> </span>65155104<span class="Apple-tab-span" style="white-space:pre"> </span>5327<span class="Apple-tab-span" style="white-space:pre"> </span>76%<span class="Apple-tab-span" style="white-space:pre"> </span>2%<span class="Apple-tab-span" style="white-space:pre"> </span>20758752<span class="Apple-tab-span" style="white-space:pre"> </span>1%</font></div><div><font face="Courier New">12<span class="Apple-tab-span" style="white-space:pre"> </span>256<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>4<span class="Apple-tab-span" style="white-space:pre"> </span>48990208<span class="Apple-tab-span" style="white-space:pre"> </span>7009<span class="Apple-tab-span" style="white-space:pre"> </span>43%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>64050944<span class="Apple-tab-span" style="white-space:pre"> </span>2%</font></div><div><font face="Courier New">13<span class="Apple-tab-span" style="white-space:pre"> </span>320<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>5<span class="Apple-tab-span" style="white-space:pre"> </span>99602240<span class="Apple-tab-span" style="white-space:pre"> </span>10444<span class="Apple-tab-span" style="white-space:pre"> </span>47%<span class="Apple-tab-span" style="white-space:pre"> </span>3%<span class="Apple-tab-span" style="white-space:pre"> </span>110948800<span class="Apple-tab-span" style="white-space:pre"> </span>4%</font></div><div><font face="Courier New">14<span class="Apple-tab-span" style="white-space:pre"> </span>384<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>6<span class="Apple-tab-span" style="white-space:pre"> </span>22376448<span class="Apple-tab-span" style="white-space:pre"> </span>1897<span class="Apple-tab-span" style="white-space:pre"> </span>49%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>23515776<span class="Apple-tab-span" style="white-space:pre"> </span>1%</font></div><div><font face="Courier New">15<span class="Apple-tab-span" style="white-space:pre"> </span>448<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>7<span class="Apple-tab-span" style="white-space:pre"> </span>19032384<span class="Apple-tab-span" style="white-space:pre"> </span>2290<span class="Apple-tab-span" style="white-space:pre"> </span>29%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>45600576<span class="Apple-tab-span" style="white-space:pre"> </span>2%</font></div><div><font face="Courier New">16<span class="Apple-tab-span" style="white-space:pre"> </span>512<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>8<span class="Apple-tab-span" style="white-space:pre"> </span>83511808<span class="Apple-tab-span" style="white-space:pre"> </span>4852<span class="Apple-tab-span" style="white-space:pre"> </span>53%<span class="Apple-tab-span" style="white-space:pre"> </span>2%<span class="Apple-tab-span" style="white-space:pre"> </span>72994304<span class="Apple-tab-span" style="white-space:pre"> </span>3%</font></div><div><font face="Courier New">17<span class="Apple-tab-span" style="white-space:pre"> </span>640<span class="Apple-tab-span" style="white-space:pre"> </span>51<span class="Apple-tab-span" style="white-space:pre"> </span>8<span class="Apple-tab-span" style="white-space:pre"> </span>40183040<span class="Apple-tab-span" style="white-space:pre"> </span>2979<span class="Apple-tab-span" style="white-space:pre"> </span>41%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>57051520<span class="Apple-tab-span" style="white-space:pre"> </span>2%</font></div><div><font face="Courier New">18<span class="Apple-tab-span" style="white-space:pre"> </span>768<span class="Apple-tab-span" style="white-space:pre"> </span>47<span class="Apple-tab-span" style="white-space:pre"> </span>9<span class="Apple-tab-span" style="white-space:pre"> </span>17687040<span class="Apple-tab-span" style="white-space:pre"> </span>747<span class="Apple-tab-span" style="white-space:pre"> </span>66%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>9276672<span class="Apple-tab-span" style="white-space:pre"> </span>0%</font></div><div><font face="Courier New">19<span class="Apple-tab-span" style="white-space:pre"> </span>896<span class="Apple-tab-span" style="white-space:pre"> </span>45<span class="Apple-tab-span" style="white-space:pre"> </span>10<span class="Apple-tab-span" style="white-space:pre"> </span>17929856<span class="Apple-tab-span" style="white-space:pre"> </span>730<span class="Apple-tab-span" style="white-space:pre"> </span>61%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>11503744<span class="Apple-tab-span" style="white-space:pre"> </span>0%</font></div><div><font face="Courier New">20<span class="Apple-tab-span" style="white-space:pre"> </span>1024<span class="Apple-tab-span" style="white-space:pre"> </span>63<span class="Apple-tab-span" style="white-space:pre"> </span>16<span class="Apple-tab-span" style="white-space:pre"> </span>226070528<span class="Apple-tab-span" style="white-space:pre"> </span>4142<span class="Apple-tab-span" style="white-space:pre"> </span>85%<span class="Apple-tab-span" style="white-space:pre"> </span>6%<span class="Apple-tab-span" style="white-space:pre"> </span>41138176<span class="Apple-tab-span" style="white-space:pre"> </span>2%</font></div><div><font face="Courier New">21<span class="Apple-tab-span" style="white-space:pre"> </span>1280<span class="Apple-tab-span" style="white-space:pre"> </span>51<span class="Apple-tab-span" style="white-space:pre"> </span>16<span class="Apple-tab-span" style="white-space:pre"> </span>24062720<span class="Apple-tab-span" style="white-space:pre"> </span>786<span class="Apple-tab-span" style="white-space:pre"> </span>47%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>27247360<span class="Apple-tab-span" style="white-space:pre"> </span>1%</font></div><div><font face="Courier New">22<span class="Apple-tab-span" style="white-space:pre"> </span>1536<span class="Apple-tab-span" style="white-space:pre"> </span>42<span class="Apple-tab-span" style="white-space:pre"> </span>16<span class="Apple-tab-span" style="white-space:pre"> </span>9480192<span class="Apple-tab-span" style="white-space:pre"> </span>326<span class="Apple-tab-span" style="white-space:pre"> </span>45%<span class="Apple-tab-span" style="white-space:pre"> </span>0%<span class="Apple-tab-span" style="white-space:pre"> </span>11550720<span class="Apple-tab-span" style="white-space:pre"> </span>0%</font></div><div><font face="Courier New">23<span class="Apple-tab-span" style="white-space:pre"> </span>1792<span class="Apple-tab-span" style="white-space:pre"> </span>38<span class="Apple-tab-span" style="white-space:pre"> </span>17<span class="Apple-tab-span" style="white-space:pre"> </span>3695104<span class="Apple-tab-span" style="white-space:pre"> </span>223<span class="Apple-tab-span" style="white-space:pre"> </span>24%<span class="Apple-tab-span" style="white-space:pre"> </span>0%<span class="Apple-tab-span" style="white-space:pre"> </span>11490304<span class="Apple-tab-span" style="white-space:pre"> </span>0%</font></div><div><font face="Courier New">24<span class="Apple-tab-span" style="white-space:pre"> </span>2048<span class="Apple-tab-span" style="white-space:pre"> </span>65<span class="Apple-tab-span" style="white-space:pre"> </span>33<span class="Apple-tab-span" style="white-space:pre"> </span>42412032<span class="Apple-tab-span" style="white-space:pre"> </span>565<span class="Apple-tab-span" style="white-space:pre"> </span>56%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>32800768<span class="Apple-tab-span" style="white-space:pre"> </span>1%</font></div><div><font face="Courier New">25<span class="Apple-tab-span" style="white-space:pre"> </span>2560<span class="Apple-tab-span" style="white-space:pre"> </span>52<span class="Apple-tab-span" style="white-space:pre"> </span>33<span class="Apple-tab-span" style="white-space:pre"> </span>27392000<span class="Apple-tab-span" style="white-space:pre"> </span>760<span class="Apple-tab-span" style="white-space:pre"> </span>27%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>73779200<span class="Apple-tab-span" style="white-space:pre"> </span>3%</font></div><div><font face="Courier New">26<span class="Apple-tab-span" style="white-space:pre"> </span>3072<span class="Apple-tab-span" style="white-space:pre"> </span>43<span class="Apple-tab-span" style="white-space:pre"> </span>33<span class="Apple-tab-span" style="white-space:pre"> </span>1959936<span class="Apple-tab-span" style="white-space:pre"> </span>65<span class="Apple-tab-span" style="white-space:pre"> </span>23%<span class="Apple-tab-span" style="white-space:pre"> </span>0%<span class="Apple-tab-span" style="white-space:pre"> </span>6626304<span class="Apple-tab-span" style="white-space:pre"> </span>0%</font></div><div><font face="Courier New">27<span class="Apple-tab-span" style="white-space:pre"> </span>3584<span class="Apple-tab-span" style="white-space:pre"> </span>39<span class="Apple-tab-span" style="white-space:pre"> </span>35<span class="Apple-tab-span" style="white-space:pre"> </span>24493056<span class="Apple-tab-span" style="white-space:pre"> </span>235<span class="Apple-tab-span" style="white-space:pre"> </span>75%<span class="Apple-tab-span" style="white-space:pre"> </span>1%<span class="Apple-tab-span" style="white-space:pre"> </span>8354304<span class="Apple-tab-span" style="white-space:pre"> </span>0%</font></div></div><div><br></div><div>utilization = allocated / (size * regs * cur runs)</div><div>% of small = allocated / total allocated</div><div>frag memory = (size * regs * cur runs) - allocated</div><div>% of blame = frag memory / total frag memory</div><div><br></div><div>In order for fragmentation to be that bad, your application has to have a steady state memory usage that is well below its peak usage. In absolute terms, 32- and 48-byte allocations are to blame for nearly half the total fragmentation, and they have utilization (1-fragmentation) of 46% and 56%, respectively.</div><div><br></div><div>The core of the problem is that short-lived and long-lived object allocations are being interleaved even during near-peak memory usage, and when the short-lived objects are freed, the long-lived objects keep entire page runs active, even if almost all neighboring regions have been freed. jemalloc is robust with regard to multiple grow/shrink cycles, in that its layout policies keep fragmentation from increasing from cycle to cycle, but it can do very little about the external fragmentation that exists during the low-usage time periods. If the application accumulates long-lived objects (i.e. each peak is higher than the previous), then the layout policies tend to cause accumulation of long-lived objects in low memory, and fragmentation in high memory is proportionally small. Presumably that's not how your application behaves though.</div><div><br></div><div>You can potentially mitigate the problem by reducing the number of arenas (only helps if per thread memory usage spikes are uncorrelated). Another possibility is to segregate short- and long-lived objects into different arenas, but this requires that you have reliable (and ideally stable) knowledge of object lifetimes. In practice, segregation is usually very difficult to maintain. If you choose to go this direction, take a look at the "arenas.extend" mallctl (for creating an arena that contains long-lived objects), and the ALLOCM_ARENA(a) macro argument to the [r]allocm() functions.</div><br><blockquote type="cite"> I am purging unused<br>dirty pages a bit more aggressively than default (lg_dirty_mult: 5). <br>Should I consider being more aggressive? <br></blockquote><div><br></div>Dirty page purging isn't related to this problem.</div><div><br><blockquote type="cite">Secondly, I am using 1 arena per CPU core but my application creates<br>lots of transient threads making small allocations. Should I consider<br>using more arenas to mitigate performance bottlenecks incurred due to<br>blocking on per-arena locks?<br></blockquote><div><br></div><div>In general, the more arenas you have, the worse fragmentation is likely to be. Use the smallest number of arenas that doesn't unacceptably degrade throughput.</div><br><blockquote type="cite">Finally, looking at the jemalloc stats how should I go about <br>configuring the tcache? My application has a high thread churn & <br>each thread performs lots of short-lived small allocations. Should<br>I consider decreasing lg_tcache_max to 4K? <br></blockquote><br></div><div>This probably won't have much effect one way or the other, but setting lg_tcache_max set to 12 will potentially reduce memory overhead, so go for it if application throughput doesn't degrade unacceptably as a side effect.</div><div><br></div><div>It's worth mentioning that the tcache is a cause of fragmentation, because it thwarts jemalloc's layout policy of always choosing the lowest available region. Fragmentation may go down substantially if you completely disable the tcache, though the potential throughput degradation may be unacceptable.</div><div><br></div><div>Jason</div></body></html>