问题描述
Java效率低的主要原因是JVM每次都要临时去编译字节码然后执行。但是,既然JVM是针对特定操作系统的(每种操作系统都有自己的JVM),那么执行过程中总会有一个步骤是将字节码编译成对于特定操作系统的机器码。所以,JIT为什么不能在下一次启动的时候直接使用之前生成好的机器码而必须每次重新编译?
“‘为什么’之前的问题是‘是不是’”系列?
题主想要的功能在IBM JDK里有实现:
IBM Knowledge CenterThe JVM can also store ahead-of-time (AOT) compiled code in the cache for certain methods to improve the startup time of subsequent JVMs. The AOT compiled code is not shared between JVMs, but is cached to reduce compilation time when the JVM starts. The amount of AOT code stored in the cache is determined heuristically. You cannot control which methods get stored in the cache. You can set maximum and minimum limits on the amount of cache space used for AOT code, or you can disable AOT caching completely. See Class data sharing command-line options for more information.
IBM JDK好歹也是主流JDK之一,这个总得算了吧?
然后经典案例
Excelsior JET支持完全AOT编译,更加符合题主的胃口。
还不行的话Oracle也宣布正在为Oracle JDK开发AOT编译器:
JVMLS 2015 - Java Goes AOT另外JIT编译不是Java效率低的主要原因。首先要定义怎样算效率低,然后才可以讨论什么导致了那方面效率低。