Eclipse抛出GC overhead limit exceeded异常问题的分析与解决

Eclipse抛出GC overhead limit exceeded异常问题的分析与解决

有时在使用Eclipse开发当中,会遇到GC overhead limit exceeded的异常。 
如下图 
GC overhead limit exceeded

其实这个问题可以在网上找到很多解决办法,不过大部分都只是告诉你要怎么做,至于出现这种错误的原因以及为什么这么做则没有提及。

解决办法: 
将eclipse.ini文件的最后两行,一般默认为

-Xms40m
-Xmx512m
 
 
  • 1
  • 2
  • 1
  • 2

修改为

-Xms512m
-Xmx1024m
 
 
  • 1
  • 2
  • 1
  • 2

到这时候,再重新启动Eclipse,一般情况下问题就解决了。

在这个时候有几个问题需要思考一下:

  • 为什么会出现这个错误
  • Xms和Xmx是什么意思(我到底修改了什么)

为什么会出现这个错误或者说这个错误是什么意思

The Java.lang.OutOfMemoryError: GC overhead limit exceeded error means that the GC tried to free memory but was pretty much unable to get anything done. By default it happens when the JVM spends more than 98% of the total time in GC and when after GC less than 2% of the heap is recovered. 
——[GC overhead limit exceeded]

也就是说GC试图回收内存,但是什么也没有回收到。默认情况下,JVM花费了98%的时间在GC上,但是GC过之后只有不到2%的堆内存被回收。

这里边比较有意思的是98%指的是时间,而2%指的却是堆内存。

到这里可以再拓展思考一下,如果没有这个GC overhead limit会发生什么现象

如果没有这个GC overhead limit会发生什么现象

What would happen if this GC overhead limit would not exist? Note that the java.lang.OutOfMemoryError: GC overhead limit exceeded error is thrown only when 2% of the memory was freed after several GC cycles. This means that the little amount GC was able to clean will be quickly filled again, forcing GC to restart the cleaning process again. This forms a vicious cycle where the CPU is 100% busy with GC and no actual work can be done. End users of the application face extreme slowdowns – operations which normally complete in milliseconds take minutes to finish. 
——[GC overhead limit exceeded]

简单来讲,就是一次GC过后,并没有回收到内存,很快又会进行GC,极端情况下会循环GC(关键是并没有回收到可用内存),从而导致CPU 100%负载。

Xms和Xmx是什么意思(我到底修改了什么)

这两个都是JVM的参数

  • Xms JVM的初始堆大小
  • Xmx JVM的最大堆大小

当最小堆被占满后,会进行GC,如果GC之后还不能得到足够的内存,就会扩展堆,最大扩展至Xmx设定的[1]值。


参考文章:

  1. 我不理解java -Xms -Xmx的概念,高手指导一下
  2. 编译或打包程序时eclipse报Unable to execute dex: GC overhead limit exceeded或java heap space
  3. Unable to execute dex: GC overhead limit exceeded in Eclipse
  4. GC overhead limit exceeded
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值