[转]java.lang.OutOfMemoryError:GC overhead limit exceeded

我遇到这样的问题,本地部署时抛出异常java.lang.OutOfMemoryError:GC overhead limit exceeded导致服务起不来,查看日志发现加载了太多资源到内存,本地的性能也不好,gc时间消耗的较多。解决这种问题两种方法是,增加参数,-XX:-UseGCOverheadLimit,关闭这个特性,同时增加heap大小,-Xmx1024m。坑填了,but why?

OOM大家都知道,就是JVM内存溢出了,那GC overhead limit exceed呢?

GC overhead limt exceed检查是Hotspot VM 1.6定义的一个策略,通过统计GC时间来预测是否要OOM了,提前抛出异常,防止OOM发生。Sun 官方对此的定义是:“并行/并发回收器在GC回收时间过长时会抛出OutOfMemroyError。过长的定义是,超过98%的时间用来做GC并且回收了不到2%的堆内存。用来避免内存过小造成应用不能正常工作。“

听起来没啥用...预测OOM有啥用?起初开来这玩意只能用来Catch住释放内存资源,避免应用挂掉。后来发现一般情况下这个策略不能拯救你的应用,但是可以在应用挂掉之前做最后的挣扎,比如数据保存或者保存现场(Heap Dump)。

而且有些时候这个策略还会带来问题,比如加载某个大的内存数据时频繁OOM。

假如你也生产环境中遇到了这个问题,在不知道原因时不要简单的猜测和规避。可以通过-verbose:gc -XX:+PrintGCDetails看下到底什么原因造成了异常。通常原因都是因为old区占用过多导致频繁Full GC,最终导致GC overhead limit exceed。如果gc log不够可以借助于JProfile等工具查看内存的占用,old区是否有内存泄露。分析内存泄露还有一个方法-XX:+HeapDumpOnOutOfMemoryError,这样OOM时会自动做Heap Dump,可以拿MAT来排查了。还要留意young区,如果有过多短暂对象分配,可能也会抛这个异常。

日志的信息不难理解,就是每次gc时打条日志,记录GC的类型,前后大小和时间。举个例子。

33.125: [GC [DefNew: 16000K->16000K(16192K), 0.0000574 secs][Tenured: 2973K->2704K(16384K), 0.1012650 secs] 18973K->2704K(32576K), 0.1015066 secs]

100.667:[Full GC [Tenured: 0K->210K(10240K), 0.0149142 secs] 4603K->210K(19456K), [Perm : 2999K->2999K(21248K)], 0.0150007 secs]

GC和Full GC代表gc的停顿类型,Full GC代表stop-the-world。箭头两边是gc前后的区空间大小,分别是young区、tenured区和perm区,括号里是该区的总大小。冒号前面是gc发生的时间,单位是秒,从jvm启动开始计算。DefNew代表Serial收集器,为Default New Generation的缩写,类似的还有PSYoungGen,代表Parallel Scavenge收集器。这样可以通过分析日志找到导致GC overhead limit exceeded的原因,通过调节相应的参数解决问题。

文中涉及到的名词解释,

Eden Space:堆内存池,大多数对象在这里分配内存空间。

Survivor Space:堆内存池,存储在Eden Space的gc中存活下来的对象。

Tenured Generation:堆内存池,存储Survivor Space中存活过几次gc的对象。

Permanent Generation:非堆空间,存储的是class和method对象。

Code Cache:非堆空间,JVM用来存储编译和存储native code。

