这取决于您的JVM及其设置.维基百科:
For example,Sun’s Java Virtual Machine has two major modes—client and server. In client mode,minimal compilation and optimization is performed,to reduce startup time. In server mode,extensive compilation and optimization is performed,to maximize performance once the application is running by sacrificing startup time. Other Java just-in-time compilers have used a runtime measurement of the number of times a method has executed combined with the bytecode size of a method as a heuristic to decide when to compile.[4] Still another uses the number of times executed combined with the detection of loops.[5]
对于vanilla HotSpot JVM in -server模式的粗略近似是当JVM注意到某个方法已被大量调用时,通常会超过某些特定次数,从而发生JIT. (这就是为什么JVM被称为“HotSpot” – 因为它识别并优化代码中的“热点”.)此时,JVM知道一些事情:
>它知道这种方法值得花时间进行优化,因为它会被调用很多.
>它对这个函数的真实世界特征了解很多:
>如果if语句的一个分支比另一个分支更常见,那么它可以改进branch prediction
>如果是传递给此方法的List通常是一个ArrayList,因此它可以针对ArrayList的特定情况进行优化和内联.
请注意,如果您事先编译为机器代码,则不会有大量此类信息需要优化 – 编译器事先并不知道哪些路径比其他路径更常见.但是,如果您等到实际数据之后再进行优化,则可以做出更好的优化决策.
关于JIT做什么的更多细节是here.