16

I just updated the Android Studio to 3.5.0 and I'm getting Expiring Daemon because JVM heap space is exhausted . Message while the build is running. Also, the build is taking more time to complete. Does anyone have any idea regarding this error help me?

Dishant Chanchad
  • 710
  • 3
  • 9
  • 27
  • I had the same error and I read a lot of doc on the internet but I didn't find any solution for this, I was forced to remove and install again all things in my computer – Mohsen Haghighatkhah Nov 26 '19 at 05:47
  • 1
    In windows, You can add a system environment variable named _JAVA_OPTIONS, and set the heap size values there.Like this SET _JAVA_OPTIONS = -Xms512m -Xmx1024m – Viral Patel Nov 26 '19 at 05:53
  • try a system restart after @ViralPatel 's suggestion – LonelyCpp Nov 26 '19 at 07:52

5 Answers5

52

I had the same problem, and the following answer helped but needs to be adapted to React Native.

https://stackoverflow.com/a/57548822/6798074

Make your gradle.properties look like the following:

android.useAndroidX=true
android.enableJetifier=true
org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

And add the following in your app/build.gradle under the android task:

android {

  dexOptions {
    javaMaxHeapSize "4g"
  }

}
Ahmed AlAskalany
  • 1,600
  • 1
  • 13
  • 13
14

As you add more modules to your app, there is an incredible demand placed on the Android build system, and the default memory settings will not work. To avoid OutOfMemoryErrors during Android builds, you should uncomment the alternate gradle memory setting present in /android/gradle.properties:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
REACT NATIVE
  • 193
  • 1
  • 2
  • 7
1

After trying couple of solution below code finally resolve the error

Add below in gradle.properties

org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx1028m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
Moumit
  • 8,314
  • 9
  • 55
  • 59
0

Solution: 1) Add following in android/app/build.gradle:

android: {
...
   dexOptions {
      javaMaxHeapSize: "4g"
   }
}

Add following in android/gradle.properties:

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx4096m
Khurshid Ansari
  • 4,638
  • 2
  • 33
  • 52
  • You need to use `dexOptions { javaMaxHeapSize "4g" }` instead You need to use `dexOptions { javaMaxHeapSize: "4g" }` Notice: colon (:) is not there. Otherwise, you'll get an error `In case you tried to configure a property named 'javaMaxHeapSize', replace ':' with '=' or ' ', otherwise it will not have the desired effect.` – Lalit Sharma Apr 17 '21 at 20:08
0

Just add this line in gradle-wrapper-properties

org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

here -Xmx4g will provide dex size to 4Gb, same as javaMaxHeapSize. XX:MaxPermSize is the permanent heap size to allocate.

Bharat Lalwani
  • 1,277
  • 15
  • 17