Backend Development 7 min read

Configuring and Optimizing Memory Settings in Spring Boot

This article explains Java memory concepts, demonstrates how to set JVM memory parameters for Spring Boot via startup scripts and configuration files, lists common memory flags, and provides practical tips for reducing memory consumption and improving application performance.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Configuring and Optimizing Memory Settings in Spring Boot

Spring Boot is one of the most popular Java application frameworks and is widely used in enterprise development; proper deployment and optimization are crucial for stable and efficient services.

1. What is Java Memory?

Java applications run on the Java Virtual Machine (JVM), which manages several memory areas such as the heap, stack, and non‑heap. The heap stores Java objects and is the primary source of OutOfMemoryError , making correct heap sizing essential during deployment and optimization.

2. Spring Boot Memory Settings

Spring Boot allows memory parameters to be configured in two main ways.

1. Set memory parameters in the startup script

For Linux, edit the *.sh script; for Windows, edit the *.bat file. Example scripts:

# Set maximum memory
export JAVA_OPTS="-Xmx1024m"
# Set minimum memory
export JAVA_OPTS="-Xms512m"
# Set off‑heap memory
export JAVA_OPTS="-XX:MaxDirectMemorySize=512m"
# Set maximum memory
set JAVA_OPTS="-Xmx1024m"
# Set minimum memory
set JAVA_OPTS="-Xms512m"
# Set off‑heap memory
set JAVA_OPTS="-XX:MaxDirectMemorySize=512m"

2. Set memory parameters in application.properties or application.yml

In application.properties :

# Set maximum memory
spring.datasource.tomcat.max-active=1024
# Set minimum memory
spring.datasource.tomcat.min-idle=512
# Set off‑heap memory
tomcat.tomcat.max-connections=512

In application.yml :

# Set maximum memory
spring:
  datasource:
    tomcat:
      max-active: 1024
# Set minimum memory
      min-idle: 512
# Set off‑heap memory
tomcat:
  max-connections: 512

3. Common Memory Parameters

-Xmx : maximum heap size (e.g., -Xmx1024m )

-Xms : initial heap size (e.g., -Xms512m )

-XX:MaxDirectMemorySize : off‑heap memory (e.g., -XX:MaxDirectMemorySize=512m )

-Xss : thread stack size (e.g., -Xss256k )

-XX:PermSize : permanent generation size (JDK 8 and earlier, e.g., -XX:PermSize=256m )

-XX:MaxPermSize : maximum permanent generation size (JDK 8 and earlier, e.g., -XX:MaxPermSize=512m )

-XX:MaxMetaspaceSize : metaspace size (JDK 8+ , e.g., -XX:MaxMetaspaceSize=256m )

4. How to Optimize Spring Boot Memory Usage

Reduce the number of fields and methods; avoid excessive static methods/variables.

Prefer primitive types over wrapper classes (e.g., int instead of Integer ).

Disable development mode in production to save memory.

Use caching mechanisms to lower memory consumption.

Employ connection pools to improve efficiency and reduce memory usage.

5. Conclusion

This article covered how to configure Spring Boot memory parameters, introduced common JVM flags, and offered practical tips for optimizing memory usage, which are essential for achieving stable and high‑performance Java applications.

JVMMemory Optimizationbackend developmentconfigurationSpring BootJava Memory
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.