Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the JIT compiled code reside?

Tags:

java

jvm

jit

So I have this method, written in Java:

public void myMethod(int y){
    int x = 5 + y;
    doSomething(x);
}

And assume my application calls this a lot of times..

When running the compiled code for this method on Java Virtual Machine, JVM will first interpret the method. Then after some time it will decide to compile it to machine language if I understand correctly.

At this point,

Will it be overwritten by the machine code in the memory? If it is overwritten, how will the issue of the size difference solved? If it is written to some other place in memory, will the bytecode loaded into memory freed or not? And also, if both bytecode and the jit compiled code is in the memory, when the application hits this method again, how does JVM decide to execute the jit compiled code instead of byte code?

like image 593
Koray Tugay Avatar asked Jan 24 '15 13:01

Koray Tugay


People also ask

Is JIT inside JVM?

JIT is one of the components of JVM. JVM compiles complete byte code to machine code. JIT compiles only the reusable byte code to machine code. JVM provides platform independence.

What does JIT do in compilation?

The JIT compiler helps improve the performance of Java programs by compiling bytecodes into native machine code at run time. The JIT compiler is enabled by default. When a method has been compiled, the JVM calls the compiled code of that method directly instead of interpreting it.

What is a JIT compiled language?

A just-in-time (JIT) compiler is a compiler that compiles code during program execution, rather than ahead of time. Many traditional compilers compiled code, transitioning between code input and machine language, well before runtime. A JIT compiler is a way to compile in real time or on the fly as the program executes.

Where does JVM resides?

The JVM resides on the RAM. During execution, using the Class Loader subsystem, the class files are brought on to the RAM. This is called Java's dynamic class loading functionality. It loads, links, and initializes the class file (.


1 Answers

HotSpot JVM has a Method structure in Metaspace (or PermGen in earlier versions). It contains method bytecode which is never overwritten and a pointer to compiled code, initially NULL until the method is compiled.

A method may have multiple entry points:

  • _i2i_entry - a pointer to the bytecode interpreter.
  • _code->entry_point() - an entry point to the JIT-compiled code. Compiled methods reside in CodeCache - the special region of native memory for the VM dynamically generated code.
  • i2c and c2i adapters to call the compiled code from interpreter and vice versa. These adapters are needed, because the interpreted methods and compiled methods have different calling convention (the way how arguments are passed, how frames are constructed etc.)

A compiled method can have uncommon traps that fall back to interpreter in some rare cases. Furthermore, a Java method can be dynamically recompiled multiple times, so JVM cannot throw away the original bytecode. There is no sense to free it anyway, because the bytecode is usually much smaller than the compiled code.

like image 112
apangin Avatar answered Sep 21 '22 15:09

apangin