Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

Android Studio or its Gradle build runs out of Java heap memory

ErrorOutOfMemoryError with the detail message Java heap space

Short answer

Two separate Java processes can run out of heap: the IDE itself and the Gradle daemon that builds your app. Raise the IDE limit in Memory Settings or studio.vmoptions, and the build limit in org.gradle.jvmargs in gradle.properties. Which one to change depends on where the error appears.

  • Applies to Android Studio current stable, Gradle 8.x and 9.x, Android Gradle Plugin 8.x and 9.x, 64-bit Windows, macOS 26, Ubuntu 24.04
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Note where the error shows. In the Build output it is the Gradle daemon; in an IDE popup or a frozen editor it is the IDE. Then check what limits are set now.

    grep -n "jvmargs" gradle.properties ~/.gradle/gradle.properties
  2. 2Match what you see, then jump to its fix

    1. 1The editor freezes or a memory warning appears while the Build output is clean.The IDE heap is too small for the projectFix
    2. 2The failure shows in the Build output, or a Gradle task fails with an out-of-memory message.The Gradle daemon heap is too small for the buildFix
    3. 3The machine swaps, the IDE is slow even after raising heap, or the OS kills Java.The computer has little RAM, and bigger limits make things worseFix

What does the error look like?

  • OutOfMemoryError with the detail message Java heap space
  • Android Studio shows a notification about recommended memory settings

Cause 1 of 3

The IDE heap is too small for the project

1Check

Android Studio's default maximum heap is 1280 MB. Open Help > Edit Custom VM Options to see the file the IDE reads, and check the -Xmx line in it.

# Help > Edit Custom VM Options opens studio.vmoptions

2Fix

Open Settings > Appearance & Behavior > System Settings > Memory Settings, raise the IDE heap, and restart Android Studio. Equivalent: add an -Xmx line to your studio.vmoptions, which is added to the default file. Edit only your own file, not the one in the install folder.

# studio.vmoptions (your own file, via Help > Edit Custom VM Options)
-Xmx4g

3Verify

After the restart the Memory Settings page shows the new value and the freezes stop.

# Settings > Appearance & Behavior > System Settings > Memory Settings

Confirmed by developer.android.com

Did this fix it?

Cause 2 of 3

The Gradle daemon heap is too small for the build

1Check

The daemon takes its limit from org.gradle.jvmargs. The Android docs name 1,536 MB as the default maximum heap for Gradle.

grep -n "jvmargs" gradle.properties

2Fix

Set a larger value in the project's gradle.properties, or in the user-level file to cover every project, then stop the old daemon.

# gradle.properties
org.gradle.jvmargs=-Xmx4g

3Verify

Stop the old daemon and build again.

./gradlew --stop
./gradlew assembleDebug

Confirmed by developer.android.com

Did this fix it?

Cause 3 of 3

The computer has little RAM, and bigger limits make things worse

1Check

Allocating too much memory can slow everything down. Compare the limits you set with the physical RAM left after the emulator and browser are running.

# Task Manager, Activity Monitor or free -h

2Fix

Turn on File > Power Save Mode, switch off Compile independent modules in parallel, and use a physical device instead of the emulator. Lower the IDE or Gradle heap step by step instead of raising it.

# gradle.properties
org.gradle.jvmargs=-Xmx1536m

3Verify

The IDE stays responsive while a build runs.

./gradlew assembleDebug

Confirmed by developer.android.com

Did this fix it?

How do I stop it coming back?

  • Keep the Android Gradle Plugin and Gradle updated; the Android docs list newer versions as a performance gain.
  • Do not set the IDE and Gradle heaps so high that together they exceed free RAM.
  • Close the emulator when you are not using it.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Mobile apps

Flutter's Android build stops: JAVA_HOME is not set

ERROR: JAVA_HOME is set to an invalid directory

The Gradle wrapper script needs a Java runtime and looks in two places: the JAVA_HOME variable, then a java command on PATH.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.