当我执行JUnit测试时,我得到了这个错误消息:

java.lang.OutOfMemoryError: GC overhead limit exceeded

我知道什么是OutOfMemoryError,但是GC开销限制意味着什么?我怎么解决这个问题?


当前回答

在Netbeans中,设计最大堆大小可能会有所帮助。执行命令Run =>设置项目配置=>自定义。在弹出窗口的运行中,进入虚拟机选项,填写-Xms2048m -Xmx2048m。它可以解决堆大小的问题。

其他回答

通过设置这个选项,稍微增加堆的大小

运行→运行配置→参数→虚拟机参数

-Xms1024M -Xmx2048M

Xms—用于最小限制

Xmx -表示最大限制

根据Java[8]平台,标准版故障处理指南,错误原因:(强调和换行符添加)

[…“GC overhead limit exceeded”表示垃圾收集器一直在运行,Java程序进展非常缓慢。 在垃圾收集之后,如果Java进程花费大约98%的时间进行垃圾收集,如果它回收的堆不到2%,并且到目前为止已经连续进行了5次(编译时间常数)垃圾收集,则抛出Java .lang. outofmemoryerror。[…]

Increase the heap size if current heap is not enough. If you still get this error after increasing heap memory, use memory profiling tools like MAT ( Memory analyzer tool), Visual VM etc and fix memory leaks. Upgrade JDK version to latest version ( 1.8.x) or at least 1.7.x and use G1GC algorithm. . The throughput goal for the G1 GC is 90 percent application time and 10 percent garbage collection time Apart from setting heap memory with -Xms1g -Xmx2g , try -XX:+UseG1GC -XX:G1HeapRegionSize=n -XX:MaxGCPauseMillis=m -XX:ParallelGCThreads=n -XX:ConcGCThreads=n

看看更多关于G1GC的相关问题

G1上的Java 7 (JDK 7)垃圾收集和文档 生产环境中的Java G1垃圾收集 Oracle technetwork关于GC优化的文章

通常是代码。这里有一个简单的例子:

import java.util.*;

public class GarbageCollector {

    public static void main(String... args) {

        System.out.printf("Testing...%n");
        List<Double> list = new ArrayList<Double>();
        for (int outer = 0; outer < 10000; outer++) {

            // list = new ArrayList<Double>(10000); // BAD
            // list = new ArrayList<Double>(); // WORSE
            list.clear(); // BETTER

            for (int inner = 0; inner < 10000; inner++) {
                list.add(Math.random());
            }

            if (outer % 1000 == 0) {
                System.out.printf("Outer loop at %d%n", outer);
            }

        }
        System.out.printf("Done.%n");
    }
}

在Windows 7 32位操作系统上使用Java 1.6.0_24-b07。

java -Xloggc:gc.log GarbageCollector

然后查看gc.log

使用BAD方法触发444次 使用WORSE方法触发666次 使用BETTER方法触发354次

现在承认,这不是最好的测试或最好的设计,但当你面临别无选择只能实现这样的循环或处理行为糟糕的现有代码时,选择重用对象而不是创建新对象可以减少垃圾收集器阻碍的次数……

这条消息意味着由于某种原因,垃圾收集器花费了过多的时间(默认情况下占进程所有CPU时间的98%),并且在每次运行中回收的内存非常少(默认情况下占堆时间的2%)。

这实际上意味着您的程序停止执行任何进度,并且一直忙于只运行垃圾收集。

为了防止应用程序占用CPU时间而不做任何事情,JVM抛出这个错误,以便您有机会诊断问题。

我很少看到这种情况发生的情况是,一些代码在已经非常受内存限制的环境中创建了大量临时对象和大量弱引用对象。

请查看Java GC调优指南,该指南可用于各种Java版本,其中包含关于此特定问题的部分:

Java 11调优指南中有针对不同垃圾收集器的过量GC的专门章节: 用于并联集热器 用于同步标记扫描(CMS)收集器 对于垃圾优先(G1)收集器,没有提到这种特定的错误条件。 Java 8调优指南及其过量GC部分 Java 6调优指南及其过量GC部分。

I'm working in Android Studio and encountered this error when trying to generate a signed APK for release. I was able to build and test a debug APK with no problem, but as soon as I wanted to build a release APK, the build process would run for minutes on end and then finally terminate with the "Error java.lang.OutOfMemoryError: GC overhead limit exceeded". I increased the heap sizes for both the VM and the Android DEX compiler, but the problem persisted. Finally, after many hours and mugs of coffee it turned out that the problem was in my app-level 'build.gradle' file - I had the 'minifyEnabled' parameter for the release build type set to 'false', consequently running Proguard stuffs on code that hasn't been through the code-shrinking' process (see https://developer.android.com/studio/build/shrink-code.html). I changed the 'minifyEnabled' parameter to 'true' and the release build executed like a dream :)

简而言之,我必须改变应用程序级别的“构建”。Gradle文件来自: / /……

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.sign_config_release
    }
    debug {
        debuggable true
        signingConfig signingConfigs.sign_config_debug
    }
}

//...

to

    //...

buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        signingConfig signingConfigs.sign_config_release
    }
    debug {
        debuggable true
        signingConfig signingConfigs.sign_config_debug
    }
}

//...