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?
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.
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.
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.
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:
(5) Spring's recommandations
java -Xmx32m -Xss256k -jar target/demo-0.0.1-SNAPSHOT.jar
server.tomcat.max-threads: 4
(6) See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With