Skip to content

Performance

Some tips to improve the performance, especially the startup time, of Spring Boot applications

Lazy Initialization

If you set the property spring.main.lazy-initialization=true, the initialization of the beans will be delayed until they are needed the first time. That can speed up the startup time, but beware, that the first access of the bean will take a bit longer.

properties
spring.main.lazy-initialization=true

Profile-Specific Configuration

If you separate the configuration into different environments, you can avoid unnecessary loading of beans.

Component Scanning

Limiting the scope of the class path scanner is limiting the classes to scan for. Use @ComponentScan to only include essential packages.

Reduce Bean Creation

Try to avoid the creation of beans on startup. Especially if the creation is very time-intensive.

Reduce Autoconfiguration

You can exclude the autoconfiguration of features, you do not need. That will reduce the creation of unneeded beans.

java
@SpringBootApplication(exclude = {
    DataSourceAutoConfiguration.class
})
public class MyApplication {
  public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
  }
}

Use spring-boot-devtools for development

This tool provides a live reload feature to restart your application much faster. It splits the class path into files, which won't change often (libraries) and files that change (your code). On a restart, only the files considered as 'change frequently' will be reloaded

xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

Use spring-context-indexer to improve bean scanning

This creates an index file on compile time to find beans more efficiently, reducing the time to scan the class path. Bean scanning can take a lot of time, especially in large projects.

xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-context-indexer</artifactId>
    <optional>true</optional>
</dependency>

WARNING

With Spring 6 the indexer will be deprecated in favor oft AOT Spring AOT

Use mvnd

You can speed up your maven build by using the maven demon mvnd.

Analyze startup time

Spring actuator provides an endpoint to give detailed insight in the startup process to identify bottlenecks. You can get a list of initialized beans and the time it takes for each bean to identify performance problems.

xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
http://localhost:8080/actuator/startup

Switch to virtual threads (JDK 21)

properties
server.tomcat.threads.max=4
server.tomcat.use-virtual-threads=true

Tune JVM

There are many possibilities to tune the JVM.

For example, you can change the garbage collection.

java -XX:+UseZGC

or limit the heap space

java -XX:MaxRAMPercentage=75
Further information

Link: Two phase commit

Contact: M_Bergmann AT gmx.at