Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle build fails with Out of Memory: Java heap space error

I am getting gradle build error while running the app. I am using windows 10 with 8GB RAM, Android studio version 4.0.1

Out of memory: Java heap space. Please assign more memory to Gradle in the project's gradle.properties file. For example, the following line, in the gradle.properties file, sets the maximum Java heap size to 1,024 MB: org.gradle.jvmargs=-Xmx1024m

Even after following the same steps I am not able to run the app. Could anyone help with a permanent solution for this error???

Here is my gradle.properties file:

android.enableJetifier=true
android.useAndroidX=true
org.gradle.jvmargs=-Xmx1024m
like image 805
Kartika Vij Avatar asked Jul 25 '20 12:07

Kartika Vij


People also ask

How do I fix Java out of memory error heap space?

OutOfMemoryError: Java heap space. 1) An easy way to solve OutOfMemoryError in java is to increase the maximum heap size by using JVM options "-Xmx512M", this will immediately solve your OutOfMemoryError.

What went wrong out of memory Java heap space gradle?

OutOfMemoryError issues occur either in the compile phase of your project or the test phase of your project while building your Gradle project. If the error occurs during compilation, you must increase heap size thus according to your system capacity and use the following lines of code in Linux and Windows.

What went wrong Java heap space?

Usually, this error is thrown when there is insufficient space to allocate an object in the Java heap. In this case, The garbage collector cannot make space available to accommodate a new object, and the heap cannot be expanded further.

How do I give more memory to gradle?

You can set or increase memory usage limits (or other JVM arguments) used for Gradle builds and the Gradle Daemon by editing $GRADLE_USER_HOME/. gradle/gradle.


3 Answers

  1. Give more RAM for gradle org.gradle.jvmargs=-Xmx4g
  2. If you face the same problem after can run for a few times, open Task Manager, End task any Open JDK Platform binary or Java(TM) Se Binary to release RAM, then you can run project again.

If your computer have less than 8GB RAM, set like this in your gradle.properties org.gradle.jvmargs=-Xmx2048M

like image 139
Thuan Tang Avatar answered Oct 28 '22 07:10

Thuan Tang


I faced the same issue in my "react": "17.0.2","react-native": "0.66.1" project
Goto project/android/gradlew.properties
& uncomment this line
# org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

and only change the code before colan(:)
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX to
org.gradle.jvmargs=-Xmx3g -XX:MaxPermSize=512m -XX

then save and run your build

like image 33
abhish Avatar answered Oct 28 '22 08:10

abhish


I'm working on a big android project with multiple modules and flavors and had the same problem.

I tried everything from tweaking memory to cleaning project, restarting while invalidating cache, deleting sources and recloning from repo.

I ended up deleting the ".gradle" folder in my user folder and recloning my project.. and it worked. Lost a lot of time on this though.

like image 39
AlexMok Avatar answered Oct 28 '22 06:10

AlexMok