Out of memory error : GC overhead limit exceeded
GC overhead limit exceeded 是指垃圾回收器通过分析当前内存使用趋势,提前抛出异常而不是真正等到内存耗尽再抛出异常。如果真正等到内存耗尽再抛出异常,可能的后果是:我们连保存重要数据的空间都没了。
那么GC是如何分析内存使用趋势来决定是否跑出此异常呢? 默认情况下, 如果内存使用量达到一个阈值之后,GC花费的时间超过 98%, 并且GC回收的内存少于 2%,JVM就会抛出异常。也就是说,GC发现“内存将尽,大厦将倾”,而自己“鞠躬尽瘁”,“费力不讨好”,总做“无用功”的时候,GC就会抛出异常。
明白了异常发生的原理,我们很容易“制造”异常来模拟“车祸”现场。下面的代码随机生成字符串,并把字符串添加到列表。因为这些新创建的字符串无法被GC回收,在内存溢出之前,GC就率先抛出异常了。
运行这段程序使用-Xmx12m
参数,把堆设的小一些效果更明显。
package wyf.clonesite;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class BugShooter {
String randomString() {
StringBuilder builder = new StringBuilder();
Random r = new Random();
for (int i = 0; i < 30; i++) {
builder.append(r.nextInt(100));
}
return builder.toString();
}
BugShooter() {
int cnt = 0;
try {
List<String> a = new ArrayList<>();
while (true) {
cnt++;
a.add(randomString());
if (cnt % 1000 == 0) System.out.println(cnt);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println(cnt);
}
}
public static void main(String[] args) {
new BugShooter();
}
}
异常内容如下:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at java.util.Random.<init>(Random.java:137)
at java.util.Random.<init>(Random.java:105)
at wyf.clonesite.BugShooter.randomString(BugShooter.java:10)
at wyf.clonesite.BugShooter.<init>(BugShooter.java:23)
at wyf.clonesite.BugShooter.main(BugShooter.java:33)
GC提前抛出异常看的是内存使用的“速度”,而内存溢出看的是内存使用的“路程”。速度如果太快,GC就认为很危险。
实际中,我们可能确实会有大量开辟不可回收对象的场景,那么如何解决这个问题呢?
- 下策:关闭GC提前抛出异常的机制,使用
-XX:-UseGCOverheadLimit
- 中策:捕捉此异常,在内存耗尽之前dump程序状态,备份好数据,坦然“就义”
- 上策:加大堆空间
-Xmx1024m
参考资料
https://blog.csdn.net/renfufei/article/details/77585294
https://blog.csdn.net/evilcry2012/article/details/79063822
Out of memory error : GC overhead limit exceeded的更多相关文章
- [Hadoop] - Hadoop Mapreduce Error: GC overhead limit exceeded
在运行mapreduce的时候,出现Error: GC overhead limit exceeded,查看log日志,发现异常信息为 2015-12-11 11:48:44,716 FATAL [m ...
- troubleshooting-sqoop mysql导入hive 报:GC overhead limit exceeded
Halting due to Out Of Memory Error...18/09/13 21:42:17 INFO mapreduce.Job: Task Id : attempt_1536756 ...
- sqoop import mysql to hive table:GC overhead limit exceeded
1. Scenario description when I use sqoop to import mysql table into hive, I got the following error: ...
- JVM--你常见的jvm 异常有哪些? 代码演示:StackOverflowError , utOfMemoryError: Java heap space , OutOfMemoryError: GC overhead limit exceeded, Direct buffer memory, Unable_to_create_new_native_Thread, Metaspace
直接上代码: public class Test001 { public static void main(String[] args) { //java.lang.StackOverflowErro ...
- android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded
android studio Error:java.lang.OutOfMemoryError: GC overhead limit exceeded 在app下的build.gradle中找到and ...
- eclipse一直报An internal error occurred during: "Building workspace". GC overhead limit exceeded
最近导入到eclipse里的工程挺大的,每次eclipse启动之后都回update workspace,然后就一直报: An internal error occurred during: " ...
- eclipse:An internal error occurred during: "Build Project". GC overhead limit exceeded
在使用Eclipse的Build Project功能时,提示以下错误: An internal error occurred during: "Build Project". GC ...
- An internal error occurred during: "Retrieving archetypes:". GC overhead limit exceeded
An internal error occurred during: "Retrieving archetypes:".GC overhead limit exceeded 异常, ...
- oozie: GC overhead limit exceeded 解决方法
1.异常表现形式 1) 提示信息 Error java.lang.OutOfMemoryError: GC overhead limit exceeded 2)提示出错 Erro ...
随机推荐
- 一款基于jQuery的图片场景标注提示弹窗特效
今天给大家分享一款基于jQuery的图片场景标注提示弹窗特效,这款实例适合在图片上标注某个物件,单击弹出详情说明,兼容360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之 ...
- 如何基于TensorFlow使用LSTM和CNN实现时序分类任务
https://www.jiqizhixin.com/articles/2017-09-12-5 By 蒋思源2017年9月12日 09:54 时序数据经常出现在很多领域中,如金融.信号处理.语音识别 ...
- mysql的sql分页函数limit使用 (转)
http://www.cnblogs.com/beijingstruggle/p/5631603.html mysql的sql分页函数limit使用 My sql数据库最简单,是利用mysql的LIM ...
- Python输出信息
Python很多情况下,都是通过Console输出信息,大量翻屏滚动的信息,太乱.而pip安装的效果就很好,只是在一行输出信息,不断变化.网上找资料,不难. 用print的话,跟一个“,”就表示不换行 ...
- 【Python】用geopy查两经纬度间的距离
代码: from geopy.distance import vincenty from geopy.distance import great_circle 天安门 = (39.90733345, ...
- logback-kafka-appender
logback 日志写入kafka队列 logback-kafka-appender Logback incompatibility Warning Due to a bug in logback-c ...
- 让.aspx同样实现.ashx文件的功能: IHttpHandler
我们需要一个能够调用该处理程序的入口点.在此上下文中,该处理程序代码的入口点只不过是一个HTTP终点——即,一个公共的URL.该URL必须有一个惟一的名称,使IIS和ASP.NET运行库能够把它映射到 ...
- vsphere中虚机的cpu热插拔和内存热添加
Guest OS对此支持如下: Windows 2008 64-bit Datacenter Edition,它是可以兼容vSphere的hot-add或hot-plug功能的.增加RAM和增加CPU ...
- 常用jQuery知识
什么是jQuery jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML do ...
- cocos2d-js 粒子系统使用自定义图片,还原原来的图片宽高
粒子系统使用自定义图片很简单只需要在plist最后一行设置png的名称即可.但是,在实际使用中,发现自定义图片无法使用原来的形状,例如设置了一长条的图片,结果出来确实一个个圆球. 翻了plist和cc ...