Home > front end >  Some ./gradlew commands are holding memory (through java processes)
Some ./gradlew commands are holding memory (through java processes)

Time:08-12

I perceived that when I run a ./gradlew task, after the task is done, the system still holds the memory used.

Example:

  • Initial memory usage:
    enter image description here

  • After I run a ./gradlew testBuildVariant1UnitTest task:
    enter image description here
    and a new Java process is created consuming gbs of memory

  • After I run a second ./gradlew testBuildVariant2UnitTest task:
    enter image description here
    and a new Java process is created consuming gbs of memory


The memory is not released until I kill the java processes manually or run the following command:

./gradlew --stop

Is this the expected behavior? Is there something I can do to release the memory automatically after a ./gradlew task is run?

CodePudding user response:

Yes this is expected, it is caused by the Gradle Daemon

Gradle runs on the Java Virtual Machine (JVM) and uses several supporting libraries that require a non-trivial initialization time. As a result, it can sometimes seem a little slow to start. The solution to this problem is the Gradle Daemon: a long-lived background process that executes your builds much more quickly than would otherwise be the case.

Although, the size of the retained memory looks concerning.

Have a look at How to disable the Daemon. It lists several ways to disable it, but keep in mind that this will decrease performance for repeated invocations.

The simplest way is to run with --no-daemon or to add org.gradle.daemon=false to your gradle.properties file.

  • Related