Configure the Heap Size When Starting a Spring Boot Application – 启动Spring Boot应用程序时配置堆的大小

最后修改: 2021年 2月 15日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll learn how to configure the heap size when we start a Spring Boot application. We’ll be configuring the -Xms and -Xmx settings, which correspond to starting and maximum heap size.

在本教程中,我们将学习如何在启动 Spring Boot 应用程序时配置 堆大小。我们将配置-Xms-Xmx设置,它们对应于起始和最大堆大小。

Then, we’ll use Maven first to configure the heap size when starting the application using mvn on the command-line. We’ll also look at how we can set those values using the Maven plugin. Next, we’ll package our application into a jar file and run it with JVM parameters provided to the java -jar command.

然后,我们将首先使用Maven,在命令行上使用mvn启动应用程序时配置堆的大小。我们还将看看如何使用Maven插件设置这些值。接下来,我们将把应用程序打包成jar文件,并使用java -jar命令提供的JVM参数来运行它。

Finally, we’ll create a .conf file that sets JAVA_OPTS and run our application as a service using the Linux System V Init technique.

最后,我们将创建一个.conf文件,设置JAVA_OPTS使用Linux System V Init技术将我们的应用程序作为服务运行

2. Running from Maven

2.从Maven运行

2.1. Passing JVM Parameters

2.1.传递JVM参数

Let’s start by creating a simple REST controller that returns some basic memory information that we can use for verifying our settings:

让我们先创建一个简单的REST控制器,返回一些基本的内存信息,我们可以用它来验证我们的设置。

@GetMapping("memory-status")
public MemoryStats getMemoryStatistics() {
    MemoryStats stats = new MemoryStats();
    stats.setHeapSize(Runtime.getRuntime().totalMemory());
    stats.setHeapMaxSize(Runtime.getRuntime().maxMemory());
    stats.setHeapFreeSize(Runtime.getRuntime().freeMemory());
    return stats;
}

Let’s run it as-is using mvn spring-boot:run to get a baseline. Once our application starts, we can use curl to call our REST controller:

让我们使用mvn spring-boot:run按原样运行它,以获得一个基线。一旦我们的应用程序启动,我们可以使用curl来调用我们的REST控制器。

curl http://localhost:8080/memory-status

Our results will vary depending on our machine, but will look something like this:

我们的结果将因我们的机器而异,但看起来会是这样的。

{"heapSize":333447168,"heapMaxSize":5316280320,"heapFreeSize":271148080}

For Spring Boot 2.x, we can pass arguments to our application using -Dspring-boot.run.

对于Spring Boot 2.x,我们可以使用-Dspring-boot.run向我们的应用程序传递参数>。

Let’s pass starting and maximum heap size to our application with -Dspring-boot.run.jvmArguments:

让我们用-Dspring-boot.run.jvmArguments向我们的应用程序传递起始和最大堆大小。

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

Now, when we hit our endpoint, we should see our specified heap settings:

现在,当我们击中我们的端点时,我们应该看到我们指定的堆设置。

{"heapSize":2147483648,"heapMaxSize":4294967296,"heapFreeSize":2042379008}

2.2. Using the Maven Plugin

2.2.使用Maven插件

We can avoid having to provide parameters each time we run our application by configuring the spring-boot-maven-plugin in our pom.xml file:

我们可以通过在pom.xml文件中配置spring-boot-maven-plugin,避免每次运行应用程序时都要提供参数。

Let’s configure the plugin to set our desired heap sizes:

让我们来配置这个插件,以设置我们想要的堆大小。

<plugins>
    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <executions>
            <execution>
                <configuration>            
                    <mainClass>com.baeldung.heap.HeapSizeDemoApplication</mainClass>
                </configuration>
            </execution>
        </executions>
        <configuration>
            <executable>true</executable>
            <jvmArguments>
                -Xms256m
                -Xmx1g
            </jvmArguments>
        </configuration>
    </plugin>
</plugins>

Now, we can run our application using just mvn spring-boot:run and see our specified JVM arguments in use when we ping our endpoint:

现在,我们可以使用mvn spring-boot:run来运行我们的应用程序,并在ping我们的端点时看到我们指定的JVM参数在使用。

{"heapSize":259588096,"heapMaxSize":1037959168,"heapFreeSize":226205152}

Any JVM arguments we configure in our plugin will take precedence over any supplied when running from Maven using -Dspring-boot.run.jvmArguments.

我们在插件中配置的任何JVM参数将优先于使用-Dspring-boot.run.jvmArguments从Maven运行时提供的任何参数。

3. Running with java -jar

