ZGC源碼分析(3)- ZGC觸發(fā)的時(shí)機(jī)

ZGC以被動回收為主隧土,即由后臺線程控制何時(shí)啟動垃圾回收。 ZGC的觸發(fā)時(shí)機(jī)在

jdk11/src/hotspot/share/gc/z/zDirector.cpp

ZDirector決定是否要執(zhí)行GC的后臺線程

線程出發(fā)的條件是根據(jù)一個自定義的時(shí)鐘轻局。

void ZDirector::run_service() {
  // Main loop
  while (_metronome.wait_for_tick()) {
    sample_allocation_rate();
    const GCCause::Cause cause = make_gc_decision();
    if (cause != GCCause::_no_gc) {
      ZCollectedHeap::heap()->collect(cause);
    }
  }
}
GCCause::Cause ZDirector::make_gc_decision() const {
  // Rule 0: Timer
  if (rule_timer()) {
    return GCCause::_z_timer;
  }

  // Rule 1: Warmup
  if (rule_warmup()) {
    return GCCause::_z_warmup;
  }

  // Rule 2: Allocation rate
  if (rule_allocation_rate()) {
    return GCCause::_z_allocation_rate;
  }

  // Rule 3: Proactive
  if (rule_proactive()) {
    return GCCause::_z_proactive;
  }

  // No GC
  return GCCause::_no_gc;
}

目前有4種情況可以自動的觸發(fā)ZGC.

1,基于一個固定時(shí)間觸發(fā)

這個時(shí)間可以通過ZCollectionInterval,來控制屡谐,缺省值為0魂爪,表示不需要。

  product(uint, ZCollectionInterval, 0,                                     \
          "Force GC at a fixed time interval (in seconds)")                 \
bool ZDirector::rule_timer() const {
  if (ZCollectionInterval == 0)      return false;

  // Perform GC if timer has expired.
  const double time_since_last_gc = ZStatCycle::time_since_last();
  const double time_until_gc = ZCollectionInterval - time_since_last_gc;

  return time_until_gc <= 0;
}

2,預(yù)熱規(guī)則觸發(fā)

指的是當(dāng)Hotspot剛啟動時(shí)哩盲,當(dāng)發(fā)現(xiàn)heap使用率達(dá)到整個堆的10/20/30%前方,并且其他類型的GC都還沒執(zhí)行時(shí)狈醉,會主動地觸發(fā)GC。當(dāng)其他類型的GC出發(fā)后惠险,會判斷是否還需要預(yù)熱舔糖,如果需要繼續(xù)執(zhí)行,不需要則不再執(zhí)行莺匠。預(yù)熱的的條件是 GC發(fā)生的次數(shù)不超過3次。

bool ZDirector::is_warm() const {
  return ZStatCycle::ncycles() >= 3;
}

bool ZDirector::rule_warmup() const {
  if (is_warm()) {
    // Rule disabled
    return false;
  }

  // Perform GC if heap usage passes 10/20/30% and no other GC has been
  // performed yet. This allows us to get some early samples of the GC
  // duration, which is needed by the other rules.
  const size_t max_capacity = ZHeap::heap()->current_max_capacity();
  const size_t used = ZHeap::heap()->used();
  const double used_threshold_percent = (ZStatCycle::ncycles() + 1) * 0.1;
  const size_t used_threshold = max_capacity * used_threshold_percent;

  log_debug(gc, director)("Rule: Warmup %.0f%%, Used: " SIZE_FORMAT "MB, UsedThreshold: " SIZE_FORMAT "MB",
                          used_threshold_percent * 100, used / M, used_threshold / M);

  return used >= used_threshold;
}

3,根據(jù)分配速率

在這里使用到正態(tài)分布十兢,我們在代碼里面能看到兩個相關(guān)的應(yīng)用:根據(jù)內(nèi)存分配的情況估算內(nèi)存被消耗完還可能要多長時(shí)間趣竣;根據(jù)垃圾回收的時(shí)間估算進(jìn)行一次垃圾回收的時(shí)間。

