OutOfMemoryError: GC overhead limit exceeded andro

2019-09-01 04:51发布

问题:

In android studio 1.5.1 just by moving the source code from one system to another and Even though the clean build is successful but while the code is run I am getting this kind of error

java.lang.OutOfMemoryError: GC overhead limit exceeded Error:Execution failed for task ':app:dexDebug'. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-7-openjdk-amd64/bin/java'' finished with non-zero exit value 3

I added the below code in app.gradle also:

dexOptions {
        javaMaxHeapSize "4g"
}

回答1:

GC overhead limit exceeded means that your app occupied all the heap and garbage collector cannot clean enough space to run the program.

So, there is too much necessary data in memory or memory leak (i.e. references which can be accessed from app root but no longer needed) exists in your application - only further app profiling can give you more details



回答2:

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.

This was taken from Oracle Java Documentation



回答3:

You can increase your allocated memory for the app by adding

android:largeHeap="true"

to your manifest file and see if it solves your problem. But you should look for memory leaks in your app because the OutOfMemoryError is usually a consequence of bad memory management.