我听说过
>每个主要版本的JVM变得更快(在某些方面)
> 9的模块化将带来更快的启动时间.
为了加快Maven构建速度,我已经下载了jdk9-ea,并发现它需要更长的时间.此外,感觉Maven开始之前有更长的延迟.
我试图使用以下代码大致测量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,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),相同的2x间隙,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,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个成功的开始是50可以得到最高的数字.
最低的是 – 13.
解决方法
是的,目前的EA版本肯定有一些启发性的回归 – 一些原因是已知和积极的工作 – 其他更多的是一个“死亡一千次削减”的折磨:在JDK 9开发过程中积累的小而微不足道的低效率功能实现和集成,然后在实际发布之前必须进行微调和优化.
我也会注意到,您的8/8-ea启用运行类数据共享,但是您的9-ea安装不会(请注意,在-version输出中缺少“共享”).如果您运行java -Xshare:dump来生成默认的CDS存档,则可以在9-ea上获得更好的数字,有关详细信息,请参阅https://docs.oracle.com/javase/8/docs/technotes/guides/vm/class-data-sharing.html.
编辑:我刚刚意识到,在9个版本中,默认情况下已经关闭了共享,因此您还必须在9-ea中添加-Xshare:auto以启用共享.