MysteryMan MysteryMan - 1 month ago 8
Android Question

Top level exception: GC Overhead limit reached. Android Maven build

I am intermittently getting an Unexpected Top level exception: GC overhead limit reached or sometimes Java out of heap space when building an Android app using Maven.

This issue started happening only after I upgraded to Windows 10 (From windows 8, was working without a problem on windows 8). The project builds fine on about 20 Other machines (Running different versions of MacOS, Linux, Windows etc.) and only breaks on my machine

I have tried setting the MAVEN_OPTS -Xmx env variable to 2048m 4096m 1024m or just leaving it out but it doesn't make a difference. I have also tried to turn off the GC overhead limit check but it also didn't work

This issue occurs when the dex part of the build is happening and only happens about half the time. Basically half the time it builds fine the other half of the time it crashes with either GC overhead limit reached or Java out of heap.

I am using Java jdk1.8.0_101

Apache Maven 3.3.9

Any help on how to fix this problem would be greatly appreciated

Edit



I ran the profiler and eventually got it to fail with:

Uncaught translation error: java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError: GC overhead limit exceeded

Screenshot of the profiler:
Dex profile screenshot

I am unsure what is causing this in the dex process as it seems to me that it is sort of hanging for a while and then suddenly the GC just kicks into overdrive

Answer

Adding this to my POM under the android maven plugin configuration fixed the issue for me

<dex>
    <jvmArguments>
      <jvmArgument>-Xms2048m</jvmArgument>
      <jvmArgument>-Xmx4096m</jvmArgument>
    </jvmArguments>
</dex>

Best explanation I could come up with

Okay so after looking at everything it seemed to be a memory issue. The dex process was allocated 1,6 Gb of JVM memory which was sometimes enough but on some occasions, for whatever reason, it required slightly more. My best guess is that this caused it to try and run the GC to free up memory which caused the GC overhead limit reached. In other cases, it would just throw an Out of heap space error. Regardless giving the Dex process a large amount of memory fixed my problem. On a side note. I never got this issue before windows 10 building the same project with the same config