最后附上GC overhead limit exceed HotSpot的实现:

  1 bool print_gc_overhead_limit_would_be_exceeded = false;
  2
  3 if (is_full_gc) {
  4
  5   if (gc_cost() > gc_cost_limit &&
  6
  7     free_in_old_gen < (size_t) mem_free_old_limit &&
  8
  9     free_in_eden < (size_t) mem_free_eden_limit) {
 10
 11     // Collections, on average, are taking too much time, and
 12
 13     //      gc_cost() > gc_cost_limit
 14
 15     // we have too little space available after a full gc.
 16
 17     //      total_free_limit < mem_free_limit
 18
 19     // where
 20
 21     //   total_free_limit is the free space available in
 22
 23     //     both generations
 24
 25     //   total_mem is the total space available for allocation
 26
 27     //     in both generations (survivor spaces are not included
 28
 29     //     just as they are not included in eden_limit).
 30
 31     //   mem_free_limit is a fraction of total_mem judged to be an
 32
 33     //     acceptable amount that is still unused.
 34
 35     // The heap can ask for the value of this variable when deciding
 36
 37     // whether to thrown an OutOfMemory error.
 38
 39     // Note that the gc time limit test only works for the collections
 40
 41     // of the young gen + tenured gen and not for collections of the
 42
 43     // permanent gen.  That is because the calculation of the space
 44
 45     // freed by the collection is the free space in the young gen +
 46
 47     // tenured gen.
 48
 49     // At this point the GC overhead limit is being exceeded.
 50
 51     inc_gc_overhead_limit_count();
 52
 53     if (UseGCOverheadLimit) {
 54
 55       if (gc_overhead_limit_count() >=
 56
 57           AdaptiveSizePolicyGCTimeLimitThreshold){
 58
 59         // All conditions have been met for throwing an out-of-memory
 60
 61         set_gc_overhead_limit_exceeded(true);
 62
 63         // Avoid consecutive OOM due to the gc time limit by resetting
 64
 65         // the counter.
 66
 67         reset_gc_overhead_limit_count();
 68
 69       } else {
 70
 71         // The required consecutive collections which exceed the
 72
 73         // GC time limit may or may not have been reached. We
 74
 75         // are approaching that condition and so as not to
 76
 77         // throw an out-of-memory before all SoftRef‘s have been
 78
 79         // cleared, set _should_clear_all_soft_refs in CollectorPolicy.
 80
 81         // The clearing will be done on the next GC.
 82
 83         bool near_limit = gc_overhead_limit_near();
 84
 85         if (near_limit) {
 86
 87           collector_policy->set_should_clear_all_soft_refs(true);
 88
 89           if (PrintGCDetails && Verbose) {
 90
 91             gclog_or_tty->print_cr("  Nearing GC overhead limit, "
 92
 93               "will be clearing all SoftReference");
 94
 95           }
 96
 97         }
 98
 99       }
100
101     }
102
103     // Set this even when the overhead limit will not
104
105     // cause an out-of-memory.  Diagnostic message indicating
106
107     // that the overhead limit is being exceeded is sometimes
108
109     // printed.
110
111     print_gc_overhead_limit_would_be_exceeded = true;
112
113
114
115   } else {
116
117     // Did not exceed overhead limits
118
119     reset_gc_overhead_limit_count();
120
121   }
122
123 }

参照&延伸阅读:

http://javaeesupportpatterns.blogspot.com/2012/01/gc-overhead-limit-exceeded-understand.html

http://www.oracle.com/technetwork/java/javase/gc-tuning-6-140523.html

http://reins.altervista.org/java/gc1.4.2_example.html

http://stackoverflow.com/questions/2129044/java-heap-terminology-young-old-and-permanent-generations

http://book.51cto.com/art/201306/399236.htm

https://blogs.oracle.com/jonthecollector/entry/presenting_the_permanent_generation

转自http://www.cnblogs.com/hucn/p/3572384.html

时间: 2024-08-03 17:49:03

[转]java.lang.OutOfMemoryError:GC overhead limit exceeded的相关文章

排查java.lang.OutOfMemoryError: GC overhead limit exceeded

帮助客户排查java.lang.OutOfMemoryError: GC overhead limit exceeded错误记录: 具体网址: https://support.oracle.com/epmos/faces/DocumentDisplay?_afrLoop=269134815562958&id=1554559.1&displayIndex=2&_afrWindowMode=0&_adf.ctrl-state=2t8bqbn6s_165 文档id: 155455

java.lang.OutOfMemoryError: GC overhead limit exceeded

今天现场weblogic报java.lang.OutOfMemoryError: GC overhead limit exceeded,在metalink查了下,有明白解释,要设置一个JVM參数.只是因为当前weblogic内存设置为4G,所以设置參数的做法事实上并非解决这个问题之道.还是要分析weblogic 内存溢出文件,得出是哪个功能有问题: APPLIES TO: Oracle WebLogic Server - Version 10.3 and later Information in

