Skip to content

Refine Efficient Deployments section in the reference guide #40175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
[[deployment.efficient.unpacking]]
== Unpacking the Executable JAR

If you are running your application, you can use an executable jar, but it is also often an advantage to extract it and run it in a different way.
You can run your application using the executable jar, but loading the classes from nested jars has a small cost.
Depending on the size of the jar, running the application from an exploded structure is faster and recommended in production.
Certain PaaS implementations may also choose to extract archives before they run.
For example, Cloud Foundry operates this way.

With the help of the jarmode, you can extract the jar to different layouts.
The most efficient layout is a CDS (class data sharing) friendly layout.
This layout is used by default.
Spring Boot supports extracting your application to a directory using different layouts.
The default layout is the most efficient, and is xref:#deployment.efficient.cds[CDS friendly].

In this layout, the libraries are extracted to a `lib/` folder, and the application JAR
contains the application classes and a manifest which references the libraries in the `lib/` folder.
Expand All @@ -23,19 +23,45 @@ $ java -Djarmode=tools -jar my-app.jar extract
$ java -jar my-app/my-app.jar
----

This is actually faster on startup (depending on the size of the jar) than running from an unexploded archive.
After startup, you should not expect any differences.

TIP: Run `java -Djarmode=tools -jar my-app.jar help extract` to see all possible options.


[[deployment.efficient.cds]]
== Optimize Startup Time with CDS

Class Data Sharing (CDS) is a https://docs.oracle.com/en/java/javase/17/vm/class-data-sharing.html[JVM feature] that can help reduce the startup time and memory footprint of Java applications.

To use it, you should first perform a training run on your application in exploded form:

[source,shell]
----
$ 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
----

This creates an `application.jsa` file that can be reused as long as the application is not updated.

To use the cache, you need to add an extra parameter when starting the application:

[source,shell]
----
$ java -XX:SharedArchiveFile=application.jsa -jar my-app.jar
----

NOTE: For more details about CDS, refer to the {url-spring-framework-docs}/integration/cds.html[Spring Framework reference documentation]


[[deployment.efficient.aot]]
== Using Ahead-of-time Processing With the JVM

It's beneficial for the startup time to run your application using the AOT generated initialization code.
First, you need to ensure that the jar you are building includes AOT generated code.

NOTE: CDS and AOT can be combined to further improve startup time.

For Maven, this means that you should build with `-Pnative` to activate the `native` profile:

[source,shell]
Expand Down