preface

We know that the JVM roughly executes as follows:

Prerequisites: 0- JVM languages go through compilation time, from source files (.java,.kt) to intermediate language files (.class), as shown in Figure 1 below:

1-.class file, initialized by the classloading system

2- The initialized information is then passed to the runtime data section. This area specifies where different variables and parameters can be stored.

3- The runtime data area stores the bytecode to be executed, and the execution engine executes on a line basis. Garbage collection also comes into play here. Object garbage collection is performed on the heap of the runtime data area.

4- If Native methods are executed, methods in the Native Method Stack in the runtime data area are executed through the local interface and the local Method library.

Step 1 to Step 4, as shown in Figure 2 below:

In this section, we’ll focus on the RUNTIME data area of the JVM.

The role of Learning

Understanding how the JVM executes can help us understand how programs actually run.

define

The runtime data area is the area where data exists during the execution of JVM code.

It can be divided into two parts.

One part is consistent with the creation and destruction of the JVM, called the thread shared area.

The other part, consistent with the thread’s life cycle, is called the thread private area.

Question 1: The JVM lifecycle

Answer:

  1. Virtual machine startup A Java virtual machine is started by boostrap Class Loader to create an initial class that is specified by the implementation of the virtual machine. The first line in Figure 2.
  2. When a Virtual machine executes a Java program, it is actually executing a Java virtual machine process. The rest of Figure 2
  3. Exit the VM

There are several cases as follows:

  • The program exits normally
  • A program terminates abnormally when it encounters an exception or error during execution
  • The Java VIRTUAL machine process is terminated due to an operating system error
  • A thread calls the Exit method of the Runtime class or the System class, or the HALT method of the Runtime class, and the Java security manager also allows the exit or HALT operation
  • In addition, the JNI specification describes the Java Virtual machine exit when the Java Virtual machine is loaded or unloaded using the JNI Invocation API

Question 2: Thread life cycle

Answer: The life cycle of a thread consists of four states: created, ready, blocked, and dead. As shown in Figure 3 below:

Five specific areas

One, PC register (thread exclusive)

Since the JVM supports multithreaded programming, when the CPU pauses thread A to hand over the time slice to thread B, it needs an area to record the progress and current state of the current thread A.

This area is the PC register.

The PC register stores the address of the instruction pointing to the current execution of the bytecode if the method is not native, or undefined if the method is native.

Summary: THE PC register only records the instruction address (execution progress) of Java bytecode, not the execution record of ambiguous local methods.

2. Local method stack (thread exclusive)

The area where native methods are stored.

If the Java language wants to call other languages, it needs to use this area. JVM implementation uses C Stack (C language method Stack) to implement native methods.

Native methods: A native Method is a Java interface that calls non-Java code. The implementation of this method is implemented in non-Java languages, such as C.

JNI: Short for Java Native Interface, which ensures easy portability of code across platforms by writing programs using the Java Native Interface.

Add: JNI is not just a dynamic library that can call C/C++. Other languages are also acceptable, such as Go.

Note: JNI in Android is primarily a dynamic library for calling C/C++. Android has an official NDK that does this.

Added: JNI and JNA

JNI technology, not only can achieve Java access to C functions, but also can achieve C language to call Java code.

JNA can only implement Java access to C functions, as a Java framework, naturally can not implement C language to call Java code. At this point, you still need to use JNI technology. JNI is the foundation of JNA, the technical foundation for Java and C interoperability.

Ps: What is a dynamic library?

C/C++ program compilation process: preprocessing -> compilation -> Assembly -> link library: compiled binaries (note that following the above process, the above “compilation” is only the assembly file, and here the compilation refers to the complete translation process, that is, the high-level language into machine code the complete process. Corresponding to the above process, can be considered as the general name of the first three steps.)

