The start script <install-path>/android-studio/bin/studio.sh does consult the files as follows to read configurations.
For 64bit systems:
<install-path>/android-studio/bin/studio64.vmoptions
${HOME}/.AndroidStudio<version>/config/studio64.vmoptions
<install-path>/android-studio.vmoptions
For 32bit systems:
<install-path>/android-studio/bin/studio.vmoptions
${HOME}/.AndroidStudio<version>/config/studio.vmoptions
<install-path>/android-studio.vmoptions
Depending on your architecture 32bit vs 64bit and how you did change the heap size setting, you can look through the files mentioned above and alter the Xmx with a text editor of your choice.
Thank you so much for myself. I got it in 2 hours. After searching too much on google, found nothing that works for me.
This is the only way to solve it.
Note : I'm using Ubuntu with 4g ram 64bit
Step 1. Go to file manager > show hidden files
Step 2. Go to folder > .config/Google/AndroidStudio-versionName
Step 3. Delete studio64.vmoptions file
Step 4. Restart Android Studio
I resolved it by removing this line in the gradle.properties file:
org.gradle.jvmargs=-Xmx4G
How to solve this fatal exception on Android Studio: Invalid maximum heap size: -Xmx4096m? - Stack Overflow
jvm - Android studio, Invalid maximum heap size - Stack Overflow
Android Studio: Invalid maximum heap size: -XmxheapSize, Error:Could not create the Java Virtual Mchine - Stack Overflow
Android Studio 1.5.1 - Invalid maximum heap size: -Xmx4g The specified size exceeds the maximum representable size - Stack Overflow
Videos
-------EDIT--------
Android Studio 2.0 and above, you can create/edit this file by accessing "Edit Custom VM Options" from the Help menu.
-------ORIGINAL ANSWER--------
Open file located at
/Applications/Android\ Studio.app/Contents/bin/studio.vmoptions
Change the content to
-Xms128m
-Xmx4096m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=200m
-XX:+UseCompressedOops
Xmx specifies the maximum memory allocation pool for a Java Virtual Machine (JVM), while Xms specifies the initial memory allocation pool. Your JVM will be started with Xms amount of memory and will be able to use a maximum of Xmx amount of memory.
Save the studio.vmoptions file and restart Android Studio.
Note:
If you changed the heap size for the IDE, you must restart Android Studio before the new memory settings are applied. (source)
I looked at my Environment Variables and had a System Variable called _JAVA_OPTIONS with the value -Xms256m -Xmx512m, after changing this to -Xms256m -Xmx1024m the max heap size increased accordingly.
I'm currently using Android Studio with 64GB of available RAM on my system. Despite setting the maximum heap size to 32GB in the studio.vmoptions file, Android Studio only utilizes around 7GB and starts lagging after a while. I find myself needing to restart it every hour during coding sessions. My current configuration is:
-Xms128m -Xmx32768m -XX:MaxPermSize=1024m -XX:ReservedCodeCacheSize=200m -XX:+UseCompressedOops -Didea.kotlin.plugin.use.k2=true
While browsing the internet and running other applications simultaneously, only Android Studio lags. Is there a way to force Android Studio to utilize more RAM or improve its performance?
Update 2022; DSL element 'dexOptions' is obsolete and should be removed.
It will be removed in version 8.0 of the Android Gradle plugin.
Using it has no effect, and the AndroidGradle plugin optimizes dexing automatically.
I tested and with Gradle 7.0+, increasing below mentioned org.gradle.jvmargs option is all we need.
Original Answer
I had a similar issue on Android Studio 2.3.3. APK packaging would fail saying Java heap space when building from Android Studio and trying to process a 300MB .so library. In my case two things were needed to fix it. Your build.gradle should contain something like that:
android {
dexOptions {
javaMaxHeapSize "4G"
}
}
At the root of your project you need to have a file named gradle.properties containing the following:
# That's needed for java to be able the create the APK with our 300MB native library
# Basically we grant java a 4GB heap.
org.gradle.jvmargs=-Xmx4608m
This is old, but just add this to the build file
dexOptions {
javaMaxHeapSize "2g"
}
My Android Studio was working fine. But executing Gradle from command line gave this same error 'could-not-reserve-enough-space-for-1572864kb-object-heap'
Looks like, it was because Gradle couldn't find java
Defined JAVA_HOME system variable, pointing to the same path Android studio was using. (get it from Studio > project structure > SDK Location > JDK Location)
Worked for me.
Open gradle.properties:
Max limit from Gradle 5.4.1 document:
If the requested build environment does not specify a maximum heap size, the Daemon will use up to 512MB of heap.
org.gradle.jvmargs=-Xmx512m
Or you can remark memory setting. The system will set by itself.
The rest of content of my file:
======================================================================
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
======================================================================
Now, File >> Sync Project with Gradle Files
