什么是字节码以及JVM如何处理它们 [英] What are bytecodes and how does the JVM handle them

查看:295
本文介绍了什么是字节码以及JVM如何处理它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次听到Java实现JIT(即时)编译,并且其跨平台可移植的字节码由JVM解释。但是,我真的不知道字节码是什么,以及JVM在Java语言架构中的实际意义;我想了解更多关于它们的信息。

I heard many times that Java implemments JIT(just-in-time) compilation, and its bytecodes which are portable across platforms get "interpreted" by JVM. However, I don't really know what the bytecodes are, and what the JVM actually mean in Java language architecture; I would like to know more about them.

推荐答案

JVM(Java虚拟机)有一个像真机一样的指令集。给出该指令集的名称是Java Bytecode。它在 Java虚拟机规范中进行了描述。其他语言在执行前被转换为字节码,例如ruby和python。 Java的字节码处于相当低的水平,而python的级别要高得多。

The JVM (Java Virtual Machine) has an instruction set just like a real machine. The name given to this instruction set is Java Bytecode. It is described in the Java Virtual Machine Specification. Other languages are translated into a bytecode before execution, for example ruby and python. Java's bytecode is at a fairly low level while python's is much more high level.

解释和JIT编译是执行字节码的两种不同策略。解释一次处理一个字节码,对每个指令中编码的虚拟机状态进行更改。 JIT编译将字节码转换为执行等效操作的主机平台本机指令。

Interpretation and JIT compilation are two different strategies for executing bytecode. Interpretation processes bytecodes one at a time making the changes to the virtual machine state that are encoded in each instruction. JIT compilation translates the bytecode into instructions native to the host platform that carry out equivalent operations.

解释通常很快启动但在执行期间变慢,而JIT有更多的启动开销,但之后运行得更快。现代JVM使用解释和JIT技术的组合来获得两者的好处。首先解释字节码,而JIT在后台翻译它。 JIT编译完成后,JVM切换到使用该代码而不是解释器。有时,JIT编译可以产生比用于C和C ++的提前编译更好的结果,因为它更具动态性。 JVM可以跟踪代码的调用频率以及代码的典型路径,并在程序运行时使用此信息生成更高效的代码。 JVM可以像最初从解释器切换到JIT代码一样切换到这个新代码。

Interpretation is generally quick to start but slow during execution, while JIT has more startup overhead but runs quicker afterwards. Modern JVMs use a combination of interpretation and JIT techniques to get the benefit of both. The bytecode is first interpreted while the JIT is translating it in the background. Once the JIT compilation is complete, the JVM switches to using that code instead of the interpreter. Sometimes JIT compilation can produce better results than the ahead-of-time compilation used for C and C++ because it is more dynamic. The JVM can keep track of how often code is called and what the typical paths through the code are and use this information to generate more efficient code while the program is running. The JVM can switch to this new code just like when it initially switches from the interpreter to the JIT code.

就像有其他语言编译为本机代码一样C,C ++,Fortran;有其他语言的编译器输出JVM字节码。一个例子是scala语言。我相信groovy和jruby也可以转换为java字节码。

Just like there are other languages that compile to native code, like C, C++, Fortran; there are compilers for other languages that output JVM bytecode. One example is the scala language. I believe that groovy and jruby can also convert to java bytecode.

这篇关于什么是字节码以及JVM如何处理它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