Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

In this tutorial, we’ll learn about the default memory settings that Spring Boot applications use. 

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.

2. Memory Settings

The Java process or the JVM’s memory is divided among the heap, stack, meta-space, JIT code cache, and shared libraries.

2.1. Heap

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.

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.

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%.

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.

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:

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 %:

java -XXkeepAreaRatio:10 MyApplication

Finally, here is how we check the heap size on Linux:

java -XX:+PrintFlagsFinal -version | grep HeapSize

The same command for checking the heap size on Windows will be:

java -XX:+PrintFlagsFinal -version | findstr HeapSize

2.2. Stack

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:

java -Xss:512k MyApplication

We can then check the thread stack size on Linux:

java -XX:+PrintFlagsFinal -version | grep ThreadStackSize

Or do the same on a Windows machine:

java -XX:+PrintFlagsFinal -version | findstr ThreadStackSize

3. Conclusion

In this article, we have learned about the default values of various heap and stack memory configuration options available for Java applications.

So while launching our Spring Boot applications we may define these parameters as per our requirements.

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.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.