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


当前回答

一开始,编译器负责将高级语言(定义为比汇编程序更高级别)转换成目标代码(机器指令),然后(通过链接器)将其链接成可执行文件。

At one point in the evolution of languages, compilers would compile a high-level language into pseudo-code, which would then be interpreted (by an interpreter) to run your program. This eliminated the object code and executables, and allowed these languages to be portable to multiple operating systems and hardware platforms. Pascal (which compiled to P-Code) was one of the first; Java and C# are more recent examples. Eventually the term P-Code was replaced with bytecode, since most of the pseudo-operations are a byte long.

即时(JIT)编译器是运行时解释器的一个特性,它不是在每次调用方法时都解释字节码,而是将字节码编译为运行机器的机器代码指令,然后调用此目标代码。理想情况下,运行目标代码的效率将克服每次运行时重新编译程序的低效率。

其他回答

一开始,编译器负责将高级语言(定义为比汇编程序更高级别)转换成目标代码(机器指令),然后(通过链接器)将其链接成可执行文件。

At one point in the evolution of languages, compilers would compile a high-level language into pseudo-code, which would then be interpreted (by an interpreter) to run your program. This eliminated the object code and executables, and allowed these languages to be portable to multiple operating systems and hardware platforms. Pascal (which compiled to P-Code) was one of the first; Java and C# are more recent examples. Eventually the term P-Code was replaced with bytecode, since most of the pseudo-operations are a byte long.

即时(JIT)编译器是运行时解释器的一个特性,它不是在每次调用方法时都解释字节码,而是将字节码编译为运行机器的机器代码指令,然后调用此目标代码。理想情况下,运行目标代码的效率将克服每次运行时重新编译程序的低效率。

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

Just In Time编译器: 它将java字节码编译成特定CPU的机器指令。

例如,如果我们在java代码中有一个循环语句:

while(i<10){
    // ...
    a=a+i;
    // ...
 }

如果i的值为0,上述循环代码将运行10次。

没有必要一次又一次地编译字节码10次,因为相同的指令将执行10次。在这种情况下,只需要编译该代码一次,并且可以将值更改所需的次数。因此,Just In Time (JIT) Compiler会跟踪这些语句和方法(如上所述),并将这些字节代码片段编译为机器代码以获得更好的性能。

另一个类似的例子是在字符串/句子列表中使用“正则表达式”搜索模式。

JIT Compiler不会将所有代码编译为机器代码。它在运行时编译具有类似模式的代码。

请参阅了解JIT的Oracle文档以了解更多信息。

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

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.