Dynamic Class-Data Sharing (CDS) Archives extend the existing CDS feature by allowing classes to be archived dynamically at application exit, not just during JDK build time.
Class-Data Sharing (CDS) improves startup performance by sharing common class metadata between different Java processes. This reduces memory footprint and speeds up application startup.
- Created during JDK build time
- Contains only JDK classes
- Fixed at JDK installation
- Created at application runtime
- Contains application-specific classes
- Generated when application exits
- Can be updated without rebuilding JDK
java -XX:ArchiveClassesAtExit=app-cds.jsa \
-cp myApp.jar \
com.example.MyAppThis command:
- Runs your application normally
- Archives all loaded classes when the application exits
- Creates
app-cds.jsafile
java -XX:SharedArchiveFile=app-cds.jsa \
-cp myApp.jar \
com.example.MyAppThis command:
- Loads classes from the archive
- Faster startup time
- Reduced memory footprint
# Run application and create archive
java -XX:ArchiveClassesAtExit=myapp-cds.jsa \
-cp target/myapp.jar \
com.example.Main
# Archive is created at: myapp-cds.jsa# Use the archive for faster startup
java -XX:SharedArchiveFile=myapp-cds.jsa \
-cp target/myapp.jar \
com.example.Main-XX:ArchiveClassesAtExit=<filename>: Create archive at exit
-XX:SharedArchiveFile=<filename>: Use existing archive-Xshare:on: Force CDS (fail if archive not found)-Xshare:auto: Use CDS if available (default)-Xshare:off: Disable CDS
- Faster Startup: Classes are pre-loaded from archive
- Reduced Memory: Shared class metadata across processes
- Application-Specific: Includes your application classes
- Dynamic: No JDK rebuild required
- Easy Updates: Regenerate archive when classes change
- Frequent restarts benefit from faster startup
- Multiple instances can share memory
- Faster iteration cycles
- Quicker test execution
- Reduced startup time
- Lower memory footprint
- Better resource utilization
-
Create Archive in Production Environment
- Archive should match production classpath
- Use same JDK version
-
Update Archive When Needed
- Regenerate when classes change
- Update when dependencies change
-
Test Archive Compatibility
- Verify archive works with new code
- Test startup performance
-
Monitor Performance
- Measure startup time improvement
- Check memory usage reduction
Error: Could not open shared archive file
Solution: Ensure archive path is correct and file exists
Error: Shared archive file is incompatible
Solution: Regenerate archive with current classes
Error: Failed to create archive
Solution: Check file permissions and disk space
# 1. Build application
mvn clean package
# 2. First run - create archive
java -XX:ArchiveClassesAtExit=app-cds.jsa \
-cp target/app.jar \
com.example.Main
# 3. Verify archive created
ls -lh app-cds.jsa
# 4. Use archive in subsequent runs
java -XX:SharedArchiveFile=app-cds.jsa \
-cp target/app.jar \
com.example.Main
# 5. Measure performance improvement
time java -XX:SharedArchiveFile=app-cds.jsa \
-cp target/app.jar \
com.example.MainTypical improvements:
- Startup Time: 10-30% faster
- Memory: 5-10% reduction
- First Request: Slightly faster (classes pre-loaded)
- Archive must match classpath
- Archive is JDK version specific
- Some classes cannot be archived
- Archive size grows with application size
- JEP 350: Dynamic CDS Archives
- Java Documentation: Class-Data Sharing
- Performance Tuning Guide
Dynamic CDS Archives provide an easy way to improve application startup performance by archiving application classes at runtime. This feature is particularly beneficial for applications that restart frequently or have long startup times.