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

java.lang.OutOfMemoryError: GC overhead limit exceeded

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


当前回答

试试这个

打开构建。gradle文件

  android {
        dexOptions {
           javaMaxHeapSize = "4g"
        }
   }

其他回答

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
    }
}

//...

试试这个

打开构建。gradle文件

  android {
        dexOptions {
           javaMaxHeapSize = "4g"
        }
   }

重启MacBook帮我解决了这个问题。

您还可以通过将此添加到gradle中来增加内存分配和堆大小。属性文件:

org . gradle。jvmargs = -Xmx2048M -XX: MaxHeapSize \ = 32g

它不需要2048M和32g,你想要多大就有多大。

如果您确定程序中没有内存泄漏,请尝试:

增加堆的大小,例如-Xmx1g。 启用并发低暂停收集器-XX:+UseConcMarkSweepGC。 在可能的情况下重用现有对象以节省内存。

如果需要,可以通过在命令行中添加-XX:-UseGCOverheadLimit选项来禁用限制检查。