Android Studio compiler error "java.lang.OutOfMemoryError: GC overhead limit exceeded"

Preface

GC overhead limt exceed check is a strategy defined by Hotspot VM 1.6. It predicts whether OOM is needed by counting GC time, throws exceptions ahead of time, and prevents OOM from happening. Sun's official definition of this is: "Parallel / Concurrent Recycler throws OutOfMemroyError when GC reclaims too long. Over 98% of the time is spent on GC and less than 2% of heap memory is recovered. It is used to prevent the application from working properly because of too little memory.

In short:
When GC takes up a lot of time to release very little space, it throws out. Usually because the heap is too small.
Cause of the exception: There is not enough memory.

Android Studio compiler error java.lang.OutOfMemoryError: GC overhead limit exceeded
Detailed crash information is as follows:

UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: GC overhead limit exceeded
    at com.android.dx.cf.code.RopperMachine.getSources(RopperMachine.java:665)
    at com.android.dx.cf.code.RopperMachine.run(RopperMachine.java:288)
    at com.android.dx.cf.code.Simulator$SimVisitor.visitLocal(Simulator.java:612)
    at com.android.dx.cf.code.BytecodeArray.parseInstruction(BytecodeArray.java:412)
    at com.android.dx.cf.code.Simulator.simulate(Simulator.java:94)
    at com.android.dx.cf.code.Ropper.processBlock(Ropper.java:782)
    at com.android.dx.cf.code.Ropper.doit(Ropper.java:737)
    at com.android.dx.cf.code.Ropper.convert(Ropper.java:346)
    at com.android.dx.dex.cf.CfTranslator.processMethods(CfTranslator.java:282)
    at com.android.dx.dex.cf.CfTranslator.translate0(CfTranslator.java:139)
    at com.android.dx.dex.cf.CfTranslator.translate(CfTranslator.java:94)
    at com.android.dx.command.dexer.Main.processClass(Main.java:682)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
    at com.android.dx.command.dexer.Main.access$600(Main.java:78)
    at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:596)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
    at com.android.dx.command.dexer.Main.run(Main.java:230)
    at com.android.dx.command.dexer.Main.main(Main.java:199)
    at com.android.dx.command.Main.main(Main.java:103)

Solution:

  • If it takes effect throughout the project, add the following configuration to build.gradle:
android {
..............
      dexOptions {
            incremental true//Turning on incremental compilation of dex is still an experimental feature, but it's recommended to try it.
            javaMaxHeapSize "4G"//Setting the maximum heap memory allowed to be allocated
        }
...............
}
  • If it only takes effect at the time of unit testing, add the following configuration to build.gradle:
android {
..............
    testOptions {
        android.dexOptions {
            incremental true
            javaMaxHeapSize "4G"
        }
    }
...............
}

Tags: Android Java Attribute simulator

Posted on Thu, 14 Feb 2019 08:09:19 -0800 by rigbyae