Java 简明教程
Java - Dynamic CDS
What is CDS?
CDS 代表类数据共享。它在 JDK 5 中引入,通过加载核心类和共享 JVM 元数据的预处理归档来改进 JVM 的启动时间。当 JVM 初始化时,它会加载一组核心类,例如 java.lang 包类。使用 CDS,Java 支持为这些核心类创建一个预处理归档,以便可以通过直接使用预处理归档来改进初始化的正常流程(展开归档、验证类、生成字节码)。以下命令可从 JDK 5 开始使用,以创建一个 CDS 归档,供 JVM 在启动时使用。
CDS stands for Class Data Sharing. It was introduced in JDK 5 to improve the startup time of the JVM by loading the pre-processed archive of core class and shared JVM Metadata. When JVM Initializes, it loads set of core classes, for example, java.lang package classes. Using CDS, Java supports creating a pre-processed archive of such core classes so that the normal process of initialization (expand archive, validate class, generate bytecode) can be improved by directly using pre-processed archive. Following command can be used in JDK 5 onwards to create a CDS archive to be used by JVM at startup time.
$java -Xshare:dump -cp APITester.jar APITester
CDS 归档将存在于 JAVA 安装目录中。
The CDS archive will be available in JAVA installation directory.
$JAVA_HOME/lib/server/classes.jsa
or
$JAVA_HOME/bin/server/classes.jsa
当 JVM 初始化并被定向使用 CDS 时,此归档将用于加载核心类,而不是解压和验证类,从而提高启动时间。
When JVM is initialized and directed to use CDS, this archive will be used to load core classes instead of decompressing and verifying the classes thus improving the startup time.
What is Dynamic CDS?
CDS(类数据共享)是 JVM 的一项重要功能,可提升应用程序加载的启动时间。因为它允许在不同的 JVM 之间共享类元数据,从而缩短启动时间并降低内存占用。Java 10 通过提供 AppCDS(应用程序 CDS)增强了 CDS,它允许开发人员访问在共享归档中包含应用程序类。Java 12 将 CDS 归档设为默认值。
CDS, Class Data Sharing is an important feature of JVM to boost the startup time of an application loading. As it allows to share class metadata across different JVMs, it reduces the startup time and memory footprint. Java 10 enhanced CDS by giving AppCDS, application CDS which gave developers access to include application classes in a shared archive. Java 12 set CDS archive as default.
但是,创建 CDS 的过程很繁琐,因为开发人员必须对其应用程序进行多次试验才能在第一步创建类列表,然后将该类列表转储到归档中。然后,此归档可用于在 JVM 之间共享元数据。
But the process of creating a CDS was tedious as developers has to go through multiple trials of their applications to create a class list as first step and then dump that class list into an archive. Then this archive can be used to share metadata between JVMs.
自 Java 13 起,Java 现在具有动态归档。现在,开发人员可以在应用程序退出时生成共享归档。因此,不再需要试运行。
From Java 13 onwards, now java has dynamic archiving. Now developers can generate a shared archive at the time of application exit. So trial runs are no more needed.
Create Dynamic CDS?
以下步骤展示了如何使用选项 -XX:ArchiveClassesAtExit 创建基于默认系统归档的动态共享归档,并传递归档名称。
Following step showcases to create a dynamic shared archive on top of default system archive using option -XX:ArchiveClassesAtExit and passing the archive name.
$java -XX:ArchiveClassesAtExit=sharedApp.jar -cp APITester.jar APITester
生成共享归档后,可以使用 -XX:SharedArchiveFile 选项来运行应用程序。
Once generated the shared archive can be used to run the application using -XX:SharedArchiveFile option.
$java -XX:SharedArchiveFile=sharedApp.jar -cp APITester.jar APITester
Example
考虑以下示例 −
Consider the following example −
APITester.java
public class APITester {
public static void main(String[] args) {
System.out.println("Welcome to TutorialsPoint.");
}
}