Gradle: increase heap size for java process started by gradle run task

J.R. :

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.

J.R. :

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 can be used. Thanks @ady for the hint.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=443376&siteId=1