1. 简介
ZGC的回收周期触发时机与其他GC算法略有不同,VM内部有个线程轮询定期检查是否满足开始回收的条件,如果满足则开始回收。
ZGC提供四种策略,其中一种满足条件即触发回收:
- rule_timer,定时策略,距离上次GC时间超过interval即触发GC
- rule_warmup,VM启动后,如果从来没有发生过GC,则在堆内存使用超过10%、20%、30%时,分别触发一次GC,以收集GC数据
- rule_allocation_rate,根据对象分配速率决定是否GC
- rule_proactive,主动控制策略,根据距离上次GC增长的堆空间和距离上次GC的时间判断是否触发GC
如果对象分配过快,以至于以上四种策略均无法及时回收对象,则在到达阈值后,STW并行回收。
2. 代码分析
2.1 GC轮询线程
zDirector.cpp
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);
}
}
}
调用wait_for_tick()等待100ms
调用sample_allocation_rate,抽样计算分配速率
判断是否触发GC,如是则执行collect函数
zStat.hpp
class ZStatAllocRate : public AllStatic {
private:
static const ZStatUnsampledCounter _counter;
static TruncatedSeq _rate; // B/s
static TruncatedSeq _rate_avg; // B/s
public:
static const uint64_t sample_window_sec = 1; // seconds
static const uint64_t sample_hz = 10;
static const ZStatUnsampledCounter& counter();
static uint64_t sample_and_reset();
static double avg();
static double avg_sd();
};
- wait_for_tick轮询等待时间是sample_hz的倒数,即100毫秒
zStat.cpp
uint64_t ZStatAllocRate::sample_and_reset() {
const ZStatCounterData bytes_per_sample = _counter.collect_and_reset();
const uint64_t bytes_per_second = bytes_per_sample._counter * sample_hz;
_rate.add(bytes_per_second);
_rate_avg.add(_rate.avg());
return bytes_per_second;
}
- 合计每个CPU自上次抽样到现在分配的字节数
- 分配的字节数除以抽样频率,可得分配速率
2.2 GC触发策略
2.2.1 rule_timer
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;
}
- 如果没有设置JVM参数-XX:ZCollectionInterval,则返回,不会使用rule_timer策略
- 如果设置了ZCollectionInterval,则判断当前时间减上次gc时间是否大于ZCollectionInterval,如是则触发GC
2.2.2 rule_warmup
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;
}
- 先判断系统是否已经预热,如果GC次数大于等于3,则不使用rule_warmup策略
- 每当堆空间占用率大于10%、20%、30%时,触发一次GC
2.2.3 rule_allocation_rate
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;
}
如果VM启动后,从来发生过GC,则不使用rule_allocation_rate
ZGC分配速率的计算与G1不同,采用的是正态分布,置信度为99.9%时,最大内存分配速率为((ZStatAllocRate::avg() * 1) + (ZStatAllocRate::avg_sd() * 3.290527))
ZAllocationSpikeTolerance是个修正参数,默认2,加入该修正系数后,置信度远大于99.9%,计算公式为((ZStatAllocRate::avg() * ZAllocationSpikeTolerance) + (ZStatAllocRate::avg_sd() * 3.290527))
根据最大分配速率,可以计算出到达OOM的剩余时间
同样根据正太分布,取置信度99.9%,计算出最大GC持续时间
当time_until_oom - max_duration_of_gc - sample_interval小于0时,即触发GC
2.2.4 rule_proactive
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;
}
先判断JVM参数ZProactive(默认true),如果是false则不使用rule_proactive
如果VM还没有预热,则不使用rule_proactive
如果距离上次GC,堆内存占用增长小于10%且小于5分钟,则不使用rule_proactive
如果距离上次GC时间超过最大预测GC时间乘49,则触发GC
3. 引用
OpenJDK12 源代码: https://hg.openjdk.java.net/jdk/jdk12/file/06222165c35f/src/hotspot/share/gc/z