JVM’s Processing Architecture

Tram Ho

JAVA has always prided itself on the “Write Once Run Anywhere” slogan, to get it all thanks to the JVM, a JAVA virtual machine. So how is it really? Most of us know byecode is executed by JRE (JAVA execution environment) but in fact JRE is an implementation of JVM, in this article we will take a deep look at JVM implementation, look forward to receiving it. Get advice from friends, brothers and sisters.

Concept

Extending from the concept of a virtual machine, a virtual machine is an implementation of a physical machine. The compiler does the compilation of JAVA files into JAVA.class files, and the JVM is a virtual machine that specializes in receiving and executing those files. So when we code, it’s actually the code for the JVM to understand, not the code for the physical server to understand, so with any operating system we just need to install the JRE, which means there will be a regular JVM. can run.

Process diagram

alt

JVM has 3 main processing steps

  • Class Loader
  • Data Runtime Area
  • Execution Engine

Class Loader

Classloader – responsible for loading, linking, and initializing files at the first run of the Runtime.

  • Loading : It is the step to initialize and load files into the JVM virtual machine
  • Linking: Verify bytecode is valid or not? Then assign default values ​​to the static variable from the file loaded in the loading step,
  • Initialization: This is the last step of the class loader, all static variables will be assigned initial values, and the static block will be executed.

Runtime Data Area

This is the main dish that makes up the largest part of the JVM architecture. It is the part of memory that the JVM uses that is allocated by the operating system.
There are 5 main areas of Runtime Data:

  • Method Area: All Class-Level data, including static variables, are stored here.
    And a separate Runtime constant pool is located in the method area: Contains numeric constants, field references, method references, and attributes.
  • Heap Area: Store data of variables, Object, corresponding array, String Pool…
    Garbage Collection is the memory manager of the Heap, when there is no longer a User Thread using a reference to Object or Array, it will be responsible for cleaning up the memory.
    Default the largest size of the heap is 64 Mb, we can configure more
  • Native method stack: Contains bytecode of languages ​​other than JAVA, which has been referenced by JNI (java native interface) via native lib so that the bytecode and machine can understand and execute.
  • PC Reigster: The PC (Program Counter) register is created when initializing a thread and for that thread alone.
  • JVM Stack: Like PC Reigster, created when creating a thread and for that thread. The JVM Stack is the stack for the JVM to store the Stack Frame.
    Stack Frame is initialized every time there is a method called, the stack frame is carrying the information of that method, it includes local variables, local arrays…

Execution Engine

It assigns the byteCode to the Runtime data area, reads the byteCode and executes it one by one.

  • Interpreter – Interpreter interprets byteCode fast, but executes it slowly. The disadvantage of interpreters is that when a method is called multiple times, a new interpreter is needed each time. The interpreter will directly execute the bytecode without converting it to machine code
  • JIT – Jit is an intermediary between the Interpreter and the Compiler. During Runtime it converts byteCode to machine code (JVM or Physical Machine) Next time it gets from cache and runs, limiting the slow execution of Interpreter.
  • Garbage Collection: Collects and discards objects that are no longer referenced by User Threads
  • JNI: interacts with Native Method Libs and provides the necessary Native Libraries.
Share the news now

Source : Viblo