Markus Weninger
Markus Weninger

Reputation: 12668

How to set maximum heap size (-Xmx) for Java application that gets started via "gradle run"?

If we want to clean, build and then run our application, we use ./gradlew clean run. run is part of the Gradle Application plugin.

What we want to achieve is to assign a certain maximum heap size to the application using run, something similar to ./gradlew clean run -Xmx1G. The result should equal running java -Xmx16G -jar /path/to/my/app.jar.

Is there a way to achieve this?


Edit: Just to make sure, I do not want to increase the build deamons's max heap but the max heap of the application that gets started by gradle.

Upvotes: 2

Views: 4999

Answers (1)

araknoid
araknoid

Reputation: 3125

As reported in the Installing Gradle documentation for version 4.2.1:

Note that it’s not currently possible to set JVM options for Gradle on the command line.

What you are looking for can be achieved modifying the build.gradle script. You can do it adding this little code snippet to your build.gradle file:

tasks.withType(JavaExec) {
    jvmArgs = ['-Xmx16g']
}

If you want to configure also the initial Java heap size, add the following code:

tasks.withType(JavaExec) {
    jvmArgs = ['-Xms1g', '-Xmx16g']
}

Upvotes: 7

Related Questions