143

I've been using Android Studio for 3 months now and one of the apps I started on it has become fairly large. The memory usage indicated at the bottom right of the program says my allocated heap is maxed at 494M.

enter image description here

When I start to change the XML files my memory usage quickly reaches that cap and the IDE crashes with an Out Of Memory error like this.

enter image description here

I've tried to increase the heap size using this but so far there has been no effect.

I've looked at dozens of articles and other questions on how to increase the heap size but none of their answers are working. No matter what I do to the VMOPTIONS or the IDE settings the heap size never increases. I believe I am editting the correct file for the VMOPTIONS because if I purposely give it an invalid command Android Studio complains about it and doesn't start.

I'm using windows 7 - 64 bit and have 16GB RAM. Has anyone else had this problem with Android Studio? And were you able to fix it?

zafrani
  • 4,030
  • 5
  • 31
  • 39
  • See @moxi's answer below for the most up-to-date information on configuring user-specific settings for memory and studio JVM startup http://stackoverflow.com/a/29057416/396005 – Bron Davies Dec 22 '15 at 19:02
  • 2
    Updated Android Studio documentation: https://developer.android.com/studio/intro/studio-config#adjusting_heap_size – Morrison Chang May 22 '18 at 22:12

25 Answers25

162

-------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)

Wesley
  • 4,084
  • 6
  • 37
  • 60
  • 2
    This fixed a problem for me on a very large project where Android Studio would run out of memory after 15mn whilst building the symbol map. I changed Xms to 1024m and Xmx to 4096 and the symbol map was built in under 30 sec - so the garbage collector must have been thrashing for a long time. – RikMaxSpeed Jan 13 '16 at 10:23
  • 14
    As noted by Lord Flash: As of Android Studio 2.0, you can create/edit this file by accessing "Edit Custom VM Options" from the Help menu. – Lasse Magnussen Dec 16 '16 at 23:18
  • 1
    Thx, it's very usefull. FYI, if you use the 64bits version, the file to edit is named studio64.exe.vmoptions. – Eselfar Jul 11 '17 at 14:47
  • @RikMaxSpeed, how much maximum have you obtained the heap size in your project. I am working on an app where I need around 1GB heap size for my static variables so the variables are available anytime but using largeHeap don't support that much storage size. It throws OutOfMemory around 600 MB which is the maximum memory android system provides my app at runtime – Umair Oct 09 '20 at 05:38
  • 1
    increasing xmx doesn't increase it! I still get the same error box – Muhammad Sarim Mehdi May 17 '22 at 11:59
53

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.

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
zafrani
  • 4,030
  • 5
  • 31
  • 39
  • 9
    There was no System Variable called _JAVA_OPTIONS. but i manually created new one and it works fine. thanx. – Yasir Ali Sep 27 '13 at 11:03
  • Good, this works! I'm wondering if to increase the heap size is a good thing... I mean, it works and surely in certain cases is the right way, but is it always the right way? Or may there be better solutions? Thanks! – Marino Nov 06 '14 at 01:43
  • is this problem also due to the heap size http://stackoverflow.com/questions/29148374/how-to-solve-low-disk-space-for-android-studio – Sagar Devanga Aug 14 '15 at 07:38
  • Didn't work for me either, but it turns out Android Studio had let some Java processes hanging from previous executions, and they were all obviously still using a lot of memory. – Alex Machado Dec 11 '15 at 19:29
  • just comment out this line **org.gradle.jvmargs=-Xmx2048m** – Abili Isaac Jun 21 '21 at 04:59
48

Or, you can go to your android-studio\bin folder and change these -Xmx and -Xms values in studio.exe.vmoptions or studio64.exe.vmoptions files (depending on which version you are running).

croc
  • 1,416
  • 1
  • 18
  • 24
40

You should not edit any files in the IDE installation directory. Instead, you can customize the attributes by creating your own .properties or .vmoptions files in the following directories. (This has been possible on some platforms before, but it required you to copy and change the entire contents of the files. With the latest changes these properties are now additive instead such that you can set just the attributes you care about, and the rest will use the defaults from the IDE installation).

Note: As of Android Studio 2.0, you can create/edit this file by accessing the "Edit Custom VM Options" file from the Help menu.

http://tools.android.com/tech-docs/configuration

enter image description here

Tobias
  • 7,282
  • 6
  • 63
  • 85
37

I increased my memory following the next Google documentation:

http://tools.android.com/tech-docs/configuration

By default Android Studio is assigned a max of 750Mb, I changed to 2048Mb.

