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:

java

# 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

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.