源碼文件:/src/hotspot/share/gc/z/zDirector.cpp
一、回收策略
main入口函數:
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); } } }
ZMetronome::wait_for_tick 是zgc定義的一個循環時鍾函數,sample_allocation_rate函數則用於rule_allocation_rate策略估算可能oom的時間。重點關注:make_gc_decision函數,在判斷從make_gc_decision函數返回的結果不是no_gc后,zgc將進行一次gc。
make_gc_decision函數:
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; }
make_gc_decision一共提供了4種被動gc策略:
rule 1:固定間隔時間
通過配置ZCollectionInterval參數,可以控制zgc在一個固定的時間間隔進行gc,默認值為0,表示不采用該策略,否則則判斷從上次gc到現在的時間間隔是否大於ZCollectionInterval秒,是則gc。源碼如下:
bool ZDirector::rule_timer() const { if (ZCollectionInterval == 0) { // Rule disabled 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; log_debug(gc, director)("Rule: Timer, Interval: %us, TimeUntilGC: %.3lfs", ZCollectionInterval, time_until_gc); return time_until_gc <= 0; }
rule 2:預熱規則
is_warm函數判斷gc次數是否已超過3次,是則不使用該策略。
注釋說的很清楚,當gc次數少於3時,判斷堆使用率達到10%/20%/30%時,使用該策略
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; } bool ZDirector::is_warm() const { return ZStatCycle::ncycles() >= 3; } // 位置:ZStat.cpp uint64_t ZStatCycle::ncycles() { return _ncycles; // gc次數 }
rule 3:分配速率預估
is_first函數判斷如果是首次gc,則直接返回false。
ZAllocationSpikeTolerance默認值為2,分配速率策略采用正態分布模型預測內存分配速率,加上ZAllocationSpikeTolerance修正因子,可以覆蓋超過99.9%的內存分配速率的可能性
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; } bool ZDirector::is_first() const { return ZStatCycle::ncycles() == 0; }
rule 4:積極回收策略
通過ZProactive可啟用積極回收策略,is_warm函數判斷啟用該策略必須是在預熱之后(gc次數超過3次)
自上一次gc后,堆使用率達到xmx的10%或者已過了5分鍾,這個參數是彌補第三個規則中沒有覆蓋的場景,從上述分析可以得到第三個條件更多的覆蓋分配速率比較高的場景。
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) { // Don't even consider doing a proactive GC log_debug(gc, director)("Rule: Proactive, UsedUntilEnabled: " SIZE_FORMAT "MB, TimeUntilEnabled: %.3lfs", (used_threshold - used) / M, time_since_last_gc_threshold - time_since_last_gc); 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; log_debug(gc, director)("Rule: Proactive, AcceptableGCInterval: %.3lfs, TimeSinceLastGC: %.3lfs, TimeUntilGC: %.3lfs", acceptable_gc_interval, time_since_last_gc, time_until_gc); return time_until_gc <= 0; }
最后,當所有策略都不滿足時,返回_no_gc,表示不進行gc
二、回收過程
gc整個周期:
彩色指針示意圖:
- (STW)Pause Mark Start,開始標記,這個階段只會標記(Mark0)由root引用的object,組成Root Set
- Concurrent Mark,並發標記,從Root Set出發,並發遍歷Root Set object的引用鏈並標記(Mark1)
- (STW)Pause Mark End,檢查是否已經並發標記完成,如果不是,需要進行多一次Concurrent Mark
-
Concurrent Process Non-Strong References,並發處理弱引用
- Concurrent Reset Relocation Set
- Concurrent Destroy Detached Pages
- Concurrent Select Relocation Set,並發選擇Relocation Set;
- Concurrent Prepare Relocation Set,並發預處理Relocation Set
- (STW)Pause Relocate Start,開始轉移對象,依然是遍歷root引用
- Concurrent Relocate,並發轉移,將需要回收的Page里的對象轉移到Relocation Set,然后回收Page給系統重新利用
run_gc_cycle函數(/src/hotspot/share/gc/z/zDriver.cpp):
void ZDriver::run_gc_cycle(GCCause::Cause cause) { ZDriverCycleScope scope(cause); // Phase 1: Pause Mark Start { ZMarkStartClosure cl; vm_operation(&cl); } // Phase 2: Concurrent Mark { ZStatTimer timer(ZPhaseConcurrentMark); ZHeap::heap()->mark(); } // Phase 3: Pause Mark End { ZMarkEndClosure cl; while (!vm_operation(&cl)) { // Phase 3.5: Concurrent Mark Continue ZStatTimer timer(ZPhaseConcurrentMarkContinue); ZHeap::heap()->mark(); } } // Phase 4: Concurrent Process Non-Strong References { ZStatTimer timer(ZPhaseConcurrentProcessNonStrongReferences); ZHeap::heap()->process_non_strong_references(); } // Phase 5: Concurrent Reset Relocation Set { ZStatTimer timer(ZPhaseConcurrentResetRelocationSet); ZHeap::heap()->reset_relocation_set(); } // Phase 6: Concurrent Destroy Detached Pages { ZStatTimer timer(ZPhaseConcurrentDestroyDetachedPages); ZHeap::heap()->destroy_detached_pages(); } // Phase 7: Concurrent Select Relocation Set { ZStatTimer timer(ZPhaseConcurrentSelectRelocationSet); ZHeap::heap()->select_relocation_set(); } // Phase 8: Concurrent Prepare Relocation Set { ZStatTimer timer(ZPhaseConcurrentPrepareRelocationSet); ZHeap::heap()->prepare_relocation_set(); } // Phase 9: Pause Relocate Start { ZRelocateStartClosure cl; vm_operation(&cl); } // Phase 10: Concurrent Relocate { ZStatTimer timer(ZPhaseConcurrentRelocated); ZHeap::heap()->relocate(); } }
未完待續