Improve the startup time and footprint of Java programs
Do you know what a Native Build is? Do you know Project Leyden? Do you know Project CRaC? Let’s talk a little bit about them.
You’ve probably heard that Java is slow or too heavy, and maybe you've also seen some memes like this one:
I will show you that this is not true.
Project Graal and NativeBuild
How many seconds does it take to start your application, and how much memory it uses? Maybe 5 seconds? Maybe 300mb?
Well, using native compilation with GraalVM is a good way to significantly decrease it. However, using a native build can come with some challenges. If you are using Quarkus, Spring, or any famous Java framework, chances are it supports native build.
Changing the startup time to 600ms and 80mb is probably something we want, mainly if you have thousands of microservices, it can save a lot of money. However, it can bring some challenges:
Long Build: The build will take longer than a normal build, it could take 5x longer, however, this is being addressed by GraalVM and will probably be improved soon.
Reflection, serialization, and usage of native components can be an issue. The frameworks and libraries are being updated to support the native build, and also give an easy way to support it. Spring provides an interface
RuntimeHintsRegistrar
to configure external resources and register types for serialization and reflection.
GraalVM provides a high-performance runtime that supports multiple languages, offering both JIT and AOT compilation for performance optimization and the ability to create native images for fast startup and low memory usage. GraalVM also emphasizes interoperability between languages.
If you want to know more about it, Alina Yurenko gave a remarkable talk on Spring I/O 2024: Going AOT: Everything you need to know about GraalVM for Java applications
Project Leyden
The project Leyden goal is to improve the startup time, time to peak performance, and footprint of Java programs.
Project Leyden focuses on improving overall performance, startup times, and consistency by utilizing techniques like AOT compilation and static configuration, making it beneficial for microservices and containerized environments.
If you want to know more about it, I do recommend the talk about Project Leyden By Brian Goetz at Devoxx.
Class Data Sharing - CDS
Class Data Sharing (CDS) is a JVM feature that can help reduce the startup time and memory footprint of Java applications.
This feature is available for Spring Boot 3.3 or above, and it is a good option to decrease the startup time and memory footprint.
Sébastien Deleuze gave a talk about this subject on Spring I/O 2024: Efficient containers with Spring Boot 3, Java 21 and CDS
Project CRaC
The Coordinated Restore at Checkpoint (CRaC) is an OpenJDK feature that provides a fast start and immediate performance for Java applications.
This project does not aim to reduce the memory footprint; however, it can help a lot to reduce startup time.
A Java application and JVM are started from an image in a warmed-up form. The image is created from a running Java instance at arbitrary point of time ("checkpoint"). The start from the image ("restore") continues from the point when checkpoint was made.
Project CRaC is primarily about creating a mechanism to checkpoint and restore Java applications, making it particularly useful for scenarios requiring rapid startup and state restoration.
Conclusion
Java has been improved over time, and there are many new ways to improve the startup time and also the memory footprint. If you are a Java developer, you should know more about it.
These techniques could save a lot of money for your company, and also a lot of time for your team.
Are you using any one of these techniques?
Do you want to know more about them?