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

java.lang.OutOfMemoryError: GC overhead limit exceeded

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


当前回答

在build.gradle(Module:app)文件中增加javaMaxHeapsize

dexOptions {
    javaMaxHeapSize "1g"
}

to(在gradle中添加这一行)

 dexOptions {
        javaMaxHeapSize "4g"
    }

其他回答

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

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

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

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

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

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

-Xms1024M -Xmx2048M

Xms—用于最小限制

Xmx -表示最大限制

引用Oracle的文章“Java SE 6 HotSpot[tm]虚拟机垃圾收集调优”:

Excessive GC Time and OutOfMemoryError The parallel collector will throw an OutOfMemoryError if too much time is being spent in garbage collection: if more than 98% of the total time is spent in garbage collection and less than 2% of the heap is recovered, an OutOfMemoryError will be thrown. This feature is designed to prevent applications from running for an extended period of time while making little or no progress because the heap is too small. If necessary, this feature can be disabled by adding the option -XX:-UseGCOverheadLimit to the command line.

编辑:看起来有人打字比我快:)

解决: 只需添加 org.gradle.jvmargs = -Xmx1024m 在 gradle.properties 如果它不存在,那就创造它。