The difference between static and dynamic libraries is the last step, the difference between links. Static library: in Link, it will directly copy. Lib (Windows) or. A (Linux and Mac) to the target program to form. Exe file. Advantages: You do not need to perform the first three steps. Disadvantages: The object executable file is larger. DLL (Windows),. So (Linux), and. Dylib (Mac) are not loaded directly into the executable file of the target program, but references to the parts that need to be called are kept. When the program is running, the object code is loaded from the dynamic library.

Note: This part is not mandatory and depends entirely on the implementation of the virtual machine.

Iii. Stack (Thread only)

A linear table in which stack frames are stored.

Stack: A linear collection of ports for deleting and inserting. 【LIFO: Last in, First out 】

Method calls and returns are implemented based on stack frames.

The stack frame

Definition: A stack frame is a data structure used to store data and partial process results. It is also used to handle dynamic linking, method return, exception dispatch, etc.

The stack frame life cycle is consistent with that of a method, being created when the method is called and destroyed when the method is terminated or when an exception occurs. Therefore, the implementation of the method follows the principle of “first in, last out”.

Composition:Each stack frame is defined byLocal variable table, operand stack, dynamic link composition.

  • Local variable scale

A list of local variables. A local variable can store data of a primitive data type or a reference to an object of a reference type (through which the object to which it refers is returned). Double and long need to occupy two local variables.

When the JVM wants to use local variables. For class methods, local variables are stored in a sequence of 0. For instance methods of a class, the 0 sequence is used to store the current object reference, and local variables are stored starting with the 1 sequence.

For double and long that need to occupy two local variables. If both types of values are stored in n and n+1 sequence positions, then the fetched sequence selects n.

The length of the local variable table is determined at compile time.

  • The operand stack

This is the stack within the stack frame. The full name is the initial operand stack of the current stack frame.

Execution stack frames are executed in operand stack order. The operand stack takes the local variable from the local variable table and executes it. The final method return value will also appear here.

Distinguish three concepts: stack, stack frame and operand stack

Stack: An area of the runtime data area that is thread exclusive and responsible for executing methods.

Stack frame: The concrete data structure of a method that resides on a stack.

Operand stack:

The operand stack is empty at creation time.

The virtual machine provides instructions for loading constants or variable values from the local variable table into the operand stack, and for fetching data from the operand stack.

The operand stack is used when a method is called to prepare the parameters of the calling method and to receive the return result of the method.

  • Dynamic link

Dynamic linking is used to complete runtime binding operations.

A method in a.class file that calls other methods or other member variables of the class needs to be represented by symbolic references.

Each stack frame has a reference to the current method in the runtime constant pool of the method area.

This reference is held for dynamic linking.

The Class file holds a large number of symbolic references, and the method invocation instructions in bytecode take symbolic references to methods in the constant pool as arguments. Some of these symbolic references are converted to direct references during class loading or the first time they are used, which is called static resolution. The other part is converted to a direct reference during each run, called the dynamic join.

Think back: the difference between dynamic and static libraries is very similar to the dynamic and static link here.

What is a symbolic reference?

You can think of it for now as a pointer in C.

4. Heap (Thread Sharing)

Used to store class instances and array objects, that is, object storage point.

When the virtual machine starts, the heap is created based on the parameters.

Heap objects are not explicitly reclaimed. It is handled by a garbage collection mechanism.

To accommodate the nature of the garbage collection mechanism, the heap is divided into young generation and old generation.

The younger generation can also be classified as Eden and Survivor(from/to)

It’s mainly to work with the garbage collection algorithm.

Method area and runtime constant pool (thread sharing)

In simple terms, this is where the metadata of the program code is stored.

Metadata includes runtime constant pool, field, and method data.

The bytecode content of constructors and ordinary functions.

Run-time constant pool

Definition: where constants exist in each class or interface in a.class file.

Include:

A numeric literal that is known at compile time.

The obtained method or field reference can only be resolved at runtime. (Guess: Note)

Afterword.

This is the beginning of the first article on the JVM, with the class loading mechanism. Draw a dead line and finish it this week. Come on, boy!