Usage of jvm instance in Java bytecode

  • 2021-08-21 20:43:28
  • OfStack

To make Java run, we can design a virtual machine for Java language features, and convert Java programs into instruction sequences that it can recognize, also known as Java bytecode, through the compiler. Since the opcode of the Java ByteCode instruction is fixed at 1 byte, its name is thus named. What this article will bring is the use of jvm in Java bytecode, which is mainly divided into two operational perspectives. Let's take a look at the specific content.

1. Virtual machine perspective

When executing the Java code, you first need to load the compiled class file into the Java virtual machine. The loaded Java class will be stored in the method area (MethodArea). The virtual opportunity executes the code in the method area at actual runtime. JavaVirtualMachines splits the heap and stack into memory to store runtime data. JavaVirtualProfessional subdivides the stack into Java method stack for Java method, local method stack for local method (native method written in C + +), and PC register for storing the execution location of each thread.

The call goes into the Java method, and the Java virtual machine generates a stack frame in the Java method stack of the current thread for storing local variables and bytecode operands. The size of the stack frame is calculated in advance, and the Java virtual machine does not need to continuously distribute the stack frame in the memory space. When the Java virtual machine exits the currently executing method, whether normal or abnormal, the current stack frame of the current thread will pop up and discard the frame.

2. Hardware perspective

The Java bytecode cannot be executed directly, so the Java virtual machine needs to translate the bytecode into machine code. In HotSpot, the translation process has two forms

1. It is to explain execution, and translate bytecode into machine code for execution

2. It is just-in-time compilation (Just-In-Timecompilation, JIT), which compiles all bytecodes contained in one method into machine code and executes it.

The advantage of the former is that there is no need to wait for compilation, while the advantage of the latter is that it actually runs faster.

Expansion of knowledge points:

As an Java developer, the pursuit of technology does not stop at being able to use API and write basic functions. If you want to have higher attainments in technology, you need to go deep into the principle level to understand the mechanism of code operation. Therefore, starting with the structure of class bytecode file, this paper dissects the internal working principle of binary bytecode step by step, which is of great benefit to deeply understand the operating mechanism of JVM, and at the same time, it is also very helpful for the work that wants to use BCEL to dynamically change Class bytecode instructions (example: JVM Class bytecode 3-using BCEL to change class attributes).

What is an Class file

The Java ByteCode class file (. class) is the "object file" produced by the Java compiler compiling the Java source file (. java). It is an 8-bit byte binary stream file, each data item is closely arranged from front to back in sequence, and there is no gap between adjacent items, which can make class file very compact and lightweight, and can be quickly loaded into memory by JVM, and occupy less memory space (convenient for network transmission).

After the Java source file is compiled by the Java compiler, each class (or interface) occupies an class file separately, and all the information in the class will be described in the class file accordingly. Because the class file is very flexible, it even has stronger description ability than the Java source file.

The information in class file is arranged one by one, and each data item has its fixed length, some occupying one byte, some occupying two bytes, and others occupying four bytes or eight bytes. The different lengths of data items are represented by u1, u2, u4 and u8 respectively, which means that one data item occupies one byte, two bytes, four bytes and eight bytes in class file respectively. u1, u2, u3 and u4 can be regarded as "types" of data items in class files.


Related articles: