Interpreting bytecode vs compiling bytecode?

丶灬走出姿态 提交于 2019-12-29 04:24:24

问题


I have come across a few references regarding the JVM/JIT activity where there appears to be a distinction made between compiling bytecode and interpreting bytecode. The particular comment stated bytecode is interpreted for the first 10000 runs and compiled thereafter.

What is the difference between "compiling" and "interpreting" bytecode?


回答1:


Interpreting byte code basically reads the bytecode line by line, doing no optimization or anything, and parsing it and executing it in realtime. This is notably inefficient for a number of reasons, including the issue that Java bytecode isn't designed to be interpreted quickly.

When a method is compiled, the JIT loads the whole method and generates native code to run directly on the CPU, rather than reading and interpreting the byte code line by line. After the method is compiled once, the generated native code gets used directly every time the method is called. This is astronomically faster, but incurs some overhead when the method gets compiled; among other things, the JVM is responsible for only compiling frequently called methods, to minimize the overhead while maximizing the performance of "tight inner loop" code that gets called extremely often.




回答2:


When the bytecode is interperted, it is executed through the JVM interpreter, not directly on the processor, when it is compiled, it is compiled to native machine language and executed directly on the CPU.




回答3:


The JVM has the Just In Time (JIT compiler); portions of the code that are repeated enough may be compiled into the native assembler code to speed it up.

Note that the change is done in the JVM only, the class (jar/war) files are not changed and remain as bytecode.



来源:https://stackoverflow.com/questions/10263782/interpreting-bytecode-vs-compiling-bytecode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!