与非JIT编译器相比,JIT编译器具体做什么?有人能给出简洁易懂的描述吗?


当前回答

JVM actually performs compilation steps during runtime for performance reasons. This means that Java doesn't have a clean compile-execution separation. It first does a so called static compilation from Java source code to bytecode. Then this bytecode is passed to the JVM for execution. But executing bytecode is slow so the JVM measures how often the bytecode is run and when it detects a "hotspot" of code that's run very frequently it performs dynamic compilation from bytecode to machinecode of the "hotspot" code (hotspot profiler). So effectively today Java programs are run by machinecode execution.

其他回答

正如其他人提到的

JIT代表Just-in-Time,这意味着代码在需要时才编译,而不是在运行时之前。

为了给上面的讨论补充一点,JVM维护一个函数执行时间的计数。如果该计数超过预定义的限制,JIT将代码编译为处理器可以直接执行的机器语言(不像一般情况下,javac将代码编译为字节码,然后java -解释器逐行解释这个字节码,将其转换为机器代码并执行)。

另外,下次计算此函数时,将再次执行相同的编译代码,而不像常规解释那样逐行重新解释代码。这使得执行速度更快。

JIT代表Just-in-Time,这意味着代码在需要时才编译,而不是在运行时之前。

这是有益的,因为编译器可以生成针对特定机器优化的代码。静态编译器,就像普通的C编译器一样,将所有代码编译为开发人员机器上的可执行代码。因此,编译器将基于一些假设执行优化。它可以编译得更慢,做更多的优化,因为它不会降低用户执行程序的速度。

即时编译器(JIT)是一种软件,它接收一个不可执行的输入,并返回要执行的适当机器代码。例如:

Intermediate representation    JIT    Native machine code for the current CPU architecture

     Java bytecode            --->        machine code
     Javascript (run with V8) --->        machine code

其结果是,对于特定的CPU体系结构,必须安装适当的JIT编译器。

区别编译器、解释器和JIT

虽然在一般情况下,当我们想要将源代码转换为机器码时可能会有例外,但我们可以使用:

Compiler: Takes source code and returns a executable Interpreter: Executes the program instruction by instruction. It takes an executable segment of the source code and turns that segment into machine instructions. This process is repeated until all source code is transformed into machine instructions and executed. JIT: Many different implementations of a JIT are possible, however a JIT is usually a combination of a compiler and an interpreter. The JIT first turn intermediary data (e.g. Java bytecode) which it receives into machine language via interpretation. A JIT can often measures when a certain part of the code is executed often and the will compile this part for faster execution.

我知道这是一个老线程,但是运行时优化是JIT编译的另一个重要部分,这里似乎没有讨论它。基本上,JIT编译器可以在程序运行时监视它,以确定改进执行的方法。然后,它可以在运行时动态地进行这些更改。谷歌JIT优化(javaworld有一篇关于它的很好的文章。)

非jit编译器获取源代码,并在编译时将其转换为特定于机器的字节代码。JIT编译器获取编译时生成的与机器无关的字节代码,并在运行时将其转换为特定于机器的字节代码。Java使用的JIT编译器允许一个二进制文件无需修改就能在多种平台上运行。