Different ways to Fix OutOfMemoryError Gradle| Solution in Android studio

Fix OutOfMemoryError error in Gradle

Gradle is a build management tool for java and android projects.

Usually, javalangoutofmemoryerror comes running an android application for the following tasks.

  • Building phase
  • compile phase
  • Package phase
  • Loading app in Android studio
  • Starting Android studio application

Did you get the below errors while compiling the android project?

  • java.lang.OutOfMemoryError: Java heap space
  • java.lang.OutOfMemoryError: Perm Gen space
  • java.lang.OutOfMemoryError: GC overhead limit exceeded
  • The system is out of resources

What is the reason for java.lang.OutOfMemoryError? For example, if the project has a lot of java files say 1000 when you try to build the project using “gradle clean build”, For a successful case, gradle clean build will try to clean/delete the project and compile the project, test the project and build the application.

For the failure case, the build failed with an error because there is enough Heap memory to run large code.

The OutOfMemoryError error occurs when there is enough heap memory for your Gradle process to execute the android project

OutOfMemoryError issues occur either in the compile phase of your project or the test phase of your project while building your Gradle project.

For debugging it, First, find out which phase of your build is occurring

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.

There isn’t enough room in heap memory to allocate your object execution in Java. When this problem occurs in Gradle, you must use the system classloader because the classloader loads the classes, but your application still maintains the references to the classes after execution is complete, which is referred to as a memory leak.

Solution for OutOfMemory Error in Gradle

There are many ways we can fix it

First way, In the Application android/Gradle.properties, Try to add the below properties

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

android.useAndroidX and android.enableJetifier enables to migrate of all support libraries to AndroidX and android team changes are released in androidx, so you will always get the latest clean and performance code. org.gradle.jvmargs contains heap memory configuration which you can increase based on your hardware RAM. Higher is always preferable.

org.Gradle.daemon is a background daemon thread that initializes and runs in the background to cache data, thus improving performance.

Second way,

    javaMaxHeapSize "4g"

The third option, You can pass maxPermsize inside the Gradle task

  jvmArgs "-XX:MaxPermSize=1096m"
  freeCompilerArgs  ['-Xjsr305=strict', '-XXLanguage:-NewInference']

How to fix the OutofMemoryError error in android studio

sometimes, You used to get Out of memory errors When you are loading or starting a project in Android studio.

The application has no memory to run the process for loading in Android studio or project. There are multiple ways to fix this error.

Approach 1: You need to add the following attributes to the application tag in manifest.xml


After adding this code, Try to restart the application, Now your app has enough memory allocated to run the code in RAM.


You have to configure Gradle.properties with maxHeapSize properties as discussed above

Fix for OutOfMemoryError for gradle in windows:-

change the environment variable GRADLE\_OPTS in such a way that increases the heap size by following a piece of code

GRADLE_OPTS="$GRADLE_OPTS  \"-Xmx4096\" \"-Xms1024m\" \"-XX:MaxPermSize=4096\""

Changed the maxPermSize values to 4096

Fix for OutOfMemoryError for gradle in Linux/Unix:-

In Linux and Unix machines, We have to incase the heap size for the environment variable GRADLE\_OPTS.
the processing for the Linux version is different

export GRADLE_OPTS=-Xmx4096M -XX:MaxPermSize=7096M  

Most of the time OutOfMemoryError solves with the above lines of code.


In this tutorial, You learned different ways to solve OutOfMemoryError in Gradle build with the android app.

Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
You'll get a notification every time a post gets published here.