- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
根据这个问题的动机:Error java.lang.OutOfMemoryError: GC overhead limit exceeded
最近我和某人就这个错误进行了辩论。
在我的理解中,这个错误本身不能被视为 JVM 失败的“首要”原因。
我的意思是,广泛的垃圾收集本身并不是失败的原因。大量的垃圾收集总是由非常少的可用内存量引起的,这会导致频繁的 GC 调用(核心原因可能是内存泄漏)。
如果我正确理解了对手的立场,他认为系统中产生了很多符合GC条件的小对象,导致它们被频繁回收,导致了这个错误。所以问题不是内存泄漏或低内存限制,而是 GC 调用频率本身。
这里是我们有不同观点的地方。
在我看来,您的流程产生多少符合 GC 条件的小对象并不重要(即使它不是一个好的设计,您可能应该尽可能减少这个数量)。如果你有足够的内存,并且没有明显的内存泄漏,那么在某个时候 GC 会收集大部分这样的对象,所以这应该不是问题。至少这不会导致系统崩溃。
简要回顾一下我的立场:如果您GC overhead limit exceeded
,那么要么是内存泄漏,要么只是需要增加内存限制。
简要回顾一下我的对手的立场:如果你产生了很多符合 GC 条件的小对象,这已经是一个问题,因为它本身会导致 GC 开销限制超出
。
我是不是错了,错过了什么?
最佳答案
- 部分回答 -
请注意,我使用 OpenJDK (JDK 9) 源作为基础来评论这个问题。这个答案不依赖于任何类型的文档或已发布的规范,并且包含一些来 self 对源代码的理解和解释的推测。
GC overhead limit exceeded
在 VM 中被视为内存不足错误的子类型,并在尝试分配内存失败后生成(参见 (a))。
本质上,VM 跟踪完全垃圾收集 的发生次数,并将其与为完全 GC 强制执行的限制进行比较(可以使用 -XX:GCTimeLimit= 在 Hotspot 上配置
,参见 Garbage Collector Ergonomics )。
如何跟踪完整 GC 计数的实现以及检测到 GC 开销限制时背后的逻辑在一个地方可用,在 hotspot/src/share/vm/gc/shared/adaptiveSizePolicy.cpp
中。 .如您所见,为了满足 GC 开销限制的标准,需要满足老年代和伊甸园中可用内存的两个附加条件:
void AdaptiveSizePolicy::check_gc_overhead_limit(
size_t young_live,
size_t eden_live,
size_t max_old_gen_size,
size_t max_eden_size,
bool is_full_gc,
GCCause::Cause gc_cause,
CollectorPolicy* collector_policy) {
...
if (is_full_gc) {
if (gc_cost() > gc_cost_limit &&
free_in_old_gen < (size_t) mem_free_old_limit &&
free_in_eden < (size_t) mem_free_eden_limit) {
// Collections, on average, are taking too much time, and
// gc_cost() > gc_cost_limit
// we have too little space available after a full gc.
// total_free_limit < mem_free_limit
// where
// total_free_limit is the free space available in
// both generations
// total_mem is the total space available for allocation
// in both generations (survivor spaces are not included
// just as they are not included in eden_limit).
// mem_free_limit is a fraction of total_mem judged to be an
// acceptable amount that is still unused.
// The heap can ask for the value of this variable when deciding
// whether to thrown an OutOfMemory error.
// Note that the gc time limit test only works for the collections
// of the young gen + tenured gen and not for collections of the
// permanent gen. That is because the calculation of the space
// freed by the collection is the free space in the young gen +
// tenured gen.
// At this point the GC overhead limit is being exceeded.
inc_gc_overhead_limit_count();
if (UseGCOverheadLimit) {
if (gc_overhead_limit_count() >= AdaptiveSizePolicyGCTimeLimitThreshold){
// All conditions have been met for throwing an out-of-memory
set_gc_overhead_limit_exceeded(true);
// Avoid consecutive OOM due to the gc time limit by resetting
// the counter.
reset_gc_overhead_limit_count();
} else {
...
}
GC overhead limit exceeded
错误何时产生?它实际上不会在收集过程中发生,但是当 VM 尝试分配内存时 - 您可以在 hotspot/src/share/vm/gc/shared/collectedHeap.inline.hpp
中找到这些语句的理由。 :
HeapWord* CollectedHeap::common_mem_allocate_noinit(KlassHandle klass, size_t size, TRAPS) {
...
bool gc_overhead_limit_was_exceeded = false;
result = Universe::heap()->mem_allocate(size, &gc_overhead_limit_was_exceeded);
...
// Failure cases
if (!gc_overhead_limit_was_exceeded) {
report_java_out_of_memory("Java heap space");
...
} else {
report_java_out_of_memory("GC overhead limit exceeded");
...
}
查看 G1 实现的方法 mem_allocate
(可以在 g1CollectedHeap.cpp
中找到),看起来 boolean 值 gc_overhead_limit_was_exceeded
是不再使用了。如果启用 G1 GC,我不会很快得出这样的结论:GC 内存开销错误不会再发生 - 我需要检查一下。
看来你是对的,这个错误确实是内存耗尽造成的;
根据收集小对象的次数可以产生这个错误的论点对我来说似乎不正确,因为
关于java - "GC overhead limit exceeded"是失败的次要原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49303173/
在我们对延迟敏感的应用程序中,我们有缓存数据(驻留在 TG 中)和在 YG 中消亡的短暂对象。我已经看到次要 GC 时间和主要 GC 时间有显着差异。我怀疑这与TG的尺寸相对较大有关。谁能解释 GC
我看到了多个建议运行 GC.Collect(GC.MaxGeneration) 的答案。 既然方法GC.Collect()会收集所有存在的分代,那么两者有什么区别吗? 也许如果只有两代而不是三代,GC
我们正在使用 UseParallelGC。 GC 日志看起来像 2016-06-09T19:38:17.362+0000:655312.397:[完整GC(人体工程学)[PSYoungGen:2291
我最近看到了两个非常好的和有教育意义的语言讲座: This first one由 Herb Sutter 撰写,介绍了 C++0x 的所有漂亮和酷炫的特性,为什么 C++ 的 future 似乎比以往
我们正在运行 gerrit 2.10.7,我们偶尔会遇到损坏的对象没有被 gerrit gc 修复的问题,即使 git gc 可以很好地修复它们。 另一方面,我读到 gerrit gc 会创建优化其他
我试图避免 Full GC(来自下面的 gc.log 示例)在生产中的 Tomcat 中运行 Grails 应用程序。关于如何更好地配置 GC 有什么建议吗? 14359.317:[完整 GC 143
我试图通过在析构函数中使用 console.WriteLine() 来确保释放某个类的实例,但输出从未出现。 我仔细搜索了任何挥之不去的引用资料以及事件订阅,但没有找到。只是为了我自己的理智,在我继续
之前看过一篇文章,说FGC影响时序,导致application出错结果。 代码示例如下: long start = System.currentTimeInMillis(); doSomething(
在 Java 中,我们可以使用 System.gc() 方法来建议 GC。今天我从this link开始了解C#中的GC.Collect()方法。 . 但我对解释有些不清楚。 第一行。 Forces
我理解 Python GC 有两种工作方式: 1) 基本引用计数 - 当“name”设置为“Tom”时,“John”下方的引用计数为零 name = "John" name = "Tom" (Refe
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
今天我们使用并发标记清除,具体如下: -XX:+UseConcMarkSweepGC 我看到一些文章推荐使用这种形式的附加参数: -XX:+UseConcMarkSweepGC -XX:+CMSInc
当我运行我的程序时,logcat 显示很多 GC Activity 喜欢 GC freed 10324 objects/ 510376 bytes in 103 ms GC freed 10324 o
2013-11-26T10:19:30.011+0800: [GC [ParNew: 2432484K->19997K(2696640K), 0.0378270 secs] 5560240K->315
在执行 GC 时,JVM 会遍历 Activity 对象,并清除未标记的对象。 根据: How to Tune Java Garbage Collection “Full GC的执行时间相对Minor
我有一个分布式缓存应用程序(内存绑定(bind),由于与集群中其他节点的交互而具有网络 I/O)在 JVM 1.7.0_51 中运行,带有 G1 垃圾收集器。这是 JVM 配置: -server -X
首先,我想让您知道,这是一个理论问题而不是实际问题,我只是好奇弱引用对象是如何被释放的。让我们快速记住 Java 中的弱引用是什么。粗略地说WeakReference意味着当没有指向“我”的强引用时,
这是运行大约 10 分钟后的输出。 Heap PSYoungGen total 7040K, used 0K [0x24060000, 0x247c0000, 0x26790000)
我正在运行一个应用程序,在 Weblogic 上使用 java 5 和 CMS 垃圾收集器。在垃圾收集日志中,我看到了消息日志,其中大部分消息我可以使用 Sun 的 Java HotSpot 虚拟机中
我有一个 ConcurrentMap> map = new ConcurrentHashMap>(); 并且希望当 SoftReference 的引用被 GC 时从映射中删除键/值对。 我该如何实现这
我是一名优秀的程序员,十分优秀!