Android Studio or its Gradle build runs out of Java heap memory
OutOfMemoryError with the detail message Java heap spaceShort 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.
Fix it fast
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.properties2Match what you see, then jump to its fix
- 1
The editor freezes or a memory warning appears while the Build output is clean.The IDE heap is too small for the projectFix - 2
The 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
The 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
- 1
What does the error look like?
OutOfMemoryError with the detail message Java heap spaceAndroid 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.vmoptions2Fix
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)
-Xmx4g3Verify
After the restart the Memory Settings page shows the new value and the freezes stop.
# Settings > Appearance & Behavior > System Settings > Memory SettingsConfirmed by developer.android.com
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.properties2Fix
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=-Xmx4g3Verify
Stop the old daemon and build again.
./gradlew --stop
./gradlew assembleDebugConfirmed by developer.android.com
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 -h2Fix
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=-Xmx1536m3Verify
The IDE stays responsive while a build runs.
./gradlew assembleDebugConfirmed by developer.android.com
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 fixMobile apps
flutter run hangs for minutes on Running Gradle task 'assembleDebug'
Downloading https://services.gradle.org/distributions/gradle-8.14-all.zip
That line only says Gradle started; the wait is inside Gradle.
3 causes, each with a fixMobile apps
flutter doctor cannot find the Java that comes with Android Studio
Unable to find bundled Java version.
Flutter looks for the Java inside Android Studio first, then JAVA_HOME, then any java on PATH.
3 causes, each with a fixMobile apps
Gradle ignores JAVA_HOME and starts a different JDK
Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
Gradle does not read JAVA_HOME first.
3 causes, each with a fixMobile apps
Flutter Android build fails with Unsupported class file major version
Unsupported class file major version 65
The number is the Java version that tried to read a file: 61 is Java 17, 65 is Java 21.
3 causes, each with a fixMobile apps
adb cannot start because something already holds its port
adb start-server failed
The adb server must bind local TCP port 5037, and every adb client talks to it there.
2 causes, each with a fix