[JAVA] I tried to reduce the capacity of Spring Boot

Self-introduction


What is Spring Boot?

--Mechanism / framework that makes it easy to create Spring-based applications --Build results are combined into a single jar file


Benefits of a single jar

--The AP server is included, so there is no need to prepare an AP server for each environment. --The fun of starting with java -jar

spring-boot.JPG


Disadvantages of a single jar

--Since all dependent libraries including AP server are included, the file size becomes large.

spring-boot.JPG


I tried to reduce the jar size


result

img1.JPG


result

img2.JPG


Size comparison

graph_2.JPG


How did you do it?


How do you do Hello World in Java?


1. 1. Write code

Hello.java


public class Hello {
    public static void main(String... args) {
        System.out.println("Hello World");
    }
}

2. Compile with javac

$ javac Hello.java

3. 3. Hello.class is generated

after-javac.jpg


4. Run in java

$ java Hello
Hello World

Did you understand?


The source code before compilation is smaller!

The API for hitting javac is standard!

java-compiler.jpg

https://docs.oracle.com/javase/jp/8/docs/api/javax/tools/JavaCompiler.html


yaruo.jpg


How the improved jar works

|-LightweightJarExecutor.class  :
|→ Code that extracts the source from its own jar, compiles and executes it
|-...
`-src/        :Spring Boot and a set of dependent library sources
  |-ch/       : logback
  |-com/      :jackson or com.like sun
  |-javax/    :javaee standard API
  |-META-INF/
  |-org/      : Spring, hibernate, JBoss, Tomcat, etc... 
  `-sample/   :Spring MVC Hello World

Behavior at startup

  1. Extract the source code from your own jar and copy it to a temporary directory
  2. Compile the source output to the temporary directory using JavaCompiler
  3. Load the Spring Boot main class (JarLauncher) from the output destination of the compilation result using ʻURLClassLoader`.
  4. Execute the main () method of JarLauncher

Just In Time Compile! (not) </ span>


If it is left as it is, it will be wasteful and cannot be reduced in weight.

lightweight-jar.jpg


Useless 1: Comment

lightweight-jar.jpg


Waste 2: Annotation

lightweight-jar.jpg


Waste 3: private

lightweight-jar.jpg


Waste 4: Blank + line feed

lightweight-jar.jpg


Wastefully sophisticated and lean code

lightweight-jar.jpg


Result Succeeded in reducing the capacity by 5MB! : grin :: thumbs up:

graph_2.JPG


Compensation 1: Slow startup: innocent :: thumbsdown:

lightweight-jar.jpg


Compensation 2: Unable to debug: joy :: thumbsdown:

lightweight-jar.jpg


Please do not imitate a good child

Recommended Posts

I tried to reduce the capacity of Spring Boot
05. I tried to stub the source of Spring Boot
I want to control the default error message of Spring Boot
The story of raising Spring Boot 1.5 series to 2.1 series
[Spring Boot] I investigated how to implement post-processing of the received request.
I tried to summarize the state transition of docker
I tried to clone a web application full of bugs with Spring Boot
I tried to explain the method
I tried GraphQL with Spring Boot
I tried Flyway with Spring Boot
The story of raising Spring Boot from 1.5 series to 2.1 series part2
About the function of Spring Boot due to different versions
I tried to summarize the basics of kotlin and java
I tried to verify this and that of Spring @ Transactional
[Swift] I tried to implement the function of the vending machine
I tried to summarize the basic grammar of Ruby briefly
I tried to build the environment of WSL2 + Docker + VSCode
I tried to get started with Swagger using Spring Boot
I tried to solve the problem of "multi-stage selection" with Ruby
I tried to summarize the methods used
I want to understand the flow of Spring processing request parameters
I tried to build the environment of PlantUML Server with Docker
I tried Spring.
What I did in the migration from Spring Boot 1.4 series to 2.0 series
I tried to implement the Iterator pattern
I tried Lazy Initialization with Spring Boot 2.2.0
I tried to check the operation of gRPC server with grpcurl
What I did in the migration from Spring Boot 1.5 series to 2.0 series
I tried to summarize the Stream API
I tried to summarize the methods of Java String and StringBuilder
I tried to solve the problem of Google Tech Dev Guide
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot
Procedure to make the value of the property file visible in Spring Boot
I tried to summarize the key points of gRPC design and development
I used Docker to solidify the template to be developed with spring boot.
I tried to make full use of the CPU core in Ruby
I tried to visualize the access of Lambda → Athena with AWS X-Ray
I introduced OpenAPI (Swagger) to Spring Boot (gradle) and tried various settings
I tried to measure and compare the speed of GraalVM with JMH
Summary of what I learned about Spring Boot
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
I tried to chew C # (basic of encapsulation)
I want to var_dump the contents of the intent
Let's check the feel of Spring Boot + Swagger 2.0
I tried to link JavaFX and Spring Framework.
I tried to set tomcat to run the Servlet.
I wanted to gradle spring boot with multi-project
[Spring Boot] How to refer to the property file
I tried using the profiler of IntelliJ IDEA
[I tried] Spring tutorial
I tried Spring Batch
What I tried when I wanted to get all the fields of a bean
03. I sent a request from Spring Boot to the zip code search API
I tried to compare the infrastructure technology of engineers these days with cooking.
How to set environment variables in the properties file of Spring boot application
[Beginner] I tried to decorate the bar after displaying the details of the hamburger menu
Specify the encoding of static resources in Spring Boot
I tried to implement file upload with Spring MVC
I tried to organize the cases used in programming
I tried using the Server Push function of Servlet 4.0