3.用java -jar运行

If we’re running our application from a jar file, we can provide JVM arguments to the java command.

如果我们从一个jar文件中运行我们的应用程序,我们可以向java命令提供JVM参数。

First, we must specify the packaging as jar in our Maven file:

首先,我们必须在Maven文件中指定打包为jar

<packaging>jar</packaging>

Then, we can package our application into a jar file:

然后,我们可以将我们的应用程序打包成一个jar文件。

mvn clean package

Now that we have our jar file, we can run it with java -jar and override the heap configuration:

现在我们有了jar文件,我们可以用java -jar运行它并覆盖堆配置。

java -Xms512m -Xmx1024m -jar target/spring-boot-runtime-2.jar

Let’s curl our endpoint to check the memory values:

让我们curl我们的端点来检查内存值。

{"heapSize":536870912,"heapMaxSize":1073741824,"heapFreeSize":491597032}

4. Using a .conf File

4.使用.conf文件

Finally, we’ll learn how to use a .conf file to set our heap size on an application run as a Linux service.

最后,我们将学习如何使用.conf文件,在作为Linux服务运行的应用程序上设置我们的堆大小。

Let’s start by creating a file with the same name as our application jar file and the .conf extension: spring-boot-runtime-2.conf.

让我们首先创建一个与我们的应用程序jar文件同名的文件,并以.conf为扩展名。spring-boot-runtime-2.conf

We can place this in a folder under resources for now and add our heap configuration to JAVA_OPTS:

我们现在可以把它放在资源下的一个文件夹里,并把我们的堆配置添加到JAVA_OPTS

JAVA_OPTS="-Xms512m -Xmx1024m"

Next, we’re going to modify our Maven build to copy the spring-boot-runtime-2.conf file into our target folder next to our jar file:

接下来,我们要修改Maven的构建,将spring-boot-runtime-2.conf文件复制到target文件夹,放在jar文件旁边。

<build>
    <finalName>${project.artifactId}</finalName>
    <resources>
        <resource>
            <directory>src/main/resources/heap</directory>
            <targetPath>${project.build.directory}</targetPath>
            <filtering>true</filtering>
            <includes>
                <include>${project.name}.conf</include>
            </includes>
        </resource>
    </resources>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <configuration>
                        <mainClass>com.baeldung.heap.HeapSizeDemoApplication</mainClass>
                    </configuration>
                </execution>
            </executions>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

We also need to set executable to true to run our application as a service.

我们还需要将executable设置为true,以便将我们的应用程序作为一项服务运行。

We can package our jar file and copy our .conf file over using Maven:

我们可以将jar文件打包,并使用Maven将.conf文件复制过来。

mvn clean package spring-boot:repackage

Let’s create our init.d service:

让我们创建我们的init.d服务。

sudo ln -s /path/to/spring-boot-runtime-2.jar /etc/init.d/spring-boot-runtime-2

Now, let’s start our application:

现在,让我们开始我们的应用程序。

sudo /etc/init.d/spring-boot-runtime-2 start

Then, when we hit our endpoint, we should see that our JAVA_OPT values specified in the .conf file are respected:

然后,当我们击中我们的端点时,我们应该看到我们在.conf文件中指定的JAVA_OPT值被尊重。

{"heapSize":538968064,"heapMaxSize":1073741824,"heapFreeSize":445879544}

5. Conclusion

5.总结

In this short tutorial, we examined how to override the Java heap settings for three common ways of running Spring Boot applications. We started with Maven, both modifying the values at the command line and also by setting them in the Spring Boot Maven plugin.

在这个简短的教程中,我们研究了如何为运行Spring Boot应用程序的三种常见方式覆盖Java堆设置。我们从Maven开始,既在命令行中修改数值,也在Spring Boot Maven插件中设置。

Next, we ran our application jar file using java -jar and passing in JVM arguments.

接下来,我们使用java -jar和传入JVM参数来运行我们的应用程序jar文件。

Finally, we looked at one possible production level solution by setting a .conf file alongside our fat jar and creating a System V init service for running our application.

最后,我们看了一个可能的生产级解决方案,即在我们的胖子jar旁边设置一个.conf文件,并创建一个System V init服务来运行我们的应用程序。

There are other solutions for creating services and daemons out of a Spring Boot fat jar, and many provide specific ways of overriding JVM arguments.

还有其他解决方案,用于从Spring Boot胖子jar中创建服务和守护程序,而且许多解决方案提供了覆盖JVM参数的具体方法。

As always, the example code is available over on GitHub.

像往常一样,示例代码可在GitHub上获得