在G1中我們介紹到垃圾回收時(shí)間的估算使用的是衰減平均(Decaying Average)旱物,它是一種簡單的數(shù)學(xué)方法遥缕,用來計(jì)算一個數(shù)列的平均,核心是給近期的數(shù)據(jù)更高的權(quán)重宵呛,即強(qiáng)調(diào)近期數(shù)據(jù)對結(jié)果的影響单匣。衰減平均計(jì)算公式如下:

image.png

式中 ɑ 為歷史數(shù)據(jù)權(quán)值,1?ɑ 為最近一次數(shù)據(jù)權(quán)值宝穗。即 ɑ 越小户秤,最新的數(shù)據(jù)對結(jié)果影響越大,最近一次的數(shù)據(jù)對結(jié)果影響最大逮矛。不難看出鸡号,其實(shí)傳統(tǒng)的平均就是 ɑ 取值為 (n?1)/n 的情況。具體可以參考《JVM G1源碼分析和調(diào)優(yōu)》

ZGC中主要是基于正態(tài)分布來估算须鼎,學(xué)過概率論的同學(xué)大都知道這一概念鲸伴。為了讀懂這段代碼,我們先來回顧一下正態(tài)分布晋控。首先它是一條中間高汞窗,兩端逐漸下降且完全對稱的鐘形曲線。圖形形狀為:

image.png

正態(tài)分布也非常容易理解赡译,它指的大多數(shù)數(shù)據(jù)應(yīng)該集中在中間附近仲吏,少數(shù)異常的情況才會落在兩端。
對于垃圾回收算法中的數(shù)據(jù):內(nèi)存的消耗時(shí)間捶朵,垃圾回收的時(shí)間也應(yīng)該符合這樣的分布蜘矢。注意,并不是說G1中的停頓預(yù)測模型不正確或者效果不好综看;而是說使用正態(tài)分布來做預(yù)測有更強(qiáng)的數(shù)學(xué)理論支撐品腹。在使用中ZGC還是對這個數(shù)學(xué)模型做了一些改變。
通常使用N表示正態(tài)分布红碑,假設(shè)X符合均值為μ方差為σ2的分布舞吭,做數(shù)學(xué)變換令Y = (X - μ)/ σ則它符合N(0, 1)分布泡垃。如下所示:


image.png

假設(shè)已知內(nèi)存分配的時(shí)間符合正態(tài)分布,我們可以獲得抽樣數(shù)據(jù)羡鸥,從而估算出內(nèi)存分配所需時(shí)間的均值和方差蔑穴。這個均值和方差是我們基于樣本數(shù)據(jù)估算得到的,它們可能和實(shí)際真實(shí)的均值和方差有一定的誤差惧浴。所以如果我們直接使用這個均值和方差可能由樣本數(shù)據(jù)波動導(dǎo)致不準(zhǔn)確存和,所以在概率論中引入了置信度和置信區(qū)間。簡單的說置信區(qū)間指的是這個參數(shù)估計(jì)的一段區(qū)間衷旅,它是這個參數(shù)的真實(shí)值有一定概率落在測量結(jié)果的周圍的程度捐腿。而置信度指的是就是這個概率。

假定給定一個內(nèi)存分配花費(fèi)的時(shí)間X1柿顶, X2茄袖, …, Xn我們想要知道在99.9%的情況下內(nèi)存分配花費(fèi)的時(shí)間嘁锯。點(diǎn)估計(jì)量符合:

image.png

其中μ為樣本均值宪祥,σ樣本標(biāo)準(zhǔn)差。
對應(yīng)99.9%置信度家乘,查標(biāo)準(zhǔn)正態(tài)分布表得到統(tǒng)計(jì)量為3.290527蝗羊。

image.png

由此可以得到置信區(qū)間為
image.png

。所以可以得到最大的內(nèi)存消耗在滿足99.9%的情況下不會超過
image.png

烤低。在ZGC中對這個公司又做了一點(diǎn)修改肘交,實(shí)際上是把這個值變得更大:
image.png

