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 android application for following tasks

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

Did you got below errors while compiling 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 lot of java files say 1000, when you try to build the project using “gradle clean build”, For success case, gradle clean build will tries to clean/delete the project and compile the project, and test the project and build the application.

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

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 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 your system capacity and used 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 number of ways we can fix it

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

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

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

org.gradle.daemon is background daemon thread which initializes and runs in background to cache data, thus improves performances.

Second way,

    javaMaxHeapSize "4g"

Thrid option, You can pass maxPermsize inside gradle task

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

How to fix OutofMemoryError error in android studio

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

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 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 way that increase 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 heapsize for environment variable GRADLE\_OPTS.
the processing for Linux version is different

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

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


In this tutorial, You learned an different ways to solve OutOfMemoryError in Gradle build with 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.