编程知识 cdmana.com

Small universe explosion! New feature of spring boot: saving 95% memory

author |  coldly

source | https://mp.weixin.qq.com/s/0m6ofmMlQTDUQwC7oRRIrQ

GraalVM[1] It's a high-performance virtual machine , It can significantly improve the performance and efficiency of the program , Great for microservices . Recently, it's more popular Java frame  Quarkus[2] The default support GraalVM

The following figure for Quarkus And the traditional framework (SpringBoot) Equal contrast chart , Faster startup data 、 Less memory consumption 、 Shorter service response .

Spring Boot 2.4 Begin to gradually offer to GraalVM Support for , It aims to enhance the above mentioned start-up 、 Memory 、 The use experience of response .

install GraalVM

  • The latest version of the official community is 20.3.0 , Is based on OpenJDK 8u272 and 11.0.9 custom , It can be understood as OpenJDK A derivative version of .

  • The official recommendation is  SDKMAN[3] For quick installation and switching between different versions JDK Tools for , Be similar to nodejs Of  nvm[4].

You can install the specified version and specify the default version by using similar commands

sdk install java 11.0.9.hs-adpt
sdk default java 11.0.9.hs-adpt

However, the installation process needs to download relevant resources from abroad , The user experience is not very good after trying , All suggest that you download the specified version GraalVM Can be installed ( and JDK Same installation method ).

  • Installation successful. View 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-b06mixed modesharing)

install native-image

native-image By Oracle Labs A kind of development AOT compiler , Apply what you need class Dependencies and runtime The library is packaged and compiled to generate a separate executable file . Have an efficient startup And the advantage of smaller runtime memory overhead .

but GraalVM It's not built-in, it just provides gu Installation tools , We need to install it separately .

-  Switch to  jdk  Installation directory 
⋊> ~ cd $JAVA_HOME/bin/

-  Use gu Command to install 
⋊>  ./gu install native-image

initialization Spring Boot 2.4 project

  • Spring Initializr establish demo project

curl https://start.spring.io/starter.zip -d dependencies=web \
           -d bootVersion=2.4.1 -o graal-demo.zip
  • Let's take a look at the startup benchmark data first , Just run empty projects need 1135 ms second

java -jar demo-0.0.1-SNAPSHOT.jar


engine[Apache Tomcat/9.0.41]
2020-12-18 11:48:36.856  INFO 91457 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-12-18 11:48:36.856  INFO 91457 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContextinitialization completed in 1135 ms
  • Memory footprint

ps aux | grep demo-0.0.1-SNAPSHOT.jar | grep -v grep | awk '{print $11 "\t" $6/1024"MB" }'
/usr/bin/java 480.965MB

Support GraalVM

  • Increase dependency , Many plug-ins are involved and have been uploaded completely  Gitee Gist[5]

<!--  What's new , Note the need to add   spring maven  The warehouse address can be downloaded to -->
<dependency>
    <groupId>org.springframework.experimental</groupId>
    <artifactId>spring-graalvm-native</artifactId>
    <version>0.8.3</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-indexer</artifactId>
</dependency>

<!-- Need to add  spring maven  Warehouse downloading  spring-graalvm-native-->
<repositories>
  <repository>
      <id>spring-milestones</id>
      <name>Spring Milestones</name>
      <url>https://repo.spring.io/milestone</url>
  </repository>
</repositories>
  • Main Methods to modify ,proxyBeanMethods = false

@SpringBootApplication(proxyBeanMethods = false)
  • Use native-image Build an executable file

 mvn -Pnative package
# The build process is slow , The log is as follows 
spring.factories files...
[com.example.demo.demoapplication:93430]    classlist:   4,633.58 ms,  1.18 GB
   _____                     _                             _   __           __     _
  / ___/    ____    _____   (_)   ____    ____ _          / | / /  ____ _  / /_   (_) _   __  ___
  \__ \    / __ \  / ___/  / /   / __ \  / __ `/         /  |/ /  / __ `/ __/  / / | | / / / _ \
 ___/ /   / /_/ / / /     / /   / / / / / /_/ /         / /|  /  / /_/ / / /_   / /  | |/ / /  __/
/____/   / .___/ /_/     /_/   /_/ /_/  \__, /         /_/ |_/   \__,_/  \__/  /_/   |___/  \___/
        /_/                            /____/

...
[com.example.demo.demoapplication:93430]      [total]: 202,974.38 ms,  4.23 GB
  • Compilation result

stay targe Directory generation The name is  com.example.demo.demoapplication  Executable file

  • Start the application   The compiled executable executed here is not jar

cd target

./com.example.demo.demoapplication
  • Starting time 0.215 seconds

2020-12-18 12:30:40.625  INFO 94578 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.215 seconds (JVM running for 0.267)
  • Look at the memory usage 24.8203MB

ps aux | grep com.example.demo.demoapplication | grep -v grep | awk '{print $11 "\t" $6/1024"MB" }'

./com.example.demo.demoapplication 24.8203MB

Data comparison

Whether to introduce GraalVM Memory footprint Starting time
no 480.965MB 1135 ms
yes 24.8203MB 215 ms

Reference material

[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

Friends who like this article , Welcome to the official account   Programmer Xiaohui , Watch more

 Order one [ Looking at ], It's the biggest support for Xiao Hui !

版权声明
本文为[osc_ jxamvmuz]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225132003236k.html

Scroll to Top