Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Developing spring boot application with lower footprint

Tags:

spring-boot

In an attempt to keep our microservices, developed in spring-boot to run on Cloud Foundry, smaller in footprint, we are looking for best approach to achieve the same.

Any inputs or pointers in this direction will be most welcomed.

It's surely the best that one always builds the application upwards starting from bare minimum dependencies, and the add any more only when required. Is there more of good practices to follow to further keep the application smaller in size?

like image 836
Ashwin Gupta Avatar asked Aug 31 '16 06:08

Ashwin Gupta


People also ask

How do I reduce spring boot memory usage?

You can get a simple Spring Boot app down to around 72M total by using the following JVM options. With -XX:MaxRAM=72m This will restrict the JVM's calculations for the heap and non heap managed memory to be within the limits of this value.

How much memory do I need for spring boot?

The bare minimum you'll get away with is around 72M total memory on the simplest of Spring Boot applications with a single controller and embedded Tomcat. Throw in Spring Data REST, Spring Security and a few JPA entities and you'll be looking at 200M-300M minimum.

Why spring boot is not suitable for large scale projects?

Spring Boot creates a lot of unused dependencies, resulting in a large deployment file; The complex and time-consuming process of converting a legacy or an existing Spring project to a Spring Boot application; Not suitable for large-scale projects.


1 Answers

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
like image 166
alexbt Avatar answered Oct 08 '22 08:10

alexbt