How to stop android studio from crashing the whole computer?

I installed android studio and it crashed several times. I installed an older version (Arctic fox) and it did the same. It never managed to install gradle. I installed gradle via the command line (using apt). Then android studio complained that the gradle libraries were newer. And crashed. I installed the latest version of android studio via the command line. It goes a bit further sometimes, I was able to create a new project and to give it a name. But I can’t do anything else.

Hardware acceleration is unavailable on my machine.

Each time, I have to hard reboot the computer.

It doesn’t always crash at the same stage. It starts doing something, for example indexing files, and then it crashes. Currently, it’s going to crash while building gradle. I test it by moving the mouse pointer. When the mouse pointer stops moving completely, I know nothing else is going to happen and I press the button.

There you go, while I’m typing this, it has crashed the other computer. Power off.

Is there a way to contain it, to slow it down, to stop it from using all the RAM? I wouldn’t mind giving it more time to finish whatever it thinks it’s got to do, even leaving it to run overnight, but I would like the rest of the computer to remain functional.

Is there any lighter alternative? Should I reinstall lubuntu and everything leaving out the android preview? (Anyway I can’t use it currently because I never found the "Split" button which google mentions in it’s android tutorial). I don’t really mind doing a new lubuntu install as this is a new machine. I have nothing personal on it except a nice desktop wallpaper.

All suggestions welcome.

Asked By: Alain Reve


Solution 1: Lower Android Studio’s JVM’s max memory

You can tell Android Studio to use less memory if your computer doesn’t have enough.

  1. Open the file ~/.config/Google/AndroidStudio20*/studio64.vmoptions
  2. Replace (add if missing) -Xmx* (like -Xmx4096m) with lower values such as -Xmx2048m
  3. Set -Xms to the same value
  4. Save
  5. Restart Android Studio

Solution 2: Lower Gradle’s JVM’s max memory

You can have Gradle use less memory when building projects.

  1. Open or create the file ~/.gradle/
  2. Add or merge the line org.gradle.jvmargs=-Xms1G -Xmx1G
  3. Add or merge the line kotlin.daemon.jvmargs=-Xms1G -Xmx1G
  4. Locally delete those lines in in any Android project you are working in before opening them, to avoid them overriding the above settings
  5. Kill all processes related to Gradle, or reboot

Solution 3: Limit it with a cgroup

cgroups are a way to impose many resource consumption limits on a specific set of processes. /sys/fs/cgroup is complicated, so let’s just use systemd-run.

  1. Enter a subshell in a new memory-limited cgroup with systemd-run --user --scope -p MemoryMax=2G bash
  2. Start Android Studio with ~/.local/share/JetBrains/Toolbox/apps/android-studio/bin/ or wherever you installed it

Solution 4: Reduce Android development resource consumption

There are other tips you can use while developing Android projects:

  • Disable "File > Power Save Mode". You said it crashes when indexing files, so this will help by disabling indexing. Note that this might cause many of the smart features to stop working
  • Use the command line. You don’t need an IDE to develop Android projects. You can just run ./gradlew in the project folder to build, package, and install the app. You can edit the source files in any text editor.

Solution 5: Stop whole computer crashes at OOM

Your whole computer should not crash when a program tries to use too much memory. It should only kill the most memory-hogging program.

  • Install and properly configure systemd-oomd
  • If that’s not possible, disable swap
Answered By: Daniel T
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.