Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run application via gradlew with -Xmx and -Xms

I have an application. I run it via

gradlew run-app

Or debug

gradlew debug-app

It works. How do I pass '-Xmx' argument into the application which I run (debug)? Is it possible to do so without edditing build.gradle file?

I found this Gradle unknown command-line option '-X'

I get a similar error when I try

gradlew debug-app -Xmx2000m

Error

FAILURE: Build failed with an exception.

* What went wrong:
Problem configuring task :debug-app from command line.
> Unknown command-line option '-X'.

I tried to create a file gradle.properties in GRADLE_USER_HOME directory (by default, it is USER_HOME/.gradle).

org.gradle.jvmargs=-XX\:MaxHeapSize\=4256m -Xmx4256m -Xms2000m

I also tried to org.gradle.jvmargs=-Xmx2000m in project folder gradle.properties.

And even then when I run an application, I see Commited Memory size is < 520 MiB

enter image description here

And this is when I run it as a normal Java App

enter image description here

In the second case, when I run the application as a normal Java app with -Xms, -Xmx, Commited Memory size is about 3.5 GiB because I passed -Xmx4512m -Xms2512m parameters.

like image 633
Yan Khonski Avatar asked Jun 22 '17 14:06

Yan Khonski


People also ask

How do I run a gradlew file?

To run a Gradle command, open a command window on the project folder and enter the Gradle command. Gradle commands look like this: On Windows: gradlew <task1> <task2> … ​ e.g. gradlew clean allTests.

What is gradlew command?

gradlew is a wrapper(w - character) that uses gradle . Under the hood gradlew performs three main things: Download and install the correct gradle version. Parse the arguments. Call a gradle task.

What does gradlew assembleDebug do?

For instance, to build a debug version of your Android application, you can run ./gradlew assembleDebug from the root of your repository. In a default project setup, the resulting apk can then be found in app/build/outputs/apk/app-debug.


3 Answers

Add this in your gradle.properties file :

org.gradle.jvmargs=-Xmx2000m 

From here

org.gradle.jvmargs

Specifies the jvmargs used for the daemon process. The setting is particularly useful for tweaking memory settings. At the moment the default settings are pretty generous with regards to memory.

edit : my answer what about the gradle daemon jvm, not the app jvm. You have to use the jvmArgs property

The extra arguments to use to launch the JVM for the process. Does not include system properties and the minimum/maximum heap size.

like image 149
ToYonos Avatar answered Sep 18 '22 10:09

ToYonos


Firstly, thanks @ToYonos for leading me to the right direction.

Secondly, I found the solution here https://stackoverflow.com/a/9648945/4587961. I ran my app from command line.

set GRADLE_OPTS=-Xms1724m -Xmx5048m gradlew debug-app 

Note, CMD Windows command SET works locally, so if you close your terminal, GRADLE_OPTS will not be set. For Linux, you can use

export GRADLE_OPTS=WHATEVER 

This is what I wanted to achieve.

enter image description here

like image 36
Yan Khonski Avatar answered Sep 20 '22 10:09

Yan Khonski


Using application plugin one can use applicationDefaultJvmArgs property

apply plugin: 'application'    
applicationDefaultJvmArgs = ["-Xms1024m", "-Xmx2048m"]

The arguments will be applied to run task and to start script of your application

more info

like image 23
MKK Avatar answered Sep 21 '22 10:09

MKK