3 Minutes read Tech 

geneated by DALL.E

While the use of native images (see https://medium.com/ekino-france/native-image-with-spring-boot-70f32788528c) with Spring Boot can significantly enhance performance by converting Java applications into standalone executables, it often introduces additional complexity. Startup time has become increasingly important as Java applications are now commonly deployed in lightweight, scalable containers where rapid initialization and efficient resource usage are crucial for maintaining performance and responsiveness. The process of building and maintaining native images can be challenging, especially when dealing with larger codebases and numerous dependencies. Fortunately, with the advent of Spring Boot 3.3, developers can now leverage Class Data Sharing (CDS) as an alternative solution to achieve faster startup times and better memory efficiency without the complexities associated with native images.

This article will explore how to effectively use CDS with Spring Boot applications, providing detailed steps to integrate and optimize your applications for improved performance. By the end of this guide, you’ll understand the benefits of CDS and how to implement it to enhance your Spring Boot projects.

Understanding Class Data Sharing (CDS)

Class Data Sharing (CDS) is a powerful JVM feature designed to enhance the startup time and reduce memory footprint by sharing a pre-loaded set of classes among multiple JVM instances.

Class Data Sharing (CDS) enables the JVM to load classes from a shared archive file, which significantly reduces the time needed to start an application and optimizes memory usage. This feature is particularly beneficial for applications with large codebases and for microservices that need to start quickly and efficiently.

Benefits of CDS for Spring Boot Applications

1. Faster Startup Times: By preloading and sharing commonly used classes, CDS can greatly reduce the time it takes for a Spring Boot application to start.
2. Memory Optimization: Sharing class data across multiple JVM instances reduces the overall memory consumption, which is particularly useful in environments with limited resources, such as containers.

How to Integrate CDS with Spring Boot

Step 1: Global configuration

Creating a CDS archive involves starting your Spring Boot application in a way that records the classes loaded during startup. This process generates a shared archive file that can be used in subsequent runs.

If you have never used CDS, start with the global classes dump.

java -Xshare:dump

Step 2: Generate the CDS Archive

Generate Class List: Run your application with the -Xshare:off option to create a list of loaded classes.

java -Xshare:off -Dspring.context.exit=onRefresh -XX:DumpLoadedClassList=classes.lst -jar my-spring-boot-app.jar

If you are using databases or other external resources, you can disable them for the training.

Step 3: Use the CDS Archive

Once the archive is created, you can start your application using this archive to benefit from faster startup times.

java -Xshare:on -XX:SharedArchiveFile=classes.lst -jar my-spring-boot-app.jar

Enhancements with Spring Framework 6.1

Spring Framework 6.1 introduces optimizations specifically designed to leverage CDS. These enhancements focus on better class loading strategies and integration points that make it easier to generate and utilize CDS archives effectively.

Automating the CDS Process

For continuous integration and deployment pipelines, automating the creation and usage of CDS archives is crucial. Tools such as `jcmd` can automate the dumping and loading processes, ensuring that your applications always start with the most optimized set of classes.

Best Practices

1. Regular Updates: Keep your CDS archives updated with the latest class data, especially after significant changes to your codebase.
2. Monitor Performance: Regularly monitor the performance impact of CDS to ensure that it meets your application’s needs.
3. Use with Containers: Integrate CDS into your containerized environments to maximize the benefits of reduced startup times and optimized memory usage.

Conclusion

Integrating Class Data Sharing with Spring Boot is a strategic move to enhance the performance of your applications. By following the steps outlined above and leveraging the latest features of Spring Framework, you can achieve faster startup times and better memory efficiency.

Although it heavily depends on the application, for a simple application, startup times can often follow the pattern illustrated in this diagram.

For more detailed information, refer to the following resources:


A Comprehensive Guide to Using Class Data Sharing (CDS) with Spring Boot was originally published in ekino-france on Medium, where people are continuing the conversation by highlighting and responding to this story.