Configuring JVM memory

The org.gradle.jvmargs Gradle property controls the VM running the build. It defaults to -Xmx512m "-XX:MaxMetaspaceSize=384m"

You can adjust JVM options for Gradle in the following ways.

Option 1: Changing JVM settings for the build VM:

org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

The JAVA_OPTS environment variable controls the command line client, which is only used to display console output. It defaults to -Xmx64m

Option 2: Changing JVM settings for the client VM:

JAVA_OPTS="-Xmx64m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8"

There is one case where the client VM can also serve as the build VM:

If you deactivate the Gradle Daemon and the client VM has the same settings as required for the build VM, the client VM will run the build directly.

Otherwise, the client VM will fork a new VM to run the actual build in order to honor the different settings.

Certain tasks, like the test task, also fork additional JVM processes. You can configure these through the tasks themselves. They use -Xmx512m by default.

Example 1: Set compile options for Java compilation tasks:

build.gradle.kts
plugins {
    java
}

tasks.withType<JavaCompile>().configureEach {
    options.compilerArgs = listOf("-Xdoclint:none", "-Xlint:none", "-nowarn")
}
build.gradle
plugins {
    id 'java'
}

tasks.withType(JavaCompile).configureEach {
    options.compilerArgs += ['-Xdoclint:none', '-Xlint:none', '-nowarn']
}

See other examples in the Test API documentation and test execution in the Java plugin reference.

Build scans will tell you information about the JVM that executed the build when you use the --scan option:

Build Environment in Build Scan

Configuring a task using project properties

It is possible to change the behavior of a task based on project properties specified at invocation time.

Suppose you would like to ensure release builds are only triggered by CI. A simple way to do this is using the isCI project property.

Example 1: Prevent releasing outside of CI:

build.gradle.kts
tasks.register("performRelease") {
    val isCI = providers.gradleProperty("isCI")
    doLast {
        if (isCI.isPresent) {
            println("Performing release actions")
        } else {
            throw InvalidUserDataException("Cannot perform release outside of CI")
        }
    }
}
build.gradle
tasks.register('performRelease') {
    def isCI = providers.gradleProperty("isCI")
    doLast {
        if (isCI.present) {
            println("Performing release actions")
        } else {
            throw new InvalidUserDataException("Cannot perform release outside of CI")
        }
    }
}
$ gradle performRelease -PisCI=true --quiet
Performing release actions