How to configure heap size when starting a Spring Boot application with embedded tomcat?

。_饼干妹妹 提交于 2019-11-28 06:18:43

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

$ java -X
...
-Xms<size>        set initial Java heap size
-Xmx<size>        set maximum Java heap size
...
Nicomak

If starting the app 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

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 that is effectively a shell script.

e.g.

$ 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 app itself, then drop a copy of it on each server I deploy to.

Of course anything you set in that .conf file is overrideable with command line operations.

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

        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <jvmArguments>-Xmx64m</jvmArguments>
            </configuration>
        </plugin>

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!