。Tolerance缺省值為2扑馁,這樣的結(jié)果使得置信度更高涯呻,即遠(yuǎn)大于99.9%。同理對于垃圾回收的時(shí)間也類似處理腻要。理解了置信區(qū)間和置信度下面的代碼非常簡單复罐。
bool ZDirector::rule_allocation_rate() const {
  if (is_first()) {
    // Rule disabled
    return false;
  }

  // Perform GC if the estimated max allocation rate indicates that we
  // will run out of memory. The estimated max allocation rate is based
  // on the moving average of the sampled allocation rate plus a safety
  // margin based on variations in the allocation rate and unforeseen
  // allocation spikes.

  // Calculate amount of free memory available to Java threads. Note that
  // the heap reserve is not available to Java threads and is therefore not
  // considered part of the free memory.
  const size_t max_capacity = ZHeap::heap()->current_max_capacity();
  const size_t max_reserve = ZHeap::heap()->max_reserve();
  const size_t used = ZHeap::heap()->used();
  const size_t free_with_reserve = max_capacity - used;
  const size_t free = free_with_reserve - MIN2(free_with_reserve, max_reserve);

  // Calculate time until OOM given the max allocation rate and the amount
  // of free memory. The allocation rate is a moving average and we multiply
  // that with an allocation spike tolerance factor to guard against unforeseen
  // phase changes in the allocate rate. We then add ~3.3 sigma to account for
  // the allocation rate variance, which means the probability is 1 in 1000
  // that a sample is outside of the confidence interval.
  const double max_alloc_rate = (ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::avg_sd() * one_in_1000);
  const double time_until_oom = free / (max_alloc_rate + 1.0); // Plus 1.0B/s to avoid division by zero

  // Calculate max duration of a GC cycle. The duration of GC is a moving
  // average, we add ~3.3 sigma to account for the GC duration variance.
  const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration();
  const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000);

  // Calculate time until GC given the time until OOM and max duration of GC.
  // We also deduct the sample interval, so that we don't overshoot the target
  // time and end up starting the GC too late in the next interval.
  const double sample_interval = 1.0 / ZStatAllocRate::sample_hz;
  const double time_until_gc = time_until_oom - max_duration_of_gc - sample_interval;

  log_debug(gc, director)("Rule: Allocation Rate, MaxAllocRate: %.3lfMB/s, Free: " SIZE_FORMAT "MB, MaxDurationOfGC: %.3lfs, TimeUntilGC: %.3lfs",
                          max_alloc_rate / M, free / M, max_duration_of_gc, time_until_gc);

  return time_until_gc <= 0;
}
ZAllocationSpikeTolerance是一個修正系數(shù),

  product(double, ZAllocationSpikeTolerance, 2.0,                           \
          "Allocation spike tolerance factor")                              \

4,自行控制進(jìn)行GC

heap距離上次GC發(fā)生后使用增長率超過10%雄家,或者距離上次GC發(fā)生后超過5min效诅。這個參數(shù)是彌補(bǔ)第三個條件中沒有覆蓋的場景,從上述分析可以得到第三個條件更多的覆蓋分配速率比較高的場景趟济。

  diagnostic(bool, ZProactive, true,                                        \
          "Enable proactive GC cycles")                                     \
