[转帖]Full GC (Ergonomics) 产生的原因
发生Full GC,有很多种原因,不仅仅是只有Allocation Failure。
还有以下这么多:
#include "precompiled.hpp"
#include "gc/shared/gcCause.hpp" const char* GCCause::to_string(GCCause::Cause cause) {
switch (cause) {
case _java_lang_system_gc:
return "System.gc()";case _full_gc_alot:
return "FullGCAlot"; case _scavenge_alot:
return "ScavengeAlot"; case _allocation_profiler:
return "Allocation Profiler"; case _jvmti_force_gc:
return "JvmtiEnv ForceGarbageCollection"; case _gc_locker:
return "GCLocker Initiated GC"; case _heap_inspection:
return "Heap Inspection Initiated GC"; case _heap_dump:
return "Heap Dump Initiated GC"; case _wb_young_gc:
return "WhiteBox Initiated Young GC"; case _wb_conc_mark:
return "WhiteBox Initiated Concurrent Mark"; case _wb_full_gc:
return "WhiteBox Initiated Full GC"; case _update_allocation_context_stats_inc:
case _update_allocation_context_stats_full:
return "Update Allocation Context Stats"; case _no_gc:
return "No GC"; case _allocation_failure:
return "Allocation Failure"; case _tenured_generation_full:
return "Tenured Generation Full"; case _metadata_GC_threshold:
return "Metadata GC Threshold"; case _metadata_GC_clear_soft_refs:
return "Metadata GC Clear Soft References"; case _cms_generation_full:
return "CMS Generation Full"; case _cms_initial_mark:
return "CMS Initial Mark"; case _cms_final_remark:
return "CMS Final Remark"; case _cms_concurrent_mark:
return "CMS Concurrent Mark"; case _old_generation_expanded_on_last_scavenge:
return "Old Generation Expanded On Last Scavenge"; case _old_generation_too_full_to_scavenge:
return "Old Generation Too Full To Scavenge"; case _adaptive_size_policy:
return "Ergonomics"; case _g1_inc_collection_pause:
return "G1 Evacuation Pause"; case _g1_humongous_allocation:
return "G1 Humongous Allocation"; case _dcmd_gc_run:
return "Diagnostic Command"; case _last_gc_cause:
return "ILLEGAL VALUE - last gc cause - ILLEGAL VALUE"; default:
return "unknown GCCause";
}
ShouldNotReachHere();
}
该文JVM内存分配担保机制在后面部分讲到在Server模式下,当设置为3M的时候,偶尔会发生Full GC。注意:是“偶尔”。
另外我们看到日志片段:
[Full GC (Ergonomics) [PSYoungGen: 544K->0K(9216K)] [ParOldGen: 6144K->6627K(10240K)] 6688K->6627K(19456K), [Metaspace: 3286K->3286K(1056768K)], 0.0063048 secs] [Times: user=0.01 sys=0.00, real=0.01 secs]
发现Full GC后面还有一个单词叫Ergonomics,Full GC后面的括号就是本次GC所产生的原因。
上文中我们说到:
发现当我们使用Server模式下的ParallelGC收集器组合(Parallel Scavenge+Serial Old的组合)下,担保机制的实现和之前的Client模式下(SerialGC收集器组合)有所变化。在GC前还会进行一次判断,如果要分配的内存>=Eden区大小的一半,那么会直接把要分配的内存放入老年代中。否则才会进入担保机制。
也就是使用了Parallel Scavenge+Serial Old的组合。
我们就去看看Parallel Scavenge回收策略的源码吧!
以下是片段:
// This method contains all heap specific policy for invoking scavenge.
// PSScavenge::invoke_no_policy() will do nothing but attempt to
// scavenge. It will not clean up after failed promotions, bail out if
// we've exceeded policy time limits, or any other special behavior.
// All such policy should be placed here.
//
// Note that this method should only be called from the vm_thread while
// at a safepoint!
bool PSScavenge::invoke() {
assert(SafepointSynchronize::is_at_safepoint(), "should be at safepoint");
assert(Thread::current() == (Thread*)VMThread::vm_thread(), "should be in vm thread");
assert(!ParallelScavengeHeap::heap()->is_gc_active(), "not reentrant"); ParallelScavengeHeap* const heap = ParallelScavengeHeap::heap();
PSAdaptiveSizePolicy* policy = heap->size_policy();
IsGCActiveMark mark; const bool scavenge_done = PSScavenge::invoke_no_policy();
const bool need_full_gc = !scavenge_done ||
policy->should_full_GC(heap->old_gen()->free_in_bytes());
bool full_gc_done = false; if (UsePerfData) {
PSGCAdaptivePolicyCounters* const counters = heap->gc_policy_counters();
const int ffs_val = need_full_gc ? full_follows_scavenge : not_skipped;
counters->update_full_follows_scavenge(ffs_val);
} if (need_full_gc) {
GCCauseSetter gccs(heap, GCCause::_adaptive_size_policy);
CollectorPolicy* cp = heap->collector_policy();
const bool clear_all_softrefs = cp->should_clear_all_soft_refs();if (UseParallelOldGC) {
full_gc_done = PSParallelCompact::invoke_no_policy(clear_all_softrefs);
} else {
full_gc_done = PSMarkSweep::invoke_no_policy(clear_all_softrefs);
}
}
return full_gc_done;
}
核心代码:
if (need_full_gc) {
GCCauseSetter gccs(heap, GCCause::_adaptive_size_policy);
CollectorPolicy* cp = heap->collector_policy();
const bool clear_all_softrefs = cp->should_clear_all_soft_refs();
if (UseParallelOldGC) {
full_gc_done = PSParallelCompact::invoke_no_policy(clear_all_softrefs);
} else {
full_gc_done = PSMarkSweep::invoke_no_policy(clear_all_softrefs);
}
}
注:基本内容是如果需要full gc那么就进入if块,然后执行full gc逻辑。另外这里的_adaptive_size_policy 常量就是对应的Ergonomics:
case _adaptive_size_policy:
return "Ergonomics";
那么full gc的条件是什么呢?也就是什么情况导致发生了本次full gc呢?
我们继续看看need_full_gc这个常量吧:
full gc条件:
const bool need_full_gc = !scavenge_done ||
policy->should_full_GC(heap->old_gen()->free_in_bytes());
should_ful_GC方法:
// If the remaining free space in the old generation is less that
// that expected to be needed by the next collection, do a full
// collection now.
bool PSAdaptiveSizePolicy::should_full_GC(size_t old_free_in_bytes) { // A similar test is done in the scavenge's should_attempt_scavenge(). If
// this is changed, decide if that test should also be changed.
bool result = padded_average_promoted_in_bytes() > (float) old_free_in_bytes;
//如果晋升到老年代的平均大小大于老年代的剩余大小,则认为要进行一次full gc log_trace(gc, ergo)(
"%s after scavenge average_promoted "
SIZE_FORMAT " padded_average_promoted "
SIZE_FORMAT " free in old gen " SIZE_FORMAT,
result ? "Full" : "No full",
(size_t) average_promoted_in_bytes(),
(size_t) padded_average_promoted_in_bytes(),
old_free_in_bytes);
return result;
}
通过查看should_full_GC方法,我们发现了这行代码:
bool result = padded_average_promoted_in_bytes() > (float) old_free_in_bytes;
通过该行代码,我们知道,如果晋升到老生代的平均大小大于老生代的剩余大小,则会返回true,认为需要一次full gc。
通过注释也可以知道:
If the remaining free space in the old generation is less than
that expected to be needed by the next collection, do a full
collection now. 如果老生代的剩余空间少于下一次收集所需的剩余空间,那么现在就做一个完整的收集。
如果 padded_average_promoted_in_bytes()大于老生代剩余空间,那么就返回true,表示要触发一次fullgc。
那么padded_average_promoted_in_bytes()这个平均大小是怎么算出来的呢?我们去看看:
// Padded average in bytes
size_t padded_average_promoted_in_bytes() const {
return (size_t)_avg_promoted->padded_average();
} float padded_average() const { return _padded_avg; }
// A weighted average that includes a deviation from the average,
// some multiple of which is added to the average.
//
// This serves as our best estimate of an upper bound on a future
// unknown.
class AdaptivePaddedAverage : public AdaptiveWeightedAverage {
private:
float _padded_avg; // The last computed padded average
float _deviation; // Running deviation from the average
unsigned _padding; // A multiple which, added to the average,
// gives us an upper bound guess. protected:
void set_padded_average(float avg) { _padded_avg = avg; }
void set_deviation(float dev) { _deviation = dev; } public:
AdaptivePaddedAverage() :
AdaptiveWeightedAverage(0),
_padded_avg(0.0), _deviation(0.0), _padding(0) {} AdaptivePaddedAverage(unsigned weight, unsigned padding) :
AdaptiveWeightedAverage(weight),
_padded_avg(0.0), _deviation(0.0), _padding(padding) {} // Placement support
void* operator new(size_t ignored, void* p) throw() { return p; }
// Allocator
void* operator new(size_t size) throw() { return CHeapObj<mtGC>::operator new(size); } // Accessor
float padded_average() const { return _padded_avg; }
float deviation() const { return _deviation; }
unsigned padding() const { return _padding; } void clear() {
AdaptiveWeightedAverage::clear();
_padded_avg = 0;
_deviation = 0;
} // Override
void sample(float new_sample); // Printing
void print_on(outputStream* st) const;
void print() const;
};
可以从代码和注释中我们发现:
加权平均值包括与平均值的偏差,其平均值加上其中的一些倍数。 这是对未来未知数的上限的最佳估计。
也就是通过这样的算法,虚拟机估算出下次分配可能会发生无法分配的问题,于是提前预测到可能的问题,提前发生一次full gc。
于是这次full gc就发生了!
那么你也许有疑问说[Full GC (Ergonomics) 的Ergonomics究竟是个什么东东?
Ergonomics翻译成中文,一般都是“人体工程学”。在JVM中的垃圾收集器中的Ergonomics就是负责自动的调解gc暂停时间和吞吐量之间的平衡,然后你的虚拟机性能更好的一种做法。
对于注重吞吐量的收集器来说,在某个generation被过渡使用之前,GC ergonomics就会启动一次GC。
正如我们前面提到的,发生本次full gc正是在使用Parallel Scavenge收集器的情况下发生的。
而Parallel Scavenge正是一款注重吞吐量的收集器:
Parallel Scavenge的目标是达到一个可控的吞吐量,吞吐量=程序运行时间/(程序运行时间+GC时间),如程序运行了99s,GC耗时1s,吞吐量=99/(99+1)=99%。Parallel Scavenge提供了两个参数用以精确控制吞吐量,分别是用以控制最大GC停顿时间的-XX:MaxGCPauseMillis及直接控制吞吐量的参数-XX:GCTimeRatio。
好,就到这里吧。
总之,以后遇到Full GC,不一定只有Allocation Failure,还有更多,比如本文中的“Ergonomics”。
原文地址
</article>
[转帖]Full GC (Ergonomics) 产生的原因的更多相关文章
- GC Ergonomics间接引发的锁等待超时问题排查分析
1. 问题背景 上周线上某模块出现锁等待超时,如下图所示: 我虽然不是该模块负责人,但出于好奇,也一起帮忙排查定位问题. 这里的业务背景就是在执行到某个地方时,需要去表中插入一批数据,这批数据需要根据 ...
- java.lang.OutOfMemoryError GC overhead limit exceeded原因分析及解决方案
最近一个上线运行良好的项目出现用户无法登录或者执行某个操作时,有卡顿现象.查看了日志,出现了大量的java.lang.OutOfMemoryError: GC overhead limit excee ...
- GC耗时高,原因竟是服务流量小?
原创:扣钉日记(微信公众号ID:codelogs),欢迎分享,转载请保留出处. 简介 最近,我们系统配置了GC耗时的监控,但配置上之后,系统会偶尔出现GC耗时大于1s的报警,排查花了一些力气,故在这里 ...
- JVM实用参数(八)GC日志
本系列的最后一部分是有关垃圾收集(GC)日志的JVM参数.GC日志是一个很重要的工具,它准确记录了每一次的GC的执行时间和执行结果,通过分析GC日志可以优化堆设置和GC设置,或者改进应用程序的对象分配 ...
- GC之一--GC 的算法分析、垃圾收集器、内存分配策略介绍
一.概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 MIT 的 Lisp 语言,经过半个多世纪,目前已经十分成熟了. jvm 中,程序计数器.虚拟机栈.本 ...
- GC参考手册 —— GC 调优(基础篇)
GC调优(Tuning Garbage Collection)和其他性能调优是同样的原理.初学者可能会被 200 多个 GC参数弄得一头雾水, 然后随便调整几个来试试结果,又或者修改几行代码来测试.其 ...
- GC参考手册 —— GC 调优(工具篇)
JVM 在程序执行的过程中, 提供了GC行为的原生数据.那么, 我们就可以利用这些原生数据来生成各种报告.原生数据(raw data) 包括: 各个内存池的当前使用情况, 各个内存池的总容量, 每次G ...
- GC参考手册 —— GC 算法(实现篇)
学习了GC算法的相关概念之后, 我们将介绍在JVM中这些算法的具体实现.首先要记住的是, 大多数JVM都需要使用两种不同的GC算法 —— 一种用来清理年轻代, 另一种用来清理老年代. 我们可以选择JV ...
- JVM垃圾回收(四)- GC算法:实现(1)
GC算法:实现 上面我们介绍了GC算法中的核心概念,接下来我们看一下JVM里的具体实现.首先必须了解的一个重要的事实是:对于大部分的JVM来说,两种不同的GC算法是必须的,一个是清理Young Gen ...
- JDK1.7 Update14 HotSpot虚拟机GC收集器
在测试服务器上使用如下命令可以查看当前使用的 GC收集器,当然不止这一个命令可以看到,还有其他一些方式 第三列”=”表示第四列是参数的默认值,而”:=” 表明了参数被用户或者JVM赋值了 [csii@ ...
随机推荐
- 2023-05-30:Redis6.0为什么要引入多线程呢?
2023-05-30:Redis6.0为什么要引入多线程呢? 答案2023-05-30: Redis多线程比单线程性能提升一倍: Redis 6.0 引入多线程 IO 特性对性能提升至少是一倍以上.据 ...
- 关于Triple DES(3DES)对称加密算法
一.引言 在网络安全领域,对称加密算法作为一种常见的加密手段,被广泛应用于保障数据传输的保密性和完整性.其中,DES(Data Encryption Standard)算法作为一种经典的对称加密算法, ...
- GaussDB(for MySQL)如何快速创建索引?华为云数据库资深架构师为您揭秘
摘要:云服务环境下,如何解决客户基于大量数据创建索引的性能问题,成为云服务厂商的一个挑战.华为云GaussDB(for MySQL)通过引入并行创建索引技术,很好地解决了批量索引创建和临时添加索引等性 ...
- 应用传送网络(ADN):率先架起“东数西算”的“高速公路”
摘要:云原生ADN网络的未来,是公有云Internet接入降成本的手段,以及对自建光纤骨干网的补充,有力地支撑 "东数西算"国家新基建布局. 本文分享自华为云社区<华为云顾炯 ...
- Python 可以满足你任何 API 使用需求
摘要:在本教程中学到的概念和技术将允许您使用自己喜欢的任何 API 进行练习,并使用 Python 来满足您可能拥有的任何 API 使用需求. 本文分享自华为云社区<Python 和 API:读 ...
- 协同编辑:Google Wave架构分析
Google Wave的设计初衷是让人们互相发送信息,一起编辑文档,但用户对此感到困惑,很快就以失败告终.Google Wave持续了大约一年时间,于2010年8月被关闭. Wave"领先于 ...
- iOS代码混淆-从入门到放弃
iOS代码混淆-从入门到放弃 目录 1. 什么是iOS代码混淆? 2. iOS自动代码混淆的方法是什么? 3. iOS代码混淆的作用是什么? 4. 怎么样才能做到更好的iOS代码混淆? 总结 参考 ...
- 火山引擎 DataTester:“在字节,A/B 实验是一种信仰”
更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,并进入官方交流群 进入数字经济时代,要用数据驱动业务增长已经成为各个行业的共识,但很多企业还没能真正掌握这项能力.如何最大限度转化数据价值, ...
- MAC zsh:no matches found
jimmy@MacBook-Pro bin % wsdl2java http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asm ...
- SpringBoot 项目实战 | 瑞吉外卖 Day06
该系列将记录一份完整的实战项目的完成过程,该篇属于第六天 案例来自B站黑马程序员Java项目实战<瑞吉外卖>,请结合课程资料阅读以下内容 该篇我们将完成以下内容: 用户地址簿相关功能 菜品 ...
