Interpreting bytecode vs compiling bytecode?
Asked Answered
S

3

8

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?

Streit answered 21/4, 2012 at 22:58 Comment(0)
A
14

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.

Armillia answered 21/4, 2012 at 23:1 Comment(3)
So what is the benefit of having the interpretation stage? So that the interpreted code which has been executed on the VM can then be optimised further and THEN compiled into native code?Streit
Code that isn't run very often doesn't really cost you much performance, and the time it takes to compile code might be greater than the time you'd save by using the compiled code. Interpreting code doesn't consume memory with newly compiled code, and bytecode is cross-platform portable while compiled code isn't.Armillia
@Loius What is a tight inner loop code? Can you clarify please?Blasien
F
3

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.

Florilegium answered 21/4, 2012 at 23:1 Comment(1)
how? how is it interpreted on JVM?Fortification
V
1

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.

Vanderbilt answered 21/4, 2012 at 23:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.