43

I am trying to deploy a Spring Boot powered web app to production. The app is built with Spring Boot 1.0.1 and has the default Tomcat 7 embedded as application server. I want to allocate larger memory to the app when start the app with java -jar myapp.jar command line.

Should I use JVM parameter such as -Xms -Xmx or use environment variable such as JAVA_OPTS? I have tried to look for the answer in documentation or google it, but I did not get an answer. Can anyone give some hints?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user2912360
  • 451
  • 1
  • 5
  • 4

5 Answers5

75

If starting the application with the spring-boot plugin:

mvn spring-boot:run -Drun.jvmArguments="-Xmx512m" -Drun.profiles=dev

Otherwise if running java -jar:

java -Xmx512m -Dspring.profiles.active=dev -jar app.jar
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nicomak
  • 2,319
  • 1
  • 21
  • 23
  • This doesn't seem to work for Spring Boot 2.0.0.M6. The [Spring Boot Maven plugin documentation](https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/run-debug.html) specifies that it should work, but it doesn't. I see two options, either specify the configuration into the pom.xml (that works), or specify the version of the Spring Boot Maven plugin to be 1.5.9-RELEASE (that works too). – Vlad Dinulescu Dec 23 '17 at 12:51
  • Like Vlad Dinulescu mentioned it works only with a set of Spring Boot versions. For one version it worked, for 2.0.3.RELEASE it doesn't. Solution in answer by Zhang Buzz works. – hipokito Sep 13 '18 at 19:55
45

Since this is specifically a Spring Boot question, I'd argue that a more useful answer than @DaveSyer's is this:

You can drop a .conf file in the same directory as your WAR file that is effectively a shell script.

For example,

$ ls
myapp.conf
myapp.war
$ cat myapp.conf
export JAVA_OPTS="-Xmx1024m -Xms256m"

Any configuration you do there will be run before the Spring Boot embedded Tomcat starts up. Personally, I version control a .conf.example file in my application itself and then drop a copy of it on each server I deploy to.

Of course, anything you set in that .conf file is overridable with command-line operations.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
inanutshellus
  • 9,683
  • 9
  • 53
  • 71
  • 5
    This is so beautiful. And it works for .jar files as well. I <3 Spring Boot – dustmachine Jun 10 '16 at 15:40
  • Since which version is available? – lrkwz Dec 07 '17 at 17:18
  • 1
    I tried with spring boot 1.5.9, but it didn't pickup my conf: export JAVA_OPTS="java.net.preferIPv4Stack=true" in .conf file named the same as jar file. Executed with java -jar using nohup. – Mahesh Dec 19 '17 at 14:43
26

Just use whatever normal mechanism you would to set up the JVM. Documentation is available on the command line:

$ java -X
...
-Xms<size>        Set initial Java heap size
-Xmx<size>        Set maximum Java heap size
...
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave Syer
  • 56,583
  • 10
  • 155
  • 143
18

For Spring Boot 2, you have to specify the heap size in the pom.xml file as below:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>-Xmx64m</jvmArguments>
    </configuration>
</plugin>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zhang Buzz
  • 10,420
  • 6
  • 38
  • 47
14

For Spring Boot 1, the Maven argument to specify in the plugin configuration is jvmArguments, and the user property is run.jvmArguments:

mvn spring-boot:run -Drun.jvmArguments="-Xms2048m -Xmx4096m"

For Spring Boot 2, the Maven argument to specify in the plugin configuration is also jvmArguments, but the user property is now spring-boot.run.jvmArguments:

mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Xms2048m -Xmx4096m"

So if you use the plugin configuration way, both for Spring Boot 1 and 2 you can do that:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>
            -Xms4048m
            -Xmx8096m
        </jvmArguments>
    </configuration>
</plugin>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
davidxxx
  • 125,838
  • 23
  • 214
  • 215