How to pass JVM args to the Java toolchain?

Using Gradle 6.8.3.

I’m defining my toolchain in my build.gradle:

java {
	toolchain {
		languageVersion = JavaLanguageVersion.of(8)
	}
}

Gradle, instead, is running with Java 11. Problem is that, even if I run Gradle passing -Xmx1g to it, it seems not to pass this JVM arg to the Java 8 compiler when it compiles my project. Since compiling this project fails due to OutOfMemoryError if I do not increase the heap space, I can’t build my project.

So, question is: how to make Gradle pass the -Xmx1g parameter to the forked JVM specified as a toolchain it uses to compile my project?

Even if you would not use toolchain you would have the same problem.
Gradle starts various processes, from the CLI, over the Gradle daemon, various worker and test execution daemons and also compile processes.
If you compilation needs more RAM, then configure the RAM for that exact task, like for example

tasks.compileJava {
    options.forkOptions.memoryMaximumSize = "1g"
}

Thank you very much Björn, it works!

1 Like