115

I just updated the Android Studio to 3.5 Beta 1 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?

larsgrefer
  • 2,387
  • 15
  • 29
Rishabh Sagar
  • 2,678
  • 3
  • 11
  • 18
  • 2
    See here if that helps: https://stackoverflow.com/questions/47207126/gradle-build-of-android-app-in-vsts-failing-after-running-out-of-memory – Juraj Martinka May 12 '19 at 06:00
  • 1
    @JurajMartinka yes, you are right. it got fixed after increasing the **IDE max heap size**. So they introduce a new option called **Memory Settings** in the latest Android Studio 3.5. This is all done to fix memory leak in Android Studio. – Rishabh Sagar May 13 '19 at 05:21

4 Answers4

130

I was able to solve this for my React Native project by configuring the following:

// gradle.properties
org.gradle.daemon=true
org.gradle.configureondemand=true
org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

and

// app/build.gradle
android {
    dexOptions {
       javaMaxHeapSize "3g"
    }
}
MattBooth
  • 653
  • 6
  • 15
Jordan Grant
  • 1,593
  • 2
  • 8
  • 9
117

This can be fixed by increasing the configured max heap size for the project.

Through IDE:

Add the below lines into the gradle.properties file. Below memory size can be configured based on the RAM availability

org.gradle.daemon=true
org.gradle.jvmargs=-Xmx2560m

Through GUI:

In the Settings, search for 'Memory Settings' and increase the IDE max heap size and Daemon max heap size as per the system RAM availability.

Memory Settings in Android Studio

Rishabh Sagar
  • 2,678
  • 3
  • 11
  • 18
37

The solution is to increase Android build memory.

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

You can find gradle.properties inside android folder.

P.S.

What we are doing this and why it helps?

Let me clear some basic terminology for understanding the whole thing.

Daemon : - A daemon is a computer program that runs as a background process, rather than being under the direct control of an interactive user.

Android Studio 2.1 enables a new feature: Dex In Process, that can dramatically increase the speed of full clean builds as well as improving Instant Run performance.

To take advantage of Dex In Process, you’ll need to modify your gradle.properties file and increase the amount of memory allocated to the Gradle Daemon VM by 1 Gb, to a minimum of 2 Gb, using the org.gradle.jvmargs property:

Specifies the JVM arguments used for the daemon process. The setting is particularly useful for tweaking memory settings.

org.gradle.jvmargs=-Xmx2048m

Default value:

-Xmx10248m -XX:MaxPermSize=256m

The default Gradle Daemon VM memory allocation is 1 gigabyte — which is insufficient to support dexInProcess, so to take advantage you’ll need to set it to at least 2 gigabytes.

Dex in process works by allowing multiple DEX processes to run within a single VM that’s also shared with Gradle, which is why you need to allocate the extra memory before it can be enabled — that memory will be shared between Gradle and multiple DEX processes.

If you’ve increased the javaMaxHeapSize in your module-level build.gradle file beyond the default of 1 gigabyte, you’ll need increase the memory assigned to the Gradle Daemon correspondingly.

When there’s enough memory assigned Dex in Process is enabled by default, improving overall build performance and removing the overhead of starting multiple parallel VM instances. The result is a significant improvement in all build times, including Instant Run, incremental, and full builds.

Source : https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e

https://rnfirebase.io/#increasing-android-build-memory

Kailash Uniyal
  • 557
  • 7
  • 14
  • 1
    Could you explain what this is doing and why it helps? – Alex Mar 24 '20 at 14:49
  • 1
    Put the sources as well. https://medium.com/google-developers/faster-android-studio-builds-with-dex-in-process-5988ed8aa37e – Rishabh Sagar Apr 06 '20 at 17:46
  • 1
    Hello @KailashUniyal what if i have the heap size at 2048 in android studio's gui and i’m still getting 'Expiring Daemon because JVM heap space is exhausted'? – abdi Apr 18 '20 at 15:07
  • 1
    @abdi Try putting this ```org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8``` in gradle.properties. If still not working then sorry I have no idea how to fix that. – Kailash Uniyal Apr 21 '20 at 14:18
  • 1
    un-commenting `org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8` in `android/gradle.properties` worked lika a charm. Thanks!! @KailashUniyal – vikas bansal Jul 18 '20 at 04:27
4

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