JIT编译器和执行
我对 JIT 编译器感到困惑,
- 什么是 JIT 编译器?
- JIT编译器在每次执行之前或每次JVM加载时编译字节码?
I'm confused with JIT compiler,
- What is JIT compiler?
- JIT compiler compiles byte codes before each execution or each time JVM loads?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JIT 代表“准时制”。它是一个编译器,可在程序运行时将 Java 字节码转换为本机机器代码。
Sun 的 JIT 不会在每次运行 Java 程序时预先编译所有字节码;它包含一些非常复杂的逻辑来决定何时编译部分字节码,它使用的标准之一是代码的执行频率。
请参阅即时编译和HotSpot(维基百科)了解更多详细信息。
JIT stands for "Just In Time". It's a compiler that translates Java bytecode to native machine code as your program runs.
Sun's JIT does not compile all your bytecode up front each time you run a Java program; it contains some very sophisticated logic to decide when to compile parts of the bytecode, one of the criteria it uses is how often the code is executed.
See Just-in-time compilation and HotSpot (Wikipedia) for more details.
JIT 编译器是一种专门的编译器,它将监视 Java 字节码的运行,并在必要时将其编译为本机指令。
如果 JIT 确定您的代码的一部分运行非常频繁,或者将其编译为本机代码将显着提高性能,它就会这样做。
JIT 编译是完全可选的,Java 的标准实现允许您将其关闭。然而,确实没有理由将其关闭。
The JIT compiler is a specialized compiler that will watch Java bye code run and compile it into native instructions if necessary.
If the JIT determines that a part of your code is running very frequently, or that compiling it to native code will significantly increase performance, it will do so.
JIT compilation is completely optional, and the standard implementation of Java allows you to turn it off. However, there is really no reason to turn it off.
即时(JIT)编译器是一种将Java字节码(包含必须解释的指令的程序)转换为可以直接发送到处理器的指令的程序。
JIT 编译器作为独立的平台相关本机库提供。如果 JIT 编译器库存在,Java VM 会初始化 Java 本机接口 (JNI) 本机代码挂钩,以调用该库中可用的 JIT 函数,而不是解释器中的等效函数。 java.lang.Compiler 类用于加载本机代码库并在 JIT 编译器内开始初始化。JIT 编译器使用自己的调用程序。 Sun 生产版本检查方法访问位的值 ACC_MACHINE_COMPILED,以通知解释器该方法的代码已被编译并存储在加载的类中。首次调用方法时(取决于 JVM 供应商)JIT 编译器进行编译方法块转换为该方法的本机代码,并将其存储在该方法的代码块中。
一旦代码被编译,就会设置在 Sun 平台上使用的 ACC_MACHINE_COMPILED 位。
首先要记住的是,JIT 编译器在第二次调用方法时(取决于 JVM 供应商)实现了大部分速度改进。 JIT 编译器会编译整个方法,而不是逐行解释它,这在启用 JIT 的情况下运行应用程序时也可以提高性能。这意味着如果代码仅被调用一次,您将不会看到显着的性能提升。 JIT 编译器还会忽略类构造函数,因此如果可能,请将构造函数代码保持在最低限度。
JIT 编译器还通过不预先检查某些 Java 边界条件(例如空指针或数组越界异常)来实现较小的性能提升。 JIT 编译器知道它有空指针异常的唯一方法是通过操作系统发出的信号。由于信号来自操作系统而不是 Java VM,因此您的程序的性能会受到影响。为了确保使用 JIT 运行应用程序时获得最佳性能,请确保您的代码非常干净,没有空指针或数组越界异常等错误。
参考:http://java.sun.com/developer/onlineTraining/Programming/JDCBook/perf2.html
Just-in-time (JIT) compiler is a program that turns Java bytecode (a program that contains instructions that must be interpreted) into instructions that can be sent directly to the processor.
JIT compilers are supplied as standalone platform-dependent native libraries. If the JIT Compiler library exists, the Java VM initializes Java Native Interface (JNI) native code hooks to call JIT functions available in that library instead of the equivalent function in the interpreter.The java.lang.Compiler class is used to load the native library and start the initialization inside the JIT compiler.The JIT compiler uses its own invoker. Sun production releases check the method access bit for value ACC_MACHINE_COMPILED to notify the interpreter that the code for this method has already been compiled and stored in the loaded class.When a method is called the first time(depends on JVM vendor) the JIT compiler compiles the method block into native code for this method and stored that in the code block for that method.
Once the code has been compiled the ACC_MACHINE_COMPILED bit, which is used on the Sun platform, is set.
The first thing to remember is that the JIT compiler achieves most of its speed improvements the second time(depends on JVM vendor) it calls a method. The JIT compiler does compile the whole method instead of interpreting it line by line which can also be a performance gain for when running an application with the JIT enabled. This means that if code is only called once you will not see a significant performance gain. The JIT compiler also ignores class constructors so if possible keep constructor code to a minimum.
The JIT compiler also achieves a minor performance gain by not pre-checking certain Java boundary conditions such as Null pointer or array out of bounds exceptions. The only way the JIT compiler knows it has a null pointer exception is by a signal raised by the operating system. Because the signal comes from the operating system and not the Java VM, your program takes a performance hit. To ensure the best performance when running an application with the JIT, make sure your code is very clean with no errors like Null pointer or array out of bounds exceptions.
Reference :http://java.sun.com/developer/onlineTraining/Programming/JDCBook/perf2.html
JIT 编译器是 Java 1.1 附带的外部即时编译器。该术语现已过时。 Java 附带了一个内置了编译功能的“HotSpot”JVM。
The JIT compiler was an external just-in-time compiler that shipped with Java 1.1. The term is now obsolete. Java ships with a 'HotSpot' JVM that has compilation built in to it.