OOM之GC Overhead limit exceeded

8 篇文章 0 订阅
4 篇文章 0 订阅

Java8常见的OOM主要有三种,分别是Exception in thread thread_name: java.lang.OutOfMemoryError: Java heap space、Exception in thread thread_name: java.lang.OutOfMemoryError: GC Overhead limit exceeded 以及Exception in thread thread_name: java.lang.OutOfMemoryError: Metaspace, 今天我们就重新其中的 GC Overhead limit exceeded,并借助gc log分析一下原因。

官方文档:https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/memleaks002.html#CIHHJDJE

重现

重新代码, 需要在启动参数加上-Xmx5m -Xms5m -XX:+HeapDumpOnOutOfMemoryError -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:gc.log

package com.yq.gc;

import java.util.ArrayList;
import java.util.List;

/**
 * Simple to Introduction代码非常简陋,只是为了说明问题
 * className: HeapOOMDemo
 *  -Xmx5m -Xms5m  -XX:+HeapDumpOnOutOfMemoryError   -XX:+PrintGCDateStamps -XX:+PrintGCDetails -Xloggc:gc.log
 * @author EricYang
 * @version 2019/8/7 14:54
 */
public class HeapOOMDemo {
    private static List<String> strList = new ArrayList<>();

    public static void main(String[] args) {
        int count = 10000;
        String str1 = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890";
        for(int i=0; i< count; i++) {
            strList.add(str1 + "i" + i);
        }
        System.out.println("...");

        for(int i=0; i< count; i++) {
            strList.add(str1 + "i" + i);
            strList.add(str1 + "i" + i);
            strList.add(str1 + "i" + i);
        }
        System.out.println("done");
    }
}

运行结果

java.lang.OutOfMemoryError: GC overhead limit exceeded
Dumping heap to java_pid26720.hprof ...
Heap dump file created [6397808 bytes in 0.026 secs]
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
	at java.util.Arrays.copyOfRange(Arrays.java:3664)
	at java.lang.String.<init>(String.java:207)
	at java.lang.StringBuilder.toString(StringBuilder.java:407)
	at com.yq.gc.HeapOOMDemo.main(HeapOOMDemo.java:25)

Process finished with exit code 1

在这里插入图片描述

分析

官方的解释:
Exception in thread thread_name: java.lang.OutOfMemoryError: GC Overhead limit exceeded
Cause: The detail message “GC overhead limit exceeded” indicates that the garbage collector is running all the time and Java program is making very slow progress. After a garbage collection, if the Java process is spending more than approximately 98% of its time doing garbage collection and if it is recovering less than 2% of the heap and has been doing so far the last 5 (compile time constant) consecutive garbage collections, then a java.lang.OutOfMemoryError is thrown. This exception is typically thrown because the amount of live data barely fits into the Java heap having little free space for new allocations.
Action: Increase the heap size. The java.lang.OutOfMemoryError exception for GC Overhead limit exceeded can be turned off with the command line flag -XX:-UseGCOverheadLimit.

总结起来就是:gc一致在运行并且java程序运行很慢。Java进程过去5个连续gc过程中,花费了大约98%+的时间进行gc并且回收的空间小于堆大小的2%,
采取的措施:增加堆大小,, 可以通过-XX:-UseGCOverheadLimit.关闭该异常。

注意:我给的例子比较极端,实际中很可能出现同一个程序有时候是java.lang.OutOfMemoryError: GC overhead limit exceeded,有时候是java.lang.OutOfMemoryError: Java heap space。 具体处理办法都是,先分析threaddump文件,看看那个地方能优化代码,不要长期持有不用的对象,防止memory leak,同时可以增加heapsize,也可以根据具体情况调整gc算法(调整后一定要进行对比测试),但是一般不建议调整gc算法。

heapdump文件分析(MAT分析截图)
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当在使用Eclipse的Build Project功能时出现"GC overhead limit exceeded"错误时,这意味着JVM内存溢出了,在GC(垃圾回收)过程中所花费的时间过长,而回收的堆内存只占很小的比例。这个错误是Hotspot VM 1.6定义的一个策略,旨在提前抛出异常以防止发生OOM(OutOfMemoryError)。 为了解决这个问题,你可以尝试以下几个方法: 1. 增加JVM的内存限制,可以通过在启动Eclipse时添加参数"-XX:MaxPermSize=1024m"来增加最大内存限制。这样在编译文件时将占用更多的内存。 2. 检查你的项目的依赖和资源使用情况,确保没有过多的资源占用内存。 3. 尝试升级Eclipse版本或使用最新的更新来修复可能存在的bug或问题。 4. 如果以上方法都不起作用,你可能需要考虑增加你的计算机的物理内存来提供更大的内存供应。 希望这些方法能够帮助你解决"GC overhead limit exceeded"错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Eclipse:An internal error occurred during: "Build Project"... GC overhead limit exceeded](https://blog.csdn.net/weixin_34321753/article/details/90149104)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值