bool ZDirector::rule_proactive() const {
  if (!ZProactive || !is_warm()) {
    // Rule disabled
    return false;
  }

  // Perform GC if the impact of doing so, in terms of application throughput
  // reduction, is considered acceptable. This rule allows us to keep the heap
  // size down and allow reference processing to happen even when we have a lot
  // of free space on the heap.

  // Only consider doing a proactive GC if the heap usage has grown by at least
  // 10% of the max capacity since the previous GC, or more than 5 minutes has
  // passed since the previous GC. This helps avoid superfluous GCs when running
  // applications with very low allocation rate.
  const size_t used_after_last_gc = ZStatHeap::used_at_relocate_end();
  const size_t used_increase_threshold = ZHeap::heap()->current_max_capacity() * 0.10; // 10%
  const size_t used_threshold = used_after_last_gc + used_increase_threshold;
  const size_t used = ZHeap::heap()->used();
  const double time_since_last_gc = ZStatCycle::time_since_last();
  const double time_since_last_gc_threshold = 5 * 60; // 5 minutes
  if (used < used_threshold && time_since_last_gc < time_since_last_gc_threshold) {
    return false;
  }

  const double assumed_throughput_drop_during_gc = 0.50; // 50%
  const double acceptable_throughput_drop = 0.01;        // 1%
  const AbsSeq& duration_of_gc = ZStatCycle::normalized_duration();
  const double max_duration_of_gc = duration_of_gc.davg() + (duration_of_gc.dsd() * one_in_1000);
  const double acceptable_gc_interval = max_duration_of_gc * ((assumed_throughput_drop_during_gc / acceptable_throughput_drop) - 1.0);
  const double time_until_gc = acceptable_gc_interval - time_since_last_gc;

  return time_until_gc <= 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末乱投,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子顷编,更是在濱河造成了極大的恐慌戚炫,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,248評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件媳纬,死亡現(xiàn)場離奇詭異双肤,居然都是意外死亡施掏,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,681評論 2 381
  • 文/潘曉璐 我一進(jìn)店門茅糜,熙熙樓的掌柜王于貴愁眉苦臉地迎上來七芭,“玉大人,你說我怎么就攤上這事蔑赘±瓴担” “怎么了?”我有些...
    開封第一講書人閱讀 153,443評論 0 344
  • 文/不壞的土叔 我叫張陵缩赛,是天一觀的道長锌历。 經(jīng)常有香客問我,道長峦筒,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,475評論 1 279
  • 正文 為了忘掉前任窗慎,我火速辦了婚禮物喷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘遮斥。我一直安慰自己峦失,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,458評論 5 374
  • 文/花漫 我一把揭開白布术吗。 她就那樣靜靜地躺著尉辑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪较屿。 梳的紋絲不亂的頭發(fā)上隧魄,一...
    開封第一講書人閱讀 49,185評論 1 284
  • 那天,我揣著相機(jī)與錄音隘蝎,去河邊找鬼购啄。 笑死,一個胖子當(dāng)著我的面吹牛嘱么,可吹牛的內(nèi)容都是我干的狮含。 我是一名探鬼主播,決...
    沈念sama閱讀 38,451評論 3 401
  • 文/蒼蘭香墨 我猛地睜開眼曼振,長吁一口氣:“原來是場噩夢啊……” “哼几迄!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起冰评,我...
    開封第一講書人閱讀 37,112評論 0 261
  • 序言:老撾萬榮一對情侶失蹤映胁,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后集索,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體屿愚,經(jīng)...
    沈念sama閱讀 43,609評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡汇跨,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,083評論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了妆距。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片穷遂。...
    茶點(diǎn)故事閱讀 38,163評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖娱据,靈堂內(nèi)的尸體忽然破棺而出蚪黑,到底是詐尸還是另有隱情,我是刑警寧澤中剩,帶...
    沈念sama閱讀 33,803評論 4 323
  • 正文 年R本政府宣布忌穿,位于F島的核電站,受9級特大地震影響结啼,放射性物質(zhì)發(fā)生泄漏掠剑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,357評論 3 307
  • 文/蒙蒙 一郊愧、第九天 我趴在偏房一處隱蔽的房頂上張望朴译。 院中可真熱鬧,春花似錦属铁、人聲如沸眠寿。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,357評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盯拱。三九已至,卻和暖如春例嘱,著一層夾襖步出監(jiān)牢的瞬間狡逢,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,590評論 1 261
  • 我被黑心中介騙來泰國打工拼卵, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留甚侣,地道東北人。 一個月前我還...
    沈念sama閱讀 45,636評論 2 355
  • 正文 我出身青樓间学,卻偏偏與公主長得像殷费,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子低葫,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,925評論 2 344

推薦閱讀更多精彩內(nèi)容