Save 95% memory usage and 80% startup time.


GraalVM[1] is a high-performance virtual machine that can significantly improve the performance and running efficiency of programs and is very suitable for microservices. The recently popular Java framework Quarkus[2] supports GraalVM by default

Below is a comparison of Quarkus and traditional frameworks (SpringBoot), with faster startup speed, lower memory consumption, and shorter service response.


Spring Boot 2.4 is beginning to progressively provide support for GraalVM, aiming to improve the Boot, memory, and response experience described above.

Install GraalVM

  • The latest official community release is 20.3.0, which is based on OpenJDK 8U272 and 11.0.9. It can be understood as a derivative of OpenJDK.

  • The official recommendation is SDKMAN[3] ‘s tool for quickly installing and switching between JDK versions, similar to NodeJS’ NVM [4].

You can use similar commands to install the specified version and specify the default version

The SDK install Java 11.0.9. Hs - adpt

Copy the code
The default Java SDK 11.0.9. Hs - adpt

Copy the code

However, during the installation process, you need to download relevant resources from foreign countries. The author has not had a very good experience after trying to use GraalVM. Therefore, it is recommended that you download the specified version of GraalVM and install it (the same way as JDK installation).

  • The installation is successful. View the version
⋊> ~ java-version 11:30:34

openjdk version "11.0.9" 2020-10-20

OpenJDK Runtime Environment GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06)

OpenJDK 64-Bit Server VM GraalVM CE 20.3.0 (build 11.0.9+10-jvmci-20.3-b06, mixed mode, sharing)

Copy the code

Install the native – image

Native Image is an AOT compiler developed by Oracle Labs. Class dependencies and runtime libraries required by applications are packaged and compiled to generate a separate executable file. Advantages of efficient startup and small runtime memory overhead.

However, GraalVM is not built-in but provides gu installation tools, which need to be installed separately.

- Switch to the JDK installation directory

⋊ > ~cd $JAVA_HOME/bin/



- Run the gu command to install

⋊ >. / gu install native - image

Copy the code

Initialize the Spring Boot 2.4 project

  • Spring Initializr creates the Demo project
curl https://start.spring.io/starter.zip -d dependencies=web \

- d bootVersion = against 2.4.1 - o graal - demo. Zip

Copy the code
  • Looking at the startup baseline, it takes 1135 ms seconds to run an empty project
Java jar demo - 0.0.1 - the SNAPSHOT. The jar





Engine: Apache Tomcat / 9.0.41

INFO 91457 -- [main] O.A.C.C.C. [Tomcat].[/] : Initializing Spring embedded WebApplicationContext

The 2020-12-18 11:48:36. 91457-856 the INFO [main] W.S.C.S ervletWebServerApplicationContext: Root WebApplicationContext: initialization completedin 1135 ms

Copy the code
  • Memory usage
Ps aux | grep demo - 0.0.1 - the SNAPSHOT. Jar | grep -v grep | awk'{print $11 "\t" $6/1024"MB" }'

The/usr/bin/Java 480.965 MB

Copy the code

Support GraalVM

  • Add related dependencies, involving many plug-ins complete upload Gitee Gist[5]
<! -- New section, note that you need to add the spring Maven repository address to download -->

<dependency>

    <groupId>org.springframework.experimental</groupId>

    <artifactId>spring-graalvm-native</artifactId>

The < version > 0.8.3 < / version >

</dependency>

<dependency>

    <groupId>org.springframework</groupId>

    <artifactId>spring-context-indexer</artifactId>

</dependency>



<! Spring-graalvm-native -->

<repositories>

  <repository>

      <id>spring-milestones</id>

      <name>Spring Milestones</name>

      <url>https://repo.spring.io/milestone</url>

  </repository>

</repositories>

Copy the code
  • Add proxyBeanMethods = false to Main
@SpringBootApplication(proxyBeanMethods = false)



Copy the code
  • Build executable files using native image
 mvn -Pnative package

Copy the code
The build process is slow

spring.factories files...

[com. Example. Demo. Demoapplication: 93430] classlist: 4633.58 ms, 1.18 GB

   _____                     _                             _   __           __     _

/ ___ / _____ _____ _____ _____ (_) _ / | / / ____ _ / _ _ _ __ ___

\ \ / (\ / ___ / / / / / / (` / / | / / / __ ` / / __ / / / | | / / / _ \

___ / / / / _ / / / / / / / / / / / / _ / / / / | / / / _ / / / / _ / / | | / / / __ /

____ / /. / ___ / __ / / __ / / _ / / _ \ __, / / _ / | _ / \ __, __ / / __ / / _ / | ___ / ___ /

There comes / __ / / /



.

[com. Example. Demo. Demoapplication: 93430] [total] : 202974.38, ms 4.23 GB

Copy the code
  • Compile the results

Directory to generate the targe name for com. Example. Demo. Demoapplication executable file

  • Start the application with the compiled executable executed here instead of the JAR
cd target



./com.example.demo.demoapplication

Copy the code
  • Start time 0.215 seconds
The 12:30:40 2020-12-18. 94578-625 the INFO [main] com. Example. Demo. DemoApplication: Started DemoApplicationin0.215 seconds (JVM is runningfor 0.267)



Copy the code
  • Take a look at the memory footprint of 24.8203MB
ps aux | grep com.example.demo.demoapplication | grep -v grep | awk '{print $11 "\t" $6/1024"MB" }'



. / com. Example. Demo. Demoapplication 24.8203 MB

Copy the code

Data contrast

Whether to introduce GraalVM Memory footprint The startup time
no 480.965 MB 1135 ms
is 24.8203 MB 215 ms

The resources

[1]

GraalVM: https://www.graalvm.org


[2]

Quarkus: https://quarkus.io


[3]

SDKMAN: https://sdkman.io/install


[4]

nvm: https://github.com/creationix/nvm


[5]

Gitee Gist: https://gitee.com/gi2/codes/famcqz6n21iylpg3us7j036