Optimizing Gradle Daemon’s memory and cache usage

When we build our project with Gradle, either by command line or by Android Studio, a long-running background process that runs the builds called Daemon is used.
The Daemon is used not only to avoid the cost of starting the JVM for each build but also to cache information about the project structure, files, tasks, and more in memory. This makes the next compilations much faster.
Android Studio contains its own JVM, which is installed inside it and is used for its builds within the IDE, therefore, its own Daemon instance.
So far so good … Right? But if you also use Gradle by command line, it will not be using the instance started by the IDE, but a new one as the terminal will be using your machine’s JVM.
What does that mean? That you will have two instances of the Daemon with cache and everything else running on your machine and consuming more memory.
Also, if you modify the file system present in one of the builds, when using the second one it will build again because it does not know about the changes in the first.
How to solve this? Use the same java in both places.
💻 Setting the environment variable
In the terminal, type echo
$JAVA_HOME
to find out if the variable has been previously configured. If so, skip this step.
In this step, you should identify where your Java is installed and add your installation path as an environment variable under the name of JAVA_HOME
.
macOS
Probably, Java is located in /usr/libexec/java_home
.
On file ~/.bashrc
if you use bash, or ~/.zshrc
if you use zsh, add the line with the path between inverted quotes:
export JAVA_HOME=`/usr/libexec/java_home`
Restart the terminal, or run the command source ~/.bashrc
if you use bash, or source ~/.zshrc
if you use zsh, to reload the file without having to restart it.
Windows
Probably, Java is located in C:\Program Files\Java\jdk1.8.0_XX/
.
In your terminal, entersetx -m JAVA_HOME "C:\Program Files\Java\jdk1.8.0_XX"
.
Restart your terminal.
Linux
Probably, Java is located at /usr/lib/jvm/java-1.x.x-openjdk
or usr/bin/javac
. You can also run the update-alternatives — config java
command that lists the installed jdk options and their respective directories.
On file/etc/profile
, add the line with the found path:
export JAVA_HOME=usr/bin/javac
Restart your terminal.
If you need help, you can count on this link directly from the Java website
🤖 Setup on Android Studio
For safety protection, for Android Studio to recognize JAVA_HOME
, restart your IDE.
With your project opened, go to File ➡️ Project Structure, as shown below.

In JDK Location, select the JAVA_HOME
option, which Android Studio has probably already detected that you have this environment variable configured.

Restart your IDE et voilá! 👌
🛑 Ending all Gradle Daemon if you need to
If after all steps you detect that there are still Gradle Daemons running, you can close them all on your terminal with the command:
pkill -f '.*GradleDaemon.*'
💰Bonus: Make your terminal “rich”
When executing a command per command line, the entire printed log will be in plain text, but if you use the --console=rich
tag after the command to be executed, e.G.: ./gradlew clean --console=rich
, we can leave the more “informative” terminal. What changes:
- Log grouping.
- Progress bar and stopwatch that describe the overall status.
- The parallel lines of tasks in progress.
- Colors and fonts will be used to highlight errors and important results.

To avoid the hassle of always having to add the --console=rich
tag at the end of each command, you can configure it by default using a file called gradle.properties
directly in the Gradle installed on your machine.
- Create a file called
gradle.properties
inside your~/.gradle/
folder (on Gradle settings in your “home” folder). - Within the file
gradle.properties
, add the lineorg.gradle.console=rich
.
Each command will be executed automatically with --console=rich
because the new gradle.properties
will be merged with your project’s gradle.properties
.
If your project’s gradle.properties
contains the same tag as the local file, your project’s file tag will be used, overwriting the local file’s tag.
If you want plain text, use
--console=plain
instead of--console=rich
Final thoughts
Not all paths and versions probably will match with your setup, but if you set up it correctly you’ll be able to run a single instance of Daemon/JAVA on Android Studio and terminal. Good code!
Contacts
You can find me on Twitter @orafaaraujo.
Feedback is welcome!
Android Community on Brazil
Join the largest forum on Android in Brazil slack Android Dev BR.
Site: http://www.androiddevbr.org | Invite: http://slack.androiddevbr.org