Android Studio - How to increase Allocated Heap Si

2019-01-01 15:29发布

问题:

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

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

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?

回答1:

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.

More at this post.

---------- EDIT ----------

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



回答2:

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



回答3:

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.



回答4:

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

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.



回答5:

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



回答6:

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 \"enter

Regards.



回答7:

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



回答8:

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

\"enter

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.



回答9:

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



回答10:

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

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



回答11:

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.



回答12:

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.



回答13:

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 !



回答14:

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



回答15:

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.



回答16:

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



回答17:

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.



回答18:

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



回答19:

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\'



回答20:

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

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

\"enter

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

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