2 min read

How to Fix GC Overhead Limit Exceeded when Dexing

java.lang.OutOfMemoryError: GC overhead limit exceeded when dexing

This is relevant if your builds are crashing due to something like:

Exception in thread "pool-1-thread-2" Exception in thread "pool-1-thread-3" Exception in thread "pool-1-thread-4" java.lang.OutOfMemoryError: GC overhead limit exceeded

This is happening because your project has tons of classes to churn through and the dexing process is using too much memory. Run your gradle build with “–info –debug” and you’ll see it failing while dexing a bunch of imports.

 

Step-by-step Guide

    1. Open your app’s build.gradle (not the one in the project root) and add:
      android {
      //snip
      //add this into your existing 'android' block
      dexOptions {
      javaMaxHeapSize "4g"
      }
      //snip
      }
    2. Try your build again. Experiment with the value as needed.

 

!Additional notes

  • The dexing process does NOT respect the JAVA_OPTS or GRADLE_OPTS env vars, so you can’t fix this with the -Xmx flag or anything like that. Because that’d be too easy.
  • The default javaMaxHeapSize appears to be a little over 1 GB.
  • In extreme cases, you could run into this: https://code.google.com/p/android/issues/detail?id=7147#c6
  • If you have that many methods being dexed, it might be time to look into using Proguard to slim down your app.

 

Interested in learning how we solve problems?

 

SEE OUR JOB OPENINGS

Ready to transform your brand?