java.lang.OutOfMemoryError: GC overhead limit exceeded解决

异常表现 Exception in thread “main” java.lang.OutOfMemoryError: GC overhead limit exceeded 异常原因 JDK6新增错误类型.当GC为释放很小空间占用大量时间时抛出. 一般是因为堆太小.导致异常的原因:没有足够的内存. 解决方法 1.查看系统是否有使用大内存的代码或死循环. 2.可以添加JVM的启动参数来限制使用内存:-XX:-UseGCOverheadLimit

java 异常 java.lang.OutOfMemoryError: GC overhead limit exceeded 解决

一.异常如下: Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded 二.解释: JDK6新增错误类型.当GC为释放很小空间占用大量时间时抛出. 一般是因为堆太小.导致异常的原因:没有足够的内存. 三.解决方案: 1.查看系统是否有使用大内存的代码或死循环. 2.可以添加JVM的启动参数来限制使用内存:-XX:-UseGCOverheadLimit cnblogs - 问题处理

【jira】java.lang.OutOfMemoryError: GC overhead limit exceeded

登录JIRA访问打开缓慢,查询日志出现下述提示:java.lang.OutOfMemoryError: GC overhead limit exceeded 修改setenv.sh文件中的JVM配置,/data/atlassian/jira/bin 是我JIRA安装的路径 #cd  /data/atlassian/jira/bin #vi  setenv.sh 编辑保存后,重启JIRA的服务即可. 参考链接:https://www.cnblogs.com/ding2016/p/7568436.h

java.lang.OutOfMemoryError GC overhead limit exceeded原因分析及解决方案

最近一个上线运行良好的项目出现用户无法登录或者执行某个操作时,有卡顿现象.查看了日志,出现了大量的java.lang.OutOfMemoryError: GC overhead limit exceeded错误. oracle官方给出了这个错误产生的原因和解决方法: Exception in thread thread_name: java.lang.OutOfMemoryError: GC Overhead limit exceeded Cause: The detail message "G

java.lang.OutOfMemoryError: GC overhead limit exceeded 问题分析和解决(转)

在项目历史数据导入过程中,出现了应用无法访问的情况.立刻对Weblogic进行分析,发现Weblogic的内存.线程等性能良好,Server也是Running的状态.随后查看了Weblogic日志,在日志中发现下述错误 通过排查问题后得出造成此类问题的原因是,项目的一个模块在批量导入数据,程序需要New一个很大的对象,GC发现内存不够后,对内存进行回收,但是发现回收后的内存还不能满足大对象的需要.应此造成了内存溢出. 解决方案:调大Weblogic的启动内存即可. http://blog.csd

Jmeter java.lang.OutOfMemoryError: GC overhead limit exceeded

使用这个jmeter工具测试时,遇到这么个gc错误,网上找到了解决方案.原因是jmeter默认分配内存的参数很小,好像是256M吧.故而解决方法,就是增加内存: set HEAP=-Xms4g -Xmx4g set NEW=-XX:NewSize=1g -XX:MaxNewSize=1g 注意,new值必须比heap值要小. 有篇文章,可以参考下,我还没搞明白:http://www.cnblogs.com/jiu0821/p/6015310.html

Android Studio编译报错“java.lang.OutOfMemoryError: GC overhead limit exceeded

1.在build.gradle添加脚本指定编译堆内存 如果在整个工程中生效,则在build.gradle中增加如下配置: android { .............. dexOptions { incremental true javaMaxHeapSize "4g" } ............... } 如果只在单元测试的时候生效,则在build.gradle中增加如下配置: android { ..............     testOptions {         

spark出现GC overhead limit exceeded和java heap space

spark执行任务时出现java.lang.OutOfMemoryError: GC overhead limit exceeded和java.lang.OutOfMemoryError: java heap space 最直接的解决方式就是在spark-env.sh中将下面两个参数调节的尽量大 export SPARK_EXECUTOR_MEMORY=6000M    export SPARK_DRIVER_MEMORY=7000M 注意,此两个参数设置需要注意大小顺序: SPARK_EXEC