简体   繁体   English

Java代码和JIT编译

[英]Java code and JIT compilation

Java code is compiled to bytecode which it is portable across many platforms. Java代码被编译为字节码,它可以在许多平台上移植。 But Java also is JIT compiled which happens on the fly. 但Java也是JIT编译的,它是即时发生的。

Does this mean Java is compile twice? 这是否意味着Java编译两次? first by us to produce the bytecode and the second by the JVM? 我们首先通过JVM生成字节码和第二个字节码? Thanks. 谢谢。

TMK, when you compile you are compiling for the JVM platform. TMK,在编译时,您正在为JVM平台进行编译。 Then when you run the app on the JVM on any machine, certain parts of code that are used frequently get compiled into code native to that machine for optimization. 然后,当您在任何计算机上的JVM上运行应用程序时,经常使用的某些代码部分将被编译为该计算机的本机代码进行优化。

So in short: Yes, but for a very good reason 所以简而言之:是的,但是有一个很好的理由

Your code may be compiled from bytecode into native code by the JVM if it's "hot enough"; 如果JVM“足够热”,你的代码可以由字节码编译成本机代码; and it may be so compiled several times, with the old version being discarded, depending on the runtime characteristics of your program. 并且可能会多次编译,旧版本将被丢弃,具体取决于程序的运行时特性。

The JIT is a complicated beast; JIT是一个复杂的野兽; in fact the Sun JVM has two JITs (-client and -server) that behave differently from each other, and some implementations even support both JITs running together (so you may have interpreted bytecode running alongside code compiled by two different JITs in your application). 事实上,Sun JVM有两个JIT(-client和-server),它们的行为彼此不同,有些实现甚至支持同时运行的两个JIT(因此您可能已经解释了字节码与应用程序中两个不同JIT编译的代码一起运行) 。

I recommend you read more about Hotspot (the most common JIT since it's the Sun one) if you're really interested in this subject. 如果你真的对这个主题感兴趣,我建议你阅读更多关于Hotspot(最常见的JIT,因为它是Sun的一个)。 You can start at Sun's page . 您可以从Sun的页面开始。

Does this mean Java is compile twice? 这是否意味着Java编译两次? first by us to produce the bytecode and the second by the JVM? 我们首先通过JVM生成字节码和第二个字节码? Thanks. 谢谢。

You could say that, once using information available in the source code (compiler), then the other in runtime (JVM/JIT) when information about the specific hardware is available, along with some profiling to decide what gets to be JIT-compiled or not. 你可以说,一旦使用源代码 (编译器)中可用的信息,那么另一个在运行时(JVM / JIT),当有关特定硬件的信息可用时,以及一些分析来决定什么是JIT编译或不。

The mechanism is 机制是

Java -> Byte Code (compiled By java compiler) Java - > Byte Code(由java编译器编译)

ByteCode -> native code (interpreted by JVM) ByteCode - >本机代码(由JVM解释)

Short answer: Yes kind of. 简短回答:是的。

The longest one: That is 2 different things. 最长的一个:这是两件不同的事情。 The first compilation is from the source code to the bytecode often call the intermediate representation (IR) in the compilation field. 第一个编译是从源代码到字节码,通常在编译字段中调用中间表示(IR)。

Then the VM take the bytecode and translate it back to the native code on which platform it is installed. 然后VM获取字节码并将其转换回安装它的平台上的本机代码。

This is 2 completely different kind of compilation. 这是两种完全不同的编译方式。 The second is not even quite a compilation since there is no syntax checker scope analyzer... Well there is some check but not the same kind of check that you have in a compiler. 第二个甚至不是一个很好的编译,因为没有语法检查器范围分析器......好吧有一些检查但不是你在编译器中的那种检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM