Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 766
J.R. Avatar asked Aug 03 '17 08:08

J.R.


People also ask

How do I give more memory to Gradle?

For faster builds, increase the maximum heap size for the Gradle daemon to more than 2048 MB. To do this set org. gradle. jvmargs=-Xmx2048M in the project gradle.

How increase JVM heap size permanently?

Edit the -Xmx256m option. This option sets the JVM heap size. Set the -Xmx256m option to a higher value, such as Xmx1024m. Save the new setting.


2 Answers

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.

like image 90
J.R. Avatar answered Nov 11 '22 16:11

J.R.


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

like image 37
ady Avatar answered Nov 11 '22 14:11

ady