Setting JAVA_OPTS and JAVA_TOOL_OPTIONS in Gradle
Asked Answered
G

3

9

Running a large Gradle build (with JDK7) I receive two OutOfMemoryErrors:

Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "main"
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread "Test worker" 

When I set the two environment variables below, the build runs through and works just fine:

export JAVA_OPTS="-Xmx2048m -XX:MaxPermSize=1024m"
export JAVA_TOOL_OPTIONS="-Xmx1024m -XX:MaxPermSize=1024m -Xms768m"
./gradlew test --stacktrace
...
Picked up JAVA_TOOL_OPTIONS: -Xmx1024m -XX:MaxPermSize=1024m -Xms768m
...

Is there a way to include those settings in gradle.properties or in build.gradle? If yes, what is the correct usage?

I already tried this in build.gradle:

allprojects {
    System.setProperty('JAVA_OPTS', "-Xmx2048m -XX:MaxPermSize=1024m")
    System.setProperty('JAVA_TOOL_OPTIONS', "-Xmx1024m -XX:MaxPermSize=1024m -Xms768m")
}

but that doesn't work.

Garber answered 8/5, 2017 at 7:16 Comment(1)
Did you find my answer useful anyhow?Ubangishari
U
5

Could you please try to create a gradle.properties file which should be located next to root build.gradle and will it with the following content:

org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=1024m 

Another option is to set the required options via compileJava task, please see here and here.

Unfortunately no idea how JAVA_TOOL_OPTIONS can be set, it seems unsupported.

Ubangishari answered 8/5, 2017 at 10:26 Comment(4)
So the solution is JAVA_TOOL_OPTIONS is not supported and needs to be set in every CI/CD environment and developer's machine manually?Garber
@user3105453, probaly yes.Ubangishari
I believe that org.gradle.jvmargs only affects the memory configuration of the Gradle daemon, not the JVM.Disaffirm
@Disaffirm yes you are correct. its does not affect the forked JVMConcubine
S
1

From what I gather the gradlew is intended to be tweaked. So adding those environment variables in the gradlew script would be acceptable and the setting would be available in all CI environments.

Shennashensi answered 4/1, 2018 at 0:32 Comment(0)
R
0

since this question is old but still relevant, I post what i found in the official docs:

Basically, the run scripts which are generated by gradlew distZip have an argument JAVA_OPTS set which can be set via using the gradle plugin 'application' and setting it via



plugins {
    id 'application'
}

...
application {
    applicationDefaultJvmArgs = ['-Dgreeting.language=en']
}

in this example i am setting a system property 'greeting.language' to 'en'.

In similar fashion you can do set JVM args like this:

application {
    applicationDefaultJvmArgs = ['-Xmx2048m', '-XX:MaxPermSize=1024m']
}

Found this in the official docs, for Gradle 6.9.1 and newer

Rag answered 8/11, 2023 at 13:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.