Java 9 ea 中是否存在启动时间回归?
2022-09-04 21:39:58
我听说过
- JVM 随着每个主要版本而变得更快(在某些方面)
- 9的模块化将带来更快的启动时间。
为了加快Maven构建的速度,我下载并发现它需要更长的时间。此外,感觉在Maven开始之前有更长的延迟。jdk9-ea
我试图使用以下代码粗略地测量JVM启动时间
public class Sampler {
public static void main(String[] args) throws IOException, InterruptedException {
long t = System.currentTimeMillis();
if (args.length == 0 || args[0].startsWith("-")) {
sample(30, args);
} else {
long t0 = Long.parseLong(args[0]);
System.out.println(t - t0);
}
}
static void sample(int n, String[] options) throws IOException, InterruptedException {
File samples = new File("samples.txt");
for (int i = 0; i < n; i++) {
String javaPath = String.join(
System.getProperty("file.separator"),
System.getProperty("java.home"),
"bin",
"java");
List<String> command = new ArrayList<String>();
command.add(javaPath);
command.addAll(Arrays.asList(options));
command.add("Sampler");
command.add(Long.toString(System.currentTimeMillis()));
ProcessBuilder processBuilder = new ProcessBuilder(command)
.inheritIO()
.redirectOutput(ProcessBuilder.Redirect.appendTo(samples));
Process process = processBuilder.start();
process.waitFor();
}
prettyPrint(samples);
samples.delete();
}
...
}
从Java 9开始需要两倍的时间
>java -version java version "1.8.0_74" Java(TM) SE Runtime Environment (build 1.8.0_74-b02) Java HotSpot(TM) Client VM (build 25.74-b02, mixed mode, sharing) >javac Sampler.java && java Sampler n=30 units=milisec min=124 max=205 mean=143 median=132 >java -version java version "9-ea" Java(TM) SE Runtime Environment (build 9-ea+111) Java HotSpot(TM) Client VM (build 9-ea+111, mixed mode) >javac Sampler.java && java Sampler n=30 units=milisec min=279 max=387 mean=301 median=294 >javac Sampler.java && java Sampler -XX:+UseParallelGC n=30 units=milisec min=279 max=382 mean=297 median=292 >java -version java version "1.8.0_76-ea" Java(TM) SE Runtime Environment (build 1.8.0_76-ea-b04) Java HotSpot(TM) Client VM (build 25.76-b04, mixed mode, sharing) >javac Sampler.java && java Sampler n=30 units=milisec min=123 max=227 mean=159 median=141 >java Sampler -XX:+UseG1GC n=99 units=milisec min=188 max=340 mean=213 median=199
注意:最初我使用的是服务器虚拟机(x64),同样的2倍差距,Java9启动时间约为0.6秒。
后java -Xshare:dump
>java -version java version "9-ea" Java(TM) SE Runtime Environment (build 9-ea+111) Java HotSpot(TM) Client VM (build 9-ea+111, mixed mode, sharing) >javac Sampler.java && java Sampler n=50 units=milisec min=228 max=422 mean=269 median=269 >javac Sampler.java && java Sampler -Xshare:on <error messages> n=44 units=milisec min=227 max=392 mean=247 median=238 >javac Sampler.java && java Sampler -Xshare:off n=50 units=milisec min=280 max=513 mean=315 median=288 >javac Sampler.java && java Sampler -Xshare:auto n=50 units=milisec min=228 max=361 mean=283 median=285
使用 Java 8 ea
>java -Xshare:off Sampler n=99 units=milisec min=124 max=264 mean=150 median=136
错误信息:
An error has occurred while processing the shared archive file. Unable to map ReadOnly shared space at required address. Error occurred during initialization of VM Unable to use shared archive.
50个中的44个成功开始是我能得到的最高数字。最低为- 13。