To limit Android Studio's memory usage, you can modify the studio.vmoptions file. This file contains configuration settings for Android Studio, including memory-related settings. Follow these steps:

  1. Locate the studio.vmoptions file:

    • On Windows, it is usually located in the bin directory of the Android Studio installation directory. For example, C:\Program Files\Android\Android Studio\bin\studio64.exe.vmoptions.
    • On macOS, it is often found in the Contents/bin directory within the application bundle. For example, /Applications/Android Studio.app/Contents/bin/studio.vmoptions.
    • On Linux, it is also typically in the bin directory of the installation. For example, /opt/android-studio/bin/studio64.vmoptions.
  2. Open the studio.vmoptions file in a text editor: Use a text editor like Notepad (on Windows), TextEdit (on macOS), or any code editor of your choice.

  3. Modify the memory settings: Add or modify the following lines to set the maximum heap size (Xmx) and the initial heap size (Xms). Adjust the values according to your system's available memory.

-Xms256m
-Xmx2048m

Example values are used above (-Xms256m for the initial heap size and -Xmx2048m for the maximum heap size). You can increase or decrease these values based on your system's configuration.

  1. Save the changes and restart Android Studio: Save the modifications to the studio.vmoptions file and restart Android Studio for the changes to take effect.

Keep in mind that setting the maximum heap size too high might cause issues if your system doesn't have enough available memory. Adjust the values based on your system's specifications to achieve optimal performance.

Enable automatic memory heap resizing

To add garbage collection options and parallelism while limiting the maximum heap size to 1GB in Android Studio, you can modify the studio.vmoptions file. Here's an example configuration:

Android Studio

# custom Android Studio VM options

# Set the maximum heap size to 1GB
-Xmx1g

# Set the initial heap size
-Xms256m

# Enable parallel garbage collection
-XX:+UseParallelGC

# Enable concurrent garbage collection (for parallel)
-XX:+UseConcMarkSweepGC

# Set the size of the young generation (you may adjust this based on your needs)
-XX:NewSize=512m
-XX:MaxNewSize=512m

# Enable automatic heap resizing
-XX:+UseAdaptiveSizePolicy

Intellij IDEA (Community or Ultimate)

# Use the server JVM, optimized for long-running applications with more aggressive optimizations.
-server 

# Aim to keep GC pause times under 200ms. This affects how the JVM manages memory.
-XX:MaxGCPauseMillis=200

# Trigger garbage collection when 45% of the heap is occupied. Helps manage memory pressure.
-XX:InitiatingHeapOccupancyPercent=45

# Set the ratio of the Eden space (young generation) to the survivor space.
# A ratio of 8 means the Eden space is 8 times larger than a survivor space.
-XX:SurvivorRatio=8

# Allow flushing of compiled code from the code cache to prevent it from running out of memory.
-XX:+UseCodeCacheFlushing

# Reserve 128MB for the code cache, which stores JIT-compiled code.
-XX:ReservedCodeCacheSize=128m

# Enable assertions during runtime for debugging purposes.
-ea

# Disable caching of canonicalized file path strings. 
# This is often useful when working with networked or temporary file systems.
-Dsun.io.useCanonCaches=false


# ===================
# Custom Android Studio VM options
# ===================

# Set the maximum heap size for the JVM to 1GB. Controls the upper limit of memory usage.
-Xmx1g

# Set the initial heap size to 256MB. This is the starting memory allocation.
-Xms256m

# Enable parallel garbage collection to improve throughput by using multiple threads.
-XX:+UseParallelGC

# Enable the concurrent garbage collector, which works alongside the application
# (used in conjunction with parallel GC for balanced performance).
-XX:+UseConcMarkSweepGC

# Set the initial size of the young generation (Eden + survivor spaces) to 512MB.
-XX:NewSize=512m

# Set the maximum size of the young generation to 512MB.
-XX:MaxNewSize=512m

# Enable adaptive resizing of heap spaces to improve performance dynamically based on application behavior.
-XX:+UseAdaptiveSizePolicy

In this configuration:

  • -XX:+UseParallelGC enables the parallel garbage collector.
  • -XX:+UseConcMarkSweepGC enables concurrent garbage collection, which works in conjunction with parallel garbage collection.
  • -XX:NewSize and -XX:MaxNewSize set the size of the young generation, which is part of the heap where new objects are created. Adjust these values based on your requirements.
  • -XX:+UseAdaptiveSizePolicy enables automatic heap resizing based on the application's behavior.

Feel free to adjust these settings based on your system's specifications and the specific needs of your Android Studio projects. Keep in mind that tuning garbage collection settings can be a trial-and-error process, so monitor the performance and adjust as needed. Save the changes, restart Android Studio, and observe the impact on performance.