java 如果JIT正在执行字节码到机器指令的转换,JVM有什么用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16439512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:57:04  来源:igfitidea点击:

What is the use of JVM if JIT is performing bytecode conversion to machine instructions

javajvmjit

提问by Anusha

I am really struggling to understand the following thing

我真的很难理解以下事情

Previously I know:

以前我知道:

When a Java program is compiled .classfile will be generated. In that code is in the form of bytes. Then the JVMwill translate that byte code into machine understandable format.

当一个 Java 程序被编译时.class会生成文件。在那个代码中是字节的形式。然后将该JVM字节码转换为机器可理解的格式。

Now I see in one of the questions in SO

现在我在 SO中的一个问题中看到

A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine

即时 (JIT) 编译器是运行时解释器的一个特性,它不是每次调用方法时都解释字节码,而是将字节码编译成运行机器的机器码指令

So here JIT is converting the bytecode to machine instructions. Then what is the use of JVM. We are able to do this with JIT. In my knowledge JIT is for only improving the performance of JVM.

所以这里 JIT 是将字节码转换为机器指令。那么JVM有什么用呢。我们可以通过 JIT 做到这一点。据我所知,JIT 只是为了提高 JVM 的性能。

回答by Stephen C

The JIT is just part ofthe JVM. Other parts include the bytecode interpreter, the class loading verification and linking mechanisms, and the native code support for stuff like reflection, I/O and so on.

JIT 只是JVM 的一部分。其他部分包括字节码解释器、类加载验证和链接机制,以及对反射、I/O 等内容的本机代码支持。

In that sense, the JIT doesn't make the JVM run faster at all. Instead it makes Java coderun faster ... than it would if the JVM just interpreted it.

从这个意义上说,JIT 根本不会使 JVM 运行得更快。相反,它使Java 代码运行得更快……比如果 JVM 只是解释它的话。

In reality, the JVM doesstart out interpreting the bytecodes. After a period, the JVM then uses its JIT compiler to compile heavily used methods to native code, using statistics that were gathered while interpreting to tune the code for the problem at hand.

实际上,JVM确实开始解释字节码。一段时间后,JVM 使用其 JIT 编译器将大量使用的方法编译为本机代码,使用在解释时收集的统计数据来针对手头的问题调整代码。

By the way, this part of the text that you quoted is clumsy and technically inaccurate:

顺便说一句,你引用的这部分文字很笨拙,技术上也不准确:

A Just-In-Time (JIT) compiler is a feature of the run-time interpreter ... (context)

即时 (JIT) 编译器是运行时解释器的一项功能... (上下文)

In fact, the JIT is not a feature of the interpreter. Rather, the JIT is functionality of the JVM that works alongsidethe interpreter.

事实上,JIT 并不是解释器的一个特性。相反,JIT 是解释器一起工作的 JVM 功能。

回答by Abraham K

To be clear :

要清楚:

JVM performs everything like :

JVM 执行以下所有操作:

It stays on the top of the operating system and provides abstraction between the compiled java program and operating system. This way, java compiled program doesn't have to worry about which platform it has to work. Java program compiles code into bytecodes which JVM can understand and execute.

它位于操作系统的顶层,并提供编译后的 java 程序和操作系统之间的抽象。这样,java 编译的程序就不必担心它必须工作在哪个平台上。Java程序将代码编译成JVM可以理解和执行的字节码。

JIT

准时制

When JVM compiles the class file, it doesn't complete the full class file; it compiles only a part of it on need basis. This avoids heavy parsing of complete source code. This type of compilation is termed as JIT or Just-In-Time compilation. JVM is Platform(OS) dependent Code generation JIT is Platform Oriented, generates the native byte code, so it is faster one than JVM :)

JVM 编译类文件时,并没有完成完整的类文件;它仅根据需要编译其中的一部分。这避免了对完整源代码的大量解析。这种类型的编译称为 JIT 或 Just-In-Time 编译。JVM 依赖于平台(OS) 代码生成 JIT 是面向平台的,生成本机字节码,因此它比 JVM 快 :)

回答by Craig Trader

The Java Virtual Machine (JVM) provides the entire environment for running a Java program. It integrates with the Operating System, loads classes, and runs programs. The Just-In-Time compiler (JIT) is just a small piece that can be disabled (-Xint) but when enabled, provides a useful improvement in performance. There have been implementations of the JVM that didn't include a JIT, and implementations that worked by pre-compiling Java to machine code just the same as traditional languages, such as C or C++.

Java 虚拟机 (JVM) 提供运行 Java 程序的整个环境。它与操作系统集成、加载类并运行程序。即时编译器 (JIT) 只是可以禁用 (-Xint) 的一小部分,但启用后,可提供有用的性能改进。已经有不包含 JIT 的 JVM 实现,以及通过将 Java 预编译为与传统语言(例如 C 或 C++)相同的机器代码来工作的实现。

回答by niklas

It compiles it just in time for the JVM with optimizations.

它通过优化及时为 JVM 编译它。