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 extracted form:
In Java 24, CDS is succeeded by the AOT Cache via https://openjdk.org/jeps/483[JEP 483].
Spring Boot supports both CDS and AOT cache, and it is recommended that you use the latter if it is available in the JVM version you are using (Java 24+).
[[packaging.class-data-sharing.cds]]
== CDS
To use CDS, you should first perform a training run on your application in extracted form:
[source,shell]
----
@ -12,9 +18,9 @@ $ cd application
@@ -12,9 +18,9 @@ $ cd application
NOTE: For more details about CDS, refer to the xref:how-to:class-data-sharing.adoc[CDS how-to guide] and the {url-spring-framework-docs}/integration/cds.html[Spring Framework reference documentation].
[[packaging.class-data-sharing.aot-cache]]
== AOT Cache
To use the AOT cache, you should first perform a training run on your application in extracted form:
@ -36,7 +36,7 @@ Here is an example of a `Dockerfile` using `jarmode`.
@@ -36,7 +36,7 @@ Here is an example of a `Dockerfile` using `jarmode`.
include::reference:partial$dockerfile[]
# Start the application jar - this is not the uber jar used by the builder
# This jar only contains application code and references to the extracted jar files
# This layout is efficient to start up and CDS friendly
# This layout is efficient to start up and CDS/AOT cache friendly
ENTRYPOINT ["java", "-jar", "application.jar"]
----
@ -53,14 +53,14 @@ Each of the `COPY` commands relates to the layers extracted by the jarmode.
@@ -53,14 +53,14 @@ Each of the `COPY` commands relates to the layers extracted by the jarmode.
Of course, a `Dockerfile` can be written without using the `jarmode`.
You can use some combination of `unzip` and `mv` to move things to the right layer but `jarmode` simplifies that.
Additionally, the layout created by the `jarmode` is CDS friendly out of the box.
Additionally, the layout created by the `jarmode` is CDS and AOT cache friendly out of the box.
[[packaging.container-images.dockerfiles.cds]]
== CDS
If you want to additionally enable xref:reference:packaging/class-data-sharing.adoc[CDS], you can use this `Dockerfile`:
If you want to additionally enable xref:reference:packaging/class-data-sharing.adoc#packaging.class-data-sharing.cds[CDS], you can use this `Dockerfile`:
If you want to additionally enable the xref:reference:packaging/class-data-sharing.adoc#packaging.class-data-sharing.aot-cache[AOT cache], you can use this `Dockerfile`:
[source,dockerfile]
----
include::reference:partial$dockerfile[]
# Execute the AOT cache training run
RUN java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf -Dspring.context.exit=onRefresh -jar application.jar
RUN java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot -jar application.jar && rm app.aotconf
# Start the application jar with AOT cache enabled - this is not the uber jar used by the builder
# This jar only contains application code and references to the extracted jar files
# This layout is efficient to start up and AOT cache friendly
@ -12,7 +12,7 @@ Certain PaaS implementations may also choose to extract archives before they run
@@ -12,7 +12,7 @@ Certain PaaS implementations may also choose to extract archives before they run
For example, Cloud Foundry operates this way.
Spring Boot supports extracting your application to a directory using different layouts.
The default layout is the most efficient, and is xref:reference:packaging/class-data-sharing.adoc[CDS friendly].
The default layout is the most efficient, and it is xref:reference:packaging/class-data-sharing.adoc#packaging.class-data-sharing.cds[CDS] and xref:reference:packaging/class-data-sharing.adoc#packaging.class-data-sharing.aot-cache[AOT cache] 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.