CDS and AppCDS: Class Data Sharing for Faster JVM Startup
Learn how JVM Class Data Sharing works, create an AppCDS archive with Oracle JDK 21, and benchmark startup on your own workload.
Learn how JVM Class Data Sharing works, create an AppCDS archive with Oracle JDK 21, and benchmark startup on your own workload.
CDS and AppCDS: Class Data Sharing for JVM Startup
Class Data Sharing (CDS) stores selected JVM class metadata in an archive so later processes can reuse it. Application Class Data Sharing (AppCDS) extends the archive to include eligible application and library classes. It can reduce startup work and allow read-only archive pages to be shared across compatible JVM processes, but the size of any benefit depends on the application and runtime.
Version scope: Commands in this guide target Oracle JDK 21 and follow its official CDS documentation. They have not been benchmarked in this article. Other vendors and JDK releases may differ; check their documentation and run the benchmark procedure below on the exact runtime you deploy.
What the archive shares
Oracle JDK 21 enables a default CDS archive by default. AppCDS can extend sharing to application classes. An archive is tied to its runtime environment: use the same JDK vendor/build, relevant JVM options, class path, and application build when creating and consuming it. Regenerate it after a JDK or application change that makes the archive incompatible. See Oracle’s JDK 21 CDS guide and java launcher documentation.
Class sharing does not mean that application objects or mutable heap state are shared between processes. The archive contains class metadata; each JVM still has its own objects and mutable runtime state.
Create and use a dynamic archive
The following example uses an existing executable JAR named app.jar. Point JAVA_HOME at the Oracle JDK 21 installation used in production. Keep the same runtime options, especially heap settings, in the archive and comparison runs.
export JAVA_HOME=/path/to/oracle-jdk-21
"$JAVA_HOME/bin/java" --version
# Baseline: disable the default CDS archive for a comparison run.
"$JAVA_HOME/bin/java" -Xshare:off -jar app.jar
# Run once to create the archive when the application exits.
"$JAVA_HOME/bin/java" -XX:+AutoCreateSharedArchive \
-XX:SharedArchiveFile=app.jsa -jar app.jar
# Start again with the same JDK, application, and options to use app.jsa.
"$JAVA_HOME/bin/java" -XX:+AutoCreateSharedArchive \
-XX:SharedArchiveFile=app.jsa -jar app.jar
On the first archive-enabled run, Oracle JDK 21 creates the archive if it does not exist; a later compatible run can use it. Confirm archive use with the JDK’s class-loading diagnostics, and treat an archive warning or fallback as a failed measurement rather than a speed result. The JDK 21 manual documents -XX:+AutoCreateSharedArchive and archive restrictions.
Compare startup fairly
Do not compare one baseline launch with one archived launch. Use the same machine, OS image, CPU limits, container memory, application artifact, configuration, JDK vendor/build, JVM flags, and readiness endpoint. Separate two conditions:
- Warm filesystem cache: repeat launches while the OS may retain the JDK and application files in memory.
- Cold filesystem cache: measure in a controlled environment after a documented cache reset or fresh machine start. Do not describe a normal container restart as cold unless the host page cache is also cold.
For each condition, collect multiple launches for -Xshare:off and for the generated archive. Record time from process start to the same application-ready signal, plus memory metrics you care about. Report the sample count, median and tail latency, JDK vendor/build, OS, hardware or container limits, JVM flags, and whether the run was cold or warm. Do not include the first archive-generation run in the archived startup results. If the measurements overlap or the difference is too small to matter to the service, keep the simpler deployment.
This is a measurement procedure, not a result claim. No universal percentage is promised: the Oracle documentation says the saved fraction tends to be larger for applications that are small relative to the core classes they use, but a reader’s workload must establish the actual effect.
Compatibility and operational costs
Treat the archive as a build artifact produced for a specific runtime and application. Build and run it in compatible environments, and regenerate it when upgrading the JDK or changing the class path or relevant JVM options. Test the real container image and launcher command; a local archive created with a different JDK may be rejected or ignored. Keep a non-archive startup path available so a bad archive does not block deployment.
CDS is most worth evaluating when startup time or per-process memory is part of a measured service objective, such as short-lived jobs or frequently restarted services. For a long-running process, compare startup savings with archive generation, storage, validation, and rollout work. The archive does not replace startup profiling or runtime memory measurement.
Further reading
- Class Loader Subsystem — how JVM class loaders locate and define classes.
- Oracle JDK 21 CDS documentation — archive creation, compatibility, and CDS options.
- Oracle JDK 21
javacommand reference — launcher options and archive restrictions.
Category
Related Posts
JVM in Containers: Cgroup Memory Limits and Heap Sizing
A guide to how the JVM detects container memory limits, configures heap accordingly, and avoids pitfalls when running Java in Docker and Kubernetes.
JVM Bytecode Verification: Type Checking and Stack Map Frames
A practical guide to JVM bytecode verification by class-file version, including type inference, StackMapTable frames, VerifyError debugging, and verification limits.
Heap Walking and Allocation Tracking: TLABs and Analysis
Understand how the JVM allocates memory with TLABs, how to track allocations with low overhead, and how heap walking tools analyze object graphs.