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


当前回答

非jit编译器获取源代码,并在编译时将其转换为特定于机器的字节代码。JIT编译器获取编译时生成的与机器无关的字节代码,并在运行时将其转换为特定于机器的字节代码。Java使用的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.

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)编译(也称为动态翻译或运行时编译)是一种执行计算机代码的方式,它涉及在程序执行期间(在运行时)而不是在执行之前进行编译。

IT编译结合了两种传统的机器代码转换方法——预先编译(AOT)和解释,并结合了两者的一些优点和缺点。JIT编译结合了编译代码的速度和解释的灵活性。

让我们考虑在JVM中使用的JIT,

例如,HotSpot JVM JIT编译器生成动态优化。换句话说,它们在Java应用程序运行时做出优化决策,并生成针对底层系统架构的高性能本机机器指令。

When a method is chosen for compilation, the JVM feeds its bytecode to the Just-In-Time compiler (JIT). The JIT needs to understand the semantics and syntax of the bytecode before it can compile the method correctly. To help the JIT compiler analyze the method, its bytecode are first reformulated in an internal representation called trace trees, which resembles machine code more closely than bytecode. Analysis and optimizations are then performed on the trees of the method. At the end, the trees are translated into native code.

跟踪树是在编程代码的运行时编译中使用的数据结构。跟踪树用于一种“即时编译器”,它跟踪在热点期间执行的代码并编译它。提到这一点。

参考:

http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html https://en.wikipedia.org/wiki/Just-in-time_compilation

Jit代表即时编译器 Jit是一个将Java字节代码转换为可以直接发送到处理器的指令的程序。

在特定的系统平台上使用java即时编译器(实际上是第二个编译器)将字节码遵从为特定的系统代码,一旦代码被jit编译器重新编译,它通常会在计算机中运行得更快。

即时编译器随虚拟机一起提供,可选使用。它将字节码编译为立即执行的特定于平台的可执行代码。

jit——来得正是时候 这个词本身就表示需要的时候(按需)

典型场景:

源代码完全转换为机器代码

JIT的场景:

源代码将转换为汇编语言,如结构[为ex IL(中间语言)为c#,字节码为java]。

中间代码仅在应用程序需要时才转换为机器语言,所需代码仅转换为机器代码。

JIT与非JIT的比较:

在JIT中,并不是所有的代码都先转换成机器码 所有必要的代码都将被转换成机器代码 如果调用的方法或功能不在机器中,那么 将被转换成机器代码…减少CPU的负担。 因为机器代码将在运行时....上生成JIT 编译器将生成优化运行的机器代码 机器的CPU架构。

JIT的例子:

在Java中,JIT是在JVM (Java虚拟机)中 在c#中,它是在CLR(公共语言运行库)中 在Android中,它是在DVM (Dalvik Virtual Machine)中,或者在更新的版本中是ART (Android RunTime)。