Out of memory error while running gradlew assembleRelease - react-native

JavaReact NativeOut of-MemoryReact Native-Android

Java Problem Overview


I am getting this error while making a release build for my react native project:

Expiring Daemon because JVM heap space is exhausted
    
> Task :app:transformDexArchiveWithDexMergerForRelease FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformDexArchiveWithDexMergerForRelease'.
> java.lang.OutOfMemoryError (no error message)

Done some research and made some changes, which are below:

  1. Added android:largeHeap="true" to the application tag in the AndroidManifest.xml

  2. Added

    dexOptions {
        javaMaxHeapSize "4g" 
    }
    

    in the android/app/build.gradle file.

  3. Added the below code to gradle.properties

    org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError
    org.gradle.daemon=true
    org.gradle.parallel=true
    org.gradle.configureondemand=true
    

Still I am not able to get rid of this error. Any permanent way to get rid of this error and how is it caused??

Java Solutions


Solution 1 - Java

configuring gradle.properties:

org.gradle.jvmargs=-Xmx4096m -XX:MaxPermSize=4096m -XX:+HeapDumpOnOutOfMemoryError
org.gradle.daemon=true
org.gradle.parallel=true
org.gradle.configureondemand=true

Solution 2 - Java

Although the given answer did not work for me, it did lead me to the correct path. Sometimes the build would go through but then again after some time this error would resurface, so to resolve it once and for all:

First comment org.gradle.jvmargs=-Xmx4096m inside your project's gradle properties.

Add the following in your app/build.gradle:

android {

  dexOptions {
    javaMaxHeapSize "4g"
  }

}

Now edit your global gradle.properties for mac it'll be inside Home/YOUR_USERNAME/.gradle/ Note that .gradle is a hidden folder.

If the file is not there simply create it and add

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFortuneCookieView Question on Stackoverflow
Solution 1 - JavaAurangzaib RanaView Answer on Stackoverflow
Solution 2 - JavaSurbhit RaoView Answer on Stackoverflow