Developing spring boot application with lower footprint

Below are some personal ideas on how to reach a smaller footprint with Spring Boot. Your question is too broad for these recommandations to be taken into account in any other context. I’m not entirely sure you want to follow these in most situation, it simply answers “how to achieve a smaller footprint”.

(1) Only specify required dependencies

I wouldn’t personally worry about it, but if the goal is to have a smaller footprint, you may avoid using starter-* dependencies. Only specify the dependencies you actually use.

Avoid this:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

In my sample project, the artifact produced with starter-* dependencies is ~25MB

Do this:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
</dependency>

In my sample project, the artifact produced without starter-* dependencies is ~15MB

(2) Exclude AutoConfigurations

Exclude the AutoConfiguration you don’t need:

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

(3) Spring Boot properties

Disable as much as you can in the application.properties (while making sure it does not have a negative impact too):

spring.main.web-environment=false
spring.main.banner-mode=off
spring.jmx.enabled=false
server.error.whitelabel.enabled=false
server.jsp-servlet.registered=false
spring.freemarker.enabled=false
spring.groovy.template.enabled=false
spring.http.multipart.enabled=false
spring.mobile.sitepreference.enabled=false
spring.session.jdbc.initializer.enabled=false
spring.thymeleaf.cache=false
...

(4) Choose your embedded web container wisely

If launching spring boot with an embedded web container, you may choose a different one:

  • tomcat (by default)
  • undertow (https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-undertow)
  • jetty (https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-jetty)

(5) Spring’s recommandations

  • Memory: java -Xmx32m -Xss256k -jar target/demo-0.0.1-SNAPSHOT.jar
  • Number of threads: server.tomcat.max-threads: 4
  • source: spring-boot-memory-performance

(6) See also:

  • How to reduce Spring memory footprint

Leave a Comment