自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Spring Boot 3.3.0 新特性| 使用 CDS 優(yōu)化啟動(dòng)時(shí)間

開發(fā) 前端
CDS、CRaC 和 GraalVM,這三種技術(shù)都有助于提高Java程序的啟動(dòng)速度,但它們的應(yīng)用場(chǎng)景和優(yōu)化方式有所不同。CDS 通過共享類數(shù)據(jù)來加速啟動(dòng),CRaC 通過運(yùn)行時(shí)優(yōu)化來提升性能,而 GraalVM 則通過 AOT 編譯來實(shí)現(xiàn)快速啟動(dòng)和高效運(yùn)行。

一、CDS 是什么?

類數(shù)據(jù)共享 (CDS) 是一項(xiàng) JVM 功能,可幫助減少 Java 應(yīng)用程序的啟動(dòng)時(shí)間和內(nèi)存占用。從 JDK 12 開始,默認(rèn)的 CDS 歸檔文件與 Oracle JDK 二進(jìn)制文件一起預(yù)打包。筆者測(cè)試使用的 OpenJDK 64-Bit Server VM Zulu21.34+19-CA (build 21.0.3+9-LTS, mixed mode, sharing)它也是支持 CDS 的。

二、如何使用

2.1 訓(xùn)練

要使用它,您應(yīng)該首先以分解形式對(duì)應(yīng)用程序執(zhí)行訓(xùn)練運(yùn)行:

$ java -Djarmode=tools -jar my-app.jar extract --destination application

$ cd application

$ java -XX:ArchiveClassesAtExit=application.jsa -Dspring.context.exit=onRefresh -jar my-app.jar

這將創(chuàng)建一個(gè)  application 目錄并生成 application.jsa,只要應(yīng)用程序未更新,就可以重復(fù)使用。

2.2 使用

要使用緩存,您需要在啟動(dòng)應(yīng)用程序時(shí)添加一個(gè)額外的 -XX:SharedArchiveFile 參數(shù):

$ java -XX:SharedArchiveFile=application.jsa -jar my-app.jar

三、效果

為了測(cè)試 CDS,筆者使用 Spring Boot initializr 生成了一個(gè) demo 項(xiàng)目。下面是 CDS 訓(xùn)練后的 application 目錄的結(jié)構(gòu):

$ tree application
application
|-- application.jsa
|-- demo-0.0.1-SNAPSHOT.jar
`-- lib
    |-- jackson-annotations-2.17.1.jar
    |-- jackson-core-2.17.1.jar
    |-- jackson-databind-2.17.1.jar
    |-- jackson-datatype-jdk8-2.17.1.jar
    |-- jackson-datatype-jsr310-2.17.1.jar
    |-- jackson-module-parameter-names-2.17.1.jar
    |-- jakarta.annotation-api-2.1.1.jar
    |-- jul-to-slf4j-2.0.13.jar
    |-- log4j-api-2.23.1.jar
    |-- log4j-to-slf4j-2.23.1.jar
    |-- logback-classic-1.5.6.jar
    |-- logback-core-1.5.6.jar
    |-- micrometer-commons-1.13.0.jar
    |-- micrometer-observation-1.13.0.jar
    |-- slf4j-api-2.0.13.jar
    |-- snakeyaml-2.2.jar
    |-- spring-aop-6.1.8.jar
    |-- spring-beans-6.1.8.jar
    |-- spring-boot-3.3.0.jar
    |-- spring-boot-autoconfigure-3.3.0.jar
    |-- spring-boot-jarmode-tools-3.3.0.jar
    |-- spring-context-6.1.8.jar
    |-- spring-core-6.1.8.jar
    |-- spring-expression-6.1.8.jar
    |-- spring-jcl-6.1.8.jar
    |-- spring-web-6.1.8.jar
    |-- spring-webmvc-6.1.8.jar
    |-- tomcat-embed-core-10.1.24.jar
    |-- tomcat-embed-el-10.1.24.jar
    `-- tomcat-embed-websocket-10.1.24.jar

1 directory, 32 files

3.1 直接運(yùn)行 demo-0.0.1-SNAPSHOT.jar

$ java -jar demo-0.0.1-SNAPSHOT.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.0)

2024-05-31T08:53:39.964+08:00  INFO 14832 --- [demo] [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 21.0.3 with PID 14832 (D:\test\demo\target\demo-0.0.1-SNAPSHOT.jar started by L.cm in D:\test\demo\target)
2024-05-31T08:53:39.967+08:00  INFO 14832 --- [demo] [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2024-05-31T08:53:40.893+08:00  INFO 14832 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8888 (http)
2024-05-31T08:53:40.908+08:00  INFO 14832 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-05-31T08:53:40.908+08:00  INFO 14832 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-05-31T08:53:40.948+08:00  INFO 14832 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-05-31T08:53:40.949+08:00  INFO 14832 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 921 ms
2024-05-31T08:53:41.257+08:00  INFO 14832 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8888 (http) with context path '/'
2024-05-31T08:53:41.274+08:00  INFO 14832 --- [demo] [           main] com.example.demo.DemoApplication         : Started DemoApplication in 1.702 seconds (process running for 2.143)

我們可以在日志 Started DemoApplication in 1.702 seconds 看到啟動(dòng)耗時(shí)為 1.702 秒。

3.2 使用 CDS 運(yùn)行

需要先 cd 到訓(xùn)練的 application 目錄。

$ java -XX:SharedArchiveFile=application.jsa -jar demo-0.0.1-SNAPSHOT.jar
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.3.0)

2024-05-31T08:53:15.828+08:00  INFO 21444 --- [demo] [           main] com.example.demo.DemoApplication         : Starting DemoApplication v0.0.1-SNAPSHOT using Java 21.0.3 with PID 21444 (D:\test\demo\target\application\demo-0.0.1-SNAPSHOT.jar started by L.cm in D:\test\demo\target\application)
2024-05-31T08:53:15.830+08:00  INFO 21444 --- [demo] [           main] com.example.demo.DemoApplication         : No active profile set, falling back to 1 default profile: "default"
2024-05-31T08:53:16.244+08:00  INFO 21444 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8888 (http)
2024-05-31T08:53:16.249+08:00  INFO 21444 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-05-31T08:53:16.249+08:00  INFO 21444 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.24]
2024-05-31T08:53:16.272+08:00  INFO 21444 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-05-31T08:53:16.273+08:00  INFO 21444 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 419 ms
2024-05-31T08:53:16.425+08:00  INFO 21444 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8888 (http) with context path '/'
2024-05-31T08:53:16.431+08:00  INFO 21444 --- [demo] [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.722 seconds (process running for 0.885)

我們可以在日志 Started DemoApplication in 0.722 seconds 看到啟動(dòng)耗時(shí)比直接啟動(dòng)少了將近 1 秒。效果還是非常明顯的。

四、總結(jié)

CDS、CRaC 和 GraalVM,這三種技術(shù)都有助于提高Java程序的啟動(dòng)速度,但它們的應(yīng)用場(chǎng)景和優(yōu)化方式有所不同。CDS 通過共享類數(shù)據(jù)來加速啟動(dòng),CRaC 通過運(yùn)行時(shí)優(yōu)化來提升性能,而 GraalVM 則通過 AOT 編譯來實(shí)現(xiàn)快速啟動(dòng)和高效運(yùn)行。作為開發(fā)者,我們可以根據(jù)具體需求選擇合適的技術(shù)來優(yōu)化 Java 程序的啟動(dòng)時(shí)間。

責(zé)任編輯:武曉燕 來源: JAVA架構(gòu)日記
相關(guān)推薦

2024-02-26 00:00:00

GolangRedisDocker

2013-03-04 10:20:23

JVM優(yōu)化eclipseJVM

2009-02-18 16:34:32

優(yōu)化Windows啟動(dòng)時(shí)間

2020-02-12 08:50:05

Linux命令啟動(dòng)時(shí)間

2013-03-04 10:59:47

eclipseJVM

2018-06-06 14:30:38

Spring BootApplication事件

2022-11-08 07:46:28

record類聲明代碼

2022-05-04 17:50:51

Linux

2022-10-26 07:14:25

Spring 6Spring業(yè)務(wù)

2025-04-16 10:03:40

開發(fā)Spring應(yīng)用程序

2018-05-30 15:10:24

Spring BootList類型

2009-06-24 09:22:04

Spring2.5新特

2023-09-27 08:14:56

2024-10-11 11:32:22

Spring6RSocket服務(wù)

2009-06-15 16:15:37

Spring2.0新特

2009-06-18 15:40:07

Spring Batc

2023-06-02 16:24:46

SpringBootSSM

2024-12-16 08:10:00

Spring開發(fā)

2025-04-27 04:00:00

錯(cuò)誤頁Spring底層

2011-08-08 18:39:42

Windows7啟動(dòng)時(shí)間
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)