1. Overview
1.概述
In this tutorial, we’ll learn about the default memory settings that Spring Boot applications use.
在本教程中,我们将了解Spring Boot应用程序使用的默认内存设置。
Generally, Spring doesn’t have any memory-specific configuration, and it runs with the configurations of the underlying Java process. So below are the ways in which we can configure the memory of our Java applications.
一般来说,Spring没有任何针对内存的配置,它是以底层Java进程的配置运行的。因此,下面是我们可以配置Java应用程序的内存的方法。
2. Memory Settings
2.内存设置
The Java process or the JVM’s memory is divided among the heap, stack, meta-space, JIT code cache, and shared libraries.
Java进程或JVM的内存分为堆、栈、元空间、JIT代码缓冲区和共享库。
2.1. Heap
2.1.堆积
Heap is the part of the memory where the objects live until there are collected by the garbage collector.
堆是内存的一部分,对象在被垃圾收集器收集之前一直在那里。
The default value for the minimum heap is 8 Mb or 1/64th of the physical memory within the 8 Mb to 1 Gb range.
最小堆的默认值是8Mb或8Mb至1Gb范围内物理内存的1/64。
The default value for the maximum heap is 1/4th of the physical memory for physical memory greater than 192 MB, otherwise, it’s 1/2th of the physical memory.
最大堆的默认值是物理内存大于192MB时为物理内存的1/4,否则为物理内存的1/2。
Inside side the heap, we have the nursery size limit which when exceeded, causes the new generation garbage collection to run. Its default value is platform-specific.
在堆的内部,我们有苗圃大小的限制,当超过这个限制时,会导致新一代的垃圾收集运行。它的默认值是针对平台的。
We also have the keep area limit. It’s the percentage of the total heap size that when reached causes sufficiently long-living objects to be promoted from the young generation to the old generation. Its default value is 25%.
我们也有保持面积的限制。它是总堆大小的百分比,当达到这个百分比时,足够长寿的对象就会从年轻一代晋升到老一代。它的默认值是25%。。
Since Java 8, we also have the meta-space as part of the heap where all the class metadata is stored. By default, its minimum value is platform-dependent and the maximum value is unlimited.
从Java 8开始,我们还有元空间作为堆的一部分,所有的类元数据都存储在这里。默认情况下,其最小值与平台有关,最大值为无限。
For overriding the default values for minimum heap, maximum heap, and meta space size, please refer to this post regarding configuring heap size.
对于覆盖最小堆、最大堆和元空间大小的默认值,请参考此关于配置堆大小的帖子。
We can override the nursery size limit using the -Xns parameter. Since the nursery is part of the heap, its value should not be greater than the -Xmx value:
我们可以使用-Xns参数来覆盖托儿所的大小限制。由于托儿所是堆的一部分,它的值不应该大于-Xmx值。
java -Xns:10m MyApplication
We can also override the default values for the keep area limit using the –XXkeepAreaRatio parameter. For example, we can set it to 10 %:
我们还可以使用-XXkeepAreaRatio参数来覆盖保留区域限制的默认值。例如,我们可以将其设置为10%。
java -XXkeepAreaRatio:10 MyApplication
Finally, here is how we check the heap size on Linux:
最后,这里是我们如何在Linux上检查堆的大小。
java -XX:+PrintFlagsFinal -version | grep HeapSize
The same command for checking the heap size on Windows will be:
在Windows上检查堆大小的相同命令将是。
java -XX:+PrintFlagsFinal -version | findstr HeapSize
2.2. Stack
2.2 堆栈
It’s the amount of memory provided to each of the threads for execution. The default value for it is platform-specific.
它是提供给每个线程执行的内存量。它的默认值是针对平台的。
So, we can define the thread stack size using the -Xss parameter. For example, we can allocate it to 512 kB:
因此,我们可以使用-Xss参数来定义线程栈的大小。例如,我们可以将其分配为512 kB。
java -Xss:512k MyApplication
We can then check the thread stack size on Linux:
然后我们可以在Linux上检查线程栈的大小。
java -XX:+PrintFlagsFinal -version | grep ThreadStackSize
Or do the same on a Windows machine:
或者在Windows机器上做同样的事情。
java -XX:+PrintFlagsFinal -version | findstr ThreadStackSize
3. Conclusion
3.总结
In this article, we have learned about the default values of various heap and stack memory configuration options available for Java applications.
在这篇文章中,我们已经了解了Java应用程序可用的各种堆和栈内存配置选项的默认值。
So while launching our Spring Boot applications we may define these parameters as per our requirements.
因此,在启动我们的Spring Boot应用程序时,我们可以根据我们的要求来定义这些参数。
For further tuning options, we do refer to the official guide. Also for a list of all the configuration parameters, please refer to this document.
对于进一步的调整选项,我们确实参考了official guide.另外,关于所有配置参数的列表,请参考这个文档。