preface

As a Java programmer, we need to know how Java code works. Recently reviewed the deep understanding of Java virtual machine this book, made a note, I hope to help you, if there is an incorrect place, welcome to put forward, thank you very much.

The Java code runs the main process

This paper mainly explains the process as follows:

  • Java source files compile to class bytecode
  • The class loader loads the bytecode into the virtual machine’s method area.
  • The runtime creates the object
  • Method calls, which the execution engine interprets as machine code
  • CPU execution instruction
  • Multithreaded switching context

compile

As we all know, Java code runs on a Java virtual machine. But Java is an object-oriented high-level language, its syntax is very complex, the degree of abstraction is also very high, and can not run directly on the computer hardware machine.

The Java Virtual Machine (JVM for short) is an abstract computer that runs all Java programs and is the runtime environment of the Java language.

Therefore, before running a Java program, the compiler needs to compile the code into an instruction program that the Java virtual machine can recognize. This is called Java bytecode, or class file.

So the first step in Java code execution is to compile the Java source code into.class bytecode files.

Class loading

The information described in the Class file needs to be loaded into the virtual machine before it can be run and used. Therefore, class bytecode files need to be loaded into the Java virtual machine.

The virtual machine loads the data describing the Class from the Class file to the memory, verifies, transforms, and initializes the data, and finally forms Java types that can be directly used by virtual machines. This is the Class loading mechanism of virtual machines.

loading

During the load phase, the virtual machine needs to do the following three things:

  • Gets the binary byte stream that defines a class by its fully qualified name.
  • Transform the static storage structure represented by this byte stream into the runtime data structure of the method area.
  • Generate a java.lang.Class object in memory that represents the Class and acts as an access point for the Class’s various data in the method area

After the load phase is complete, the binary byte streams are stored in the method area in the format required by the virtual machine.

validation

To ensure that the byte stream in the Class file meets the requirements of the current VIRTUAL machine and does not compromise the security of the virtual machine, the Java virtual machine validates the input byte stream.

The verification stage includes four stages: file format verification, metadata verification, bytecode verification and symbol reference verification.

  • File format verification: Verifies that the byte stream complies with the Class file format specification, for example, starts with a magic number 0xCAFEBABE.
  • Metadata validation: Semantic analysis of information described by bytecode, such as whether the parent of this class inherits classes that are not allowed to be inherited (classes modified by final);
  • Bytecode validation: The primary purpose is to determine that program semantics are legitimate and logical through data flow and control flow analysis. For example, ensure that jump instructions do not jump to bytecode instructions outside the method body.
  • Symbolic reference validation: occurs when the virtual machine converts a symbolic reference to a direct reference, for example, to verify that a class can be found for a fully qualified name described in a string in a symbolic reference.

To prepare

The preparation phase is to formally allocate memory for class variables and set their initial values. The memory used by these variables will be allocated in the method area. Such as:

Public static int value =123;Copy the code

The initial value of the variable value after the preparation phase is 0 instead of 123.

parsing

The parsing phase is the process by which the virtual machine replaces symbolic references in the constant pool with direct references.

For example, the com.user class references com.tool. At compile time, the User class does not know the actual memory address of Tool, so it can only be represented by the symbol com.tool (assumed). When the User class is loaded, the virtual machine can obtain the actual memory address of the Tool class. Therefore, the symbol com.tool can be replaced with the actual memory address of the Tool class, that is, directly reference the address.

The parse action is mainly for class or interface, field, class method, interface method, method type, method handle, and call point qualifier 7 class symbol references.

Initialize the

During the initialization phase, you actually start executing the Java bytecode defined in the class. At this stage, class variables and other resources are initialized according to a subjective plan made by the programmer through the program.

Create an object

How does a Java virtual machine execute bytecode? Let’s take a look at creating objects at run time.

Java is an object-oriented programming language, the program is run in object as the call unit.

  • After the bytecode file is loaded into the method area of the VM, object information corresponding to the bytecode file is created through the class bytecode file during the program running.
  • Object creation methods include: new keyword, reflection, etc.
  • Java heap memory is the area shared by threads where information about objects created is stored.

The method call

JVM calls are made in units of objects, but the actual functional code is executed in methods on objects.

At run time, each time the call enters a Java method, the Java virtual machine generates a stack frame in the Java method stack of the current thread to hold local variables and bytecode operands. Method stack memory is thread private, and each thread has its own method stack. If the corresponding method is a local method, the corresponding is the local method stack.

The Java runtime data area is as follows:

explain

When a method on a Java object is called, the JVM execution engine translates the method’s bytecode file into machine code that the computer recognizes, and the machine code information is stored in the method area. Translation has two methods: interpretation execution and real-time compilation.

Explain to perform

Take a line of code, explain a line of code, and that’s the way most of the not-so-common code works.

Even if the compilation

For partial hot code, all the bytecode contained in a method is translated into machine instructions to improve the running efficiency of the Java virtual machine.

Just-in-time compilation is based on the classic 80/20 rule that 20% of code takes up 80% of computing resources.

An instruction to

  • After the Java program is loaded into memory, the instructions are also in memory.
  • The instruction register IP of the instruction points to the address of the next instruction to be executed.
  • The CPU’s control unit loads the instructions in main memory into the instruction registers according to the IP registers. These loaded instructions are a string of binary codes, which need to be decoded by the decoder.
  • After decoding, if operands need to be obtained, the data is fetched from memory and the operation unit is called for calculation.

Multithreaded context switch

Once the CPU is powered on, it will cycle from the memory to obtain instructions, decoding, execution.

  • To support multitasking, the CPU divides the execution time resource into time slices, with each program executing for a period of time.
  • Multithreading in the Java VIRTUAL machine is implemented by the way that threads alternate the allocation of processing execution time. At any given time, one processor (or kernel for multi-core processors) will execute only one instruction in the program.
  • Assume that the current thread is running, the distribution of CPU time finished execution, must keep running through the information, or a waste of previous work, therefore, the program counter (PC) register, is out, it is a small piece of memory space, thread private, can be viewed as the number of rows in the current threads execute bytecode indicator. When the CPU assigns it a time run, it can restore the data and continue execution at the last place.

Reference and thanks

  • In-depth Understanding of the Java Virtual Machine
  • “How to Understand how a Line of Java Code works with an Operating System”, by Peng Dong
  • How does Java code run

Personal public account

  • If you think it’s good, give me a thumbs up and attention. Thank you
  • If there is any incorrect place, please kindly point out, thank you very much.
  • At the same time, I am looking forward to friends can pay attention to my public account, behind slowly launch better dry goods ~ hee hee