I tried what google described but for me the only thing that it worked was to use an environment variable. I will describe what I did:

First I created a directory that I called .AndroidStudioSettings,

  • mkdir .AndroidStudioSettings

Then I created a file called studio.vmoptions , and I put in that file the following content:

-Xms256m 
-Xmx2048m 
-XX:MaxPermSize=512m 
-XX:ReservedCodeCacheSize=128m 
-XX:+UseCompressedOops 

Then I added the STUDIO_VM_OPTIONS environment variables in my .profile file:

  • export STUDIO_VM_OPTIONS=/Users/youruser/.AndroidStudioSettings/studio.vmoptions

Then I reload my .profile:

  • source ~/.profile

And finally I open Android Studio:

  • open /Applications/Android\ Studio.app

And now as you can see using the status bar , I have more than 2000 MB available for Android Studio:

enter image description here

You can customize your values according to your need in my case 2048Mb is enough.

UPDATE : Android Studio 2.0 let's you modify this file by accessing "Edit Custom VM Options" from the Help menu, just copy and paste the variables you might want to keep in order to increase it for everversion you might have on your box.

moxi
  • 1,390
  • 20
  • 21
  • Wouldn't this solution work only when opening AS from shell? – guy.gc Jan 19 '16 at 09:15
  • Where are you typing the commands such as `export STUDIO_VM_OPTIONS=/Users/youruser/.AndroidStudioSettings/studio.vmoptions` in the terminal? – COYG May 12 '16 at 00:35
16

You can try any one of the following

1)Change the gradle.properties file and change the heap size as per your requirement.

If org.gradle.jvmargs=-Xmx2048M is not sufficient then change to 4096 as given

org.gradle.jvmargs=-Xmx4096M enter image description here

2)"Edit Custom VM Options" from the Help menu.

It will open studio.vmoptions / studio64.exe.vmoptions file.

enter image description here Change the content to

-Xms128m

-Xmx4096m

-XX:MaxPermSize=1024m

-XX:ReservedCodeCacheSize=200m

-XX:+UseCompressedOops

Save the the file and restart Android Studio.

Nithin
  • 932
  • 7
  • 14
15

Android Studio 3.1 has option to edit your customize virtual memory options.

You can go Android Studio > Help > Edit Custom VM Options

enter image description here

Then paste below settings code to studio64.exe.vmoptions file & Save it.

file location : "\Users\username\.AndroidStudio3.**\config\"

-Xms128m
-Xmx4096m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=200m
-XX:+UseCompressedOops
Ahad Porkar
  • 1,666
  • 2
  • 33
  • 68
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
14

If you are using MAC BOOK, this option is available inside Applications -> Right click Android Studio then choose Show Package contents -> bin.

enter image description here

or

open -e /Applications/Android\ Studio.app/Contents/bin/studio.vmoptions

Then increase Xmx value

-Xms128m
-Xmx2048m
-XX:MaxPermSize=350m
-XX:ReservedCodeCacheSize=64m
-XX:+UseCodeCacheFlushing
-XX:+UseCompressedOops

Now your Android Studio will be super-fast.

mariopce
  • 1,116
  • 9
  • 17
11

Note: I now this is not the answer for the post, but maybe this will be helpful for some one that is looking.

If Nothing of this works for you, try on a Mac this to see if helps you, in the last version of Android Studio, the studio.vmoptions is inside the AndroidStudio.app in your Applications folder.

So right click or ctrl click on your AndroidStudio.App and then select show package content the studio.vmoptions is in:

Contents/bin/studio.vmoptions

Replace or change it and you will get all the RAM you need.

enter image description here enter image description here

Regards.

Stornu2
  • 2,284
  • 3
  • 27
  • 47
  • You should not change that file, but the one stated here: http://tools.android.com/tech-docs/configuration. For Mac: ~/Library/Preferences/{FOLDER_NAME}/studio.vmoptions – GaRRaPeTa Mar 08 '16 at 09:39
7

I tried the _JAVA_OPTIONS thing but it wasn't working for me still.

In the end, what worked for me was the following:

  • Launching studio64.exe instead of the studio.exe(I've got a 64-bits machine).
  • Add/Change the following values in "studio64.exe.vmoptions":

-Xms2048m
-Xmx2048m
-XX:MaxPermSize=1024m
-XX:+CMSClassUnloadingEnabled
-XX:+CMSPermGenSweepingEnabled 
-XX:+HeapDumpOnOutOfMemoryError
-Dfile.encoding=utf-8
ExpensiveBelly
  • 444
  • 5
  • 8
6

There are a lot of answers that are now outdated. The desired way of changing the heap size for Android Studio recently changed.

Users should now create their own vmoptions file in one of the following directories;

Windows: %USERPROFILE%\.{FOLDER_NAME}\studio64.exe.vmoptions

Mac: ~/Library/Preferences/{FOLDER_NAME}/studio.vmoptions

Linux: ~/.{FOLDER_NAME}/studio.vmoptions and/or ~/.{FOLDER_NAME}/studio64.vmoptions

The contents of the newly created *.vmoptions file should be:

-Xms128m
-Xmx750m
-XX:MaxPermSize=350m
-XX:ReservedCodeCacheSize=96m
-XX:+UseCompressedOops

To increase the RAM allotment change -XmX750m to another value.

Full instructions can be found here: http://tools.android.com/tech-docs/configuration

Charles Durham
  • 2,445
  • 16
  • 17
  • 1
    but there is no file `studio.vmoptions` at the path you mentioned. they are under `%AndroidStudioInstallationPath%\bin\studio.exe.vmoptions` – Mulgard Mar 04 '16 at 12:05
  • This is the right answer. Others are obsolete or discouraged. – GaRRaPeTa Mar 08 '16 at 09:40
6

First check how much memory allocated for your android studio to check that follow this steps :

File -> Settings->Appearance & Behavior->Appearance

then check the show memory indicator option like below image red highlighted part enter image description here

In my case my RAM is 12Gb so i have allocated memory for android studio 6gb .To edit that follow this steps

Help->Edit custom VM options

-Xmx6g

In my case, i have set it 6gb because my pc ram is 12 GB. Its upto you how much memory you want to allocate to your android studio

Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
6

This is how you change the heap size currently in Android Studio 3.6.1

On Windows in your Android Studio:

  1. Select File -> Settings (the popup below will be displayed)

Settings Dialog

  1. Select Appearance & Behavior
  2. Select System Settings
  3. Select Memory Settings

Then change the IDE Heap Size settings to your desired value

On Mac:

  1. Select Android Studio
  2. Select Preferences

The same pop up will appear. However, this time on the Mac its called Preferences.

Follow the same steps as the Windows version of Android Studio to adjust the heap size wants the Preferences dialog has been displayed

George
  • 2,865
  • 6
  • 35
  • 59
5

On Mac OSX one can easily change heap size by going to first menu item, Android Studio > preference > System Settings (left menu) > Memory Settings and change heap size there in the dialog.

enter image description here

TomiL
  • 671
  • 2
  • 11
  • 25
TheTechGuy
  • 16,560
  • 16
  • 115
  • 136
3

On Windows 7, the configuration files at [INSTALL-LOCATION]\bin seem to be ignored. Per the following Google documentation, the file to be modified should be here: %USERPROFILE%\.AndroidStudio\studio[64].exe.vmoptions

http://tools.android.com/tech-docs/configuration

Odd that there is no such file upon a clean install of Android Studio 1.1 from the developer site. And they are in the old (ignored) location. Once I copy the file (studio.exe.vmoptions in my case) over and modify it, the change is respected.

However, the fun doesn't stop there. There's an upper value for -Xmx which may be related to the amount of memory in your system. I just bumped my machine's memory from 4Gb to 16Gb, and assumed I could set -Xmx to 2048m, but I found that if I set it to anything larger than 1500m, Android Studio would silently fail to launch with no indication at all as to why. I have more RAM on the way, so it will be interesting to see if I can increase the value at that point.

I hope this additional information is helpful. While all of the above replies were undoubtedly true at one point (and may still be in some environments), I found that this was the only approach that increased the memory used by AS for me.

Groovee60
  • 268
  • 5
  • 7
2

Had this Xms and Xmx memory low issue happen to me any time I was working with the XML. I also tried increasing this memory, only to find that it just took a little longer for it to happen again.

After getting very frustrated and almost deciding to convert all my current projects back over to Eclipse, which I did not want to do, I figured out what was causing it and was able to repeat this failure and prevent it every time.

While editing the XML in (Text view), and using the "Preview" render view, this causes the loss of memory, every time. Turning off "Preview" and using the Design tab to render the screen only, I am able to use Android Studio all day long, with no crash.

I wish this could be fixed for good, because it would be very nice to use the "Preview" render while editing the XML, however I am glad I can keep using Android Studio.

2

Open studio.vmoptions and change JVM options

studio.vmoptions locates at /Applications/Android\ Studio.app/bin/studio.vmoptions (Mac OS). In my machine, it looks like

-Xms128m
-Xmx800m
-XX:MaxPermSize=350m
-XX:ReservedCodeCacheSize=64m
-XX:+UseCodeCacheFlushing
-XX:+UseCompressedOops

Change to

-Xms256m
-Xmx1024m
-XX:MaxPermSize=350m
-XX:ReservedCodeCacheSize=64m
-XX:+UseCodeCacheFlushing
-XX:+UseCompressedOops

And restart Android Studio

See more Android Studio website

Bao Le
  • 16,643
  • 9
  • 65
  • 68
2

I found on on Windows 7/8 (64-bit), as of Android Studio 1.1.0:

[INSTALL_DIR]\bin\studio64.exe.vmoptions be used (if it exists), otherwise it would always fall back to %USERPROFILE%.\AndroidStudio\studio[64].exe.vmoptions

If you want the settings managed from %USERPROFILE%.\AndroidStudio\studio[64].exe.vmoptions, just delete the one in the installation directory.

Jay Sidri
  • 6,271
  • 3
  • 43
  • 62
2

Go in the Gradle Scripts -> local.properties and paste this

`org.gradle.jvmargs=-XX\:MaxHeapSize\=512m -Xmx512m`

, if you want to change it to 512. Hope it works !

2

you are not supposed to modify the bin/studio.exe.vmoptions file, which will be verified during applying update patch.

Solutions are here http://tools.android.com/tech-docs/configuration

copy that file into following location, then change the -Xmx1280m to whatever you want.

Windows:

%USERPROFILE%\.{FOLDER_NAME}\studio.exe.vmoptions and/or %USERPROFILE%\.{FOLDER_NAME}\studio64.exe.vmoptions

%USERPROFILE%\.{FOLDER_NAME}\idea.properties

Mac:

~/Library/Preferences/{FOLDER_NAME}/studio.vmoptions ~/Library/Preferences/{FOLDER_NAME}/idea.properties

Linux:

~/.{FOLDER_NAME}/studio.vmoptions and/or ~/.{FOLDER_NAME}/studio64.vmoptions

~/.{FOLDER_NAME}/idea.properties

Community
  • 1
  • 1
landerlyoung
  • 1,693
  • 17
  • 14
  • 1
    It is safer to use IntelliJs/Android Studios own function to do it: Open the IDE an click "Help" - > "Edit custom VM options" – Thommy Aug 10 '16 at 14:09
2

On Ubuntu, it's straight forward, clicking Help > Edit Custom VM Options . The IDE asks whether to create for you a file, click OK.

An empty studio64.vmoptions file is create and paste the following variables:

-Xms128m
-Xmx4096m
-XX:MaxPermSize=1024m
-XX:ReservedCodeCacheSize=200m
-XX:+UseCompressedOops

Restart the IDE, the Max Heap Size is increased to: 4,062M

TrevorDeTutor
  • 683
  • 6
  • 11
1

May help someone that get this problem:

I edit studio64.exe.vmoptions file, but failed to save.

So I opened this file with Notepad++ in Run as Administrator mode and then saved successfully.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
1

IF by changing or creating the .studio.exe.vmoptions doesn't work, then try changing the gradle.properties file and change the heap size as per your requirement.

It really worked for me on my Windows 7 with 4Gb RAM and Android Studio 2.2 install on it.

Working properly with no error and displaying 'Gradle Sync complete'

Dishant J
  • 189
  • 1
  • 3
  • I had to add a grade.properties in order to set max heap size that was used when I debug my app. But I have to ask, why doesn't setting the studio.vmoptions as mentioned in the Android Studio configuration page not work as advertised? My concern is that I am doing something fundamentally wrong (but it sure doesn't look like it). (Thanks) I posted a detailed question at: http://stackoverflow.com/questions/40833031/maximum-heap-size-not-being-set-as-indicated-in-studio-vmoptions-file – JimCzek Nov 28 '16 at 17:34
0

-Xms256m 
-Xmx2048m 
-XX:MaxPermSize=512m 
-XX:ReservedCodeCacheSize=128m 
-XX:+UseCompressedOops 
Kamil
  • 9
  • 2
0

As of 2022, you may check this link here. https://developer.android.com/studio/intro/studio-config.html

Windows

Syntax: %APPDATA%\Google\<product><version>

Example: C:\Users\YourUserName\AppData\Roaming\Google\AndroidStudio4.1

macOS

Syntax: ~/Library/Application Support/Google/<product><version>

Example: ~/Library/Application Support/Google/AndroidStudio4.1

am5a03
  • 506
  • 7
  • 24