By Yi Yang
Java applications are expected to start quickly because of the advent of the cloud-native era. It is becoming increasingly popular to reduce resource costs by dynamically scaling and using serverless computing. Alibaba has done a lot of work and exploration to meet the demands for quick start of applications in Serverless computing scenarios, including AppCDS, Ahead of Time Compilation (AOT), fast class indexing(JarIndex), class pre-initialization, and other technologies to optimize language runtime. Users can obtain up to three times the startup performance improvement without modifying any code. One of the cutting-edge technologies is class pre-initialization.
Profiling data on Java startup stage shows that the main reason for Javas' slow startup is that it takes much time to load, link, and initialize classes, which we aim to break one by one.
public class Foo {
private static HashMap<Integer, Integer> cache;
static {
if (cache == null) {
cache = new HashMap<>();
}
for (int i = 0; i < 1024; i++) {
cache.put(i, 0);
}
}
}
No matter when, no matter how many times, the result of initialization of the Foo class is the same: creating a hash table with a size of 1024 as a cache will not affect the external environment. In such cases, we can apply class pre-initialization, i.e. dump cache objects into CDS images, and directly map cache objects in CDS images to the Java G1 heap when JVM starts (8042668: Provide GC support for shared heap ranges in Class Data Sharing). When the program runs, skip the static code block initialization of the Foo class to speed up the startup:
public class Foo {
private static HashMap<Integer, Integer> cache; // materialized from G1 archive region
static {
// skip execution
}
}
Another classical example is java.lang.Integer$IntegerCache class. It is an excellent candidate for class pre-initialization, which always creates a range of integer cache in [-128,127].
The native class pre-initialization mechanism requires explicit insertions of object materialization calls (jdk.internal.misc.VM.initializeFromArchive(...)
) on certain initialization points and only supports a few restricted classes, which are hard-coded in the JVM code and cannot be extended. Alibaba and Google have proposed the Eclipse Adoptium FastStartup Incubator project They aim to explore the Java quick start technologies, including (but not limited to) class pre-initialization. As aforementioned requirements, only the initialization phase that does not take extra side effects is able to apply class pre-initialization. Alibaba and Google have explored and contributed two approaches for safe and flexible class pre-initialization.
jdk.internal.vm.annotation.Preserve
), which allows more safe classes to be pre-initialized through manual labeling.At the same time, we have added a security check mechanism of class pre-initialization to the JVM to ensure the virtual machine can still work normally in the worst case. The whole workflow is shown in the figure below:
Class pre-initialization extends shared GC heap, it's a sub-phase of AppCDS dump time workflow, the following performance evaluation concerns AppCDS(Deep blue one) and AppCDS with Class pre-initialization(Green one). We found that about 90% (1800/2000) of classes can be pre-initialized safely in a better scenario, and average startup performance is improved by 19.2%.In a larger-scale evaluation, we found that class pre-initialization is slightly better than AppCDS, with a 5% performance improvement.
This is because some classes with high time consumption on initialization usually have side effects. They cannot be pre-initialized, but the pre-initialization mechanism of these classes will still be performed in common paths, and these hot paths will cause a performance penalty.
There have been three carriages for Java quick start for a long time: OpenJDK CRaC, JVM Runtime optimizations(AppCDS-based optimizations, AOT, JarIndex, etc), and static compilation (OpenJDK Leyden). All of them are committed to optimizing Java application startup performance from different directions. Alibaba has achieved good results in the second direction. We will continue moving forward and continuously optimize the startup performance of Java applications.
87 posts | 5 followers
FollowAlibaba Cloud Community - October 14, 2022
Alibaba Cloud Community - September 30, 2022
Alibaba Cloud Community - December 20, 2022
Alibaba Cloud Community - December 16, 2024
Alibaba Cloud Native Community - February 24, 2023
Alibaba Cloud Native Community - September 13, 2023
87 posts | 5 followers
FollowAlibaba Cloud Function Compute is a fully-managed event-driven compute service. It allows you to focus on writing and uploading code without the need to manage infrastructure such as servers.
Learn MoreAccelerate and secure the development, deployment, and management of containerized applications cost-effectively.
Learn MoreVisualization, O&M-free orchestration, and Coordination of Stateful Application Scenarios
Learn MoreServerless Application Engine (SAE) is the world's first application-oriented serverless PaaS, providing a cost-effective and highly efficient one-stop application hosting solution.
Learn MoreMore Posts by OpenAnolis