19

I have a gradle task which starts a java project. Basically like this:

gradle run -PmainClass=package.path.ServiceMain

Now, I want to increase the heap for the java process started by gradle because the standard heap size is too small. The problem is that I succeed only to increase the size of the gradle process, not the size of the java process which is launched by the gradle project. I check the heap size with this command in my Java code:

Runtime.getRuntime().totalMemory()

I tested this check and it is valid to use it like this. But it shows me that gradle starts my Java process always with the same heap size.

I experimented with these options:

DEFAULT_JVM_OPTS='-Xmx1024m -Xms512m'
GRADLE_OPTS='-Xmx1024m -Xms512m'
JAVA_OPTS='-Xmx1024m -Xms512m'

No success.

I also tried this:

gradle run -PmainClass=package.path.ServiceMain -DXmx1024m -DXms512m

Still, no success.

Of course, I already searched the web but I found only some hints saying that I could modify the build.gradle file. Unfortunately, this is not what I want/can.

I need to specify the java heap size for my java program on the command line when starting it by a gradle run task (due to the actual project structure).

Thanks in advance for support. Any help is appreciated.

trincot
  • 317,000
  • 35
  • 244
  • 286
J.R.
  • 576
  • 1
  • 4
  • 15
  • 3
    It's not possible without modifying the `build.gradle` script. – Opal Aug 03 '17 at 10:14
  • 1
    If you relax your requirement on `build.gradle` you can find nice solutions here: https://stackoverflow.com/questions/23689054/problems-passing-system-properties-and-parameters-when-running-java-class-via-gr – charlie_pl Aug 03 '17 at 11:34
  • Thanks for the hint charlie_pl, but from what I understand the post is about program arguments, not JVM arguments. Or do I miss something? – J.R. Aug 03 '17 at 11:50

2 Answers2

19

As @Opal states above it is not possible.

The easiest/simplest alternative I could find (for now) is to add this little snippet to the build.gradle file:

tasks.withType(JavaExec) {
    jvmArgs = ['-Xms512m', '-Xmx512m']
}

Alternatively, the environment variable _JAVA_OPTIONS the can be used. Even better: the environment variable JAVA_TOOL_OPTIONS; the content of this variable will be used as (additional) JVM options.

Thanks @ady for the hints.

J.R.
  • 576
  • 1
  • 4
  • 15
  • For this particular question, the env variables are best. But regarding editing the build.gradle, while tasks.withType.. and not simply `run { jvmArgs = ...}`? Reference: https://blog.jakubholy.net/2020/customizing-gradle-run-task/ – Jakub Holý Nov 11 '20 at 14:13
  • 1
    @JakubHolý: Indeed, this might be also a possibility. But then it is limited to the run task and it needs to be copied to the test task (if it's needed there), right? – J.R. Nov 12 '20 at 15:09
7

Use this command in Linux: export _JAVA_OPTIONS="-Xms4000m -Xmx8000m" where values 4000 and 8000 can be changed. Instead of JAVA_OPTS use _JAVA_OPTIONS


Old answer: You can set or increase memory usage limits (or other JVM arguments) used for Gradle builds and the Gradle Daemon by editing $GRADLE_USER_HOME/.gradle/gradle.properties (~/.gradle/gradle.properties by default), and setting org.gradle.jvmargs:

org.gradle.jvmargs=-Xmx2024m -XX:MaxPermSize=512m

Source: https://riptutorial.com/gradle/example/11911/tuning-jvm-memory-usage-parameters-for-gradle

ady
  • 1,108
  • 13
  • 19
  • This seem to relate to the gradle process. What I need to modify is the JVM params of the process started by the gradle process. – J.R. Jan 23 '20 at 07:12
  • 3
    I see, then you can use this command in linux: `export _JAVA_OPTIONS="-Xms4000m -Xmx8000m"` where values 4000 and 8000 can be changed. Instead of `JAVA_OPTS` use `_JAVA_OPTIONS`. – ady Jan 24 '20 at 12:39
  • But in this case I must tell the gradle task (somehow in the build.gradle?) that it should evaluate this parameter and apply it when starting the Java process for the application, right? Ir is the `_JAVA_OPTIONS` param evaluated always automatically? – J.R. Jan 27 '20 at 07:01
  • 1
    The _JAVA_OPTIONS param is evaluated automatically. – ady Jan 28 '20 at 16:40
  • 1
    WOW! "export _JAVA_OPTIONS" was exactly what I needed to pass a -javaagent=/mypath/myjavaagent.jar argument to the JVM when running gradlew. Thank you for this answer @ady! If I just set "export JAVA_OPTIONS" then the values are passed only to Gradle and are NOT passed on to the JVM. – Lucy Mar 30 '20 at 15:17
  • 2
    Credit to @ady again: I see that JAVA_TOOL_OPTIONS is also available and documented: https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/envvars002.html – Lucy Apr 02 '20 at 14:38
  • Using _JAVA_OPTIONS over JAVA_OPS to set these flags is what I have been looking for. This was surprisingly hard to find. – KC54 Mar 16 '22 at 22:59