In depth parsing of the Java virtual machine

  • 2020-04-01 02:07:40
  • OfStack

What is a Java virtual machine

"Java virtual machine" may refer to three things:
1) abstract specification;
2) a specific implementation;
3) a running virtual machine instance;

The Java virtual machine life cycle

Start the
When a Java program is started, a virtual machine instance is born.
A virtual machine instance runs a Java program by calling the public static void main(String[] args) method of an initial class.
Any class with such a main method can be the starting point for a Java program to run, so you must tell the virtual machine the name of the original class, and the entire program will run from its main method.

die
The main method of the initial class is used as the starting point for the program's initial thread, and any other thread is started by the initial thread.
There are two types of threads inside the Java virtual machine: daemon and non-daemon threads.
Daemon threads, usually used by virtual machines, such as garbage collection. A Java program can also mark the created thread as a daemon thread.
Non-daemon threads, the initial threads in Java, are non-daemon threads that start with the main method.
As long as there are non-daemon threads running in the program, the Java program continues to run (the virtual machine is still alive), and the virtual machine instance exits automatically when all the non-daemon threads in the program terminate.
The program itself can also exit through the Runtime class or the System class's exit() method.

Java virtual machine architecture

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201308/201308160929326.jpg ">

Classloading subsystem
1) responsible for finding and loading types, including loading, connection and initialization (link: http://jb51.net/article/40628.htm) Detailed in;
2) there are two types of loaders: bootstrap classloader and user-defined classloader;
The user-defined ClassLoader must be derived from the java.lang.ClassLoader class.

Run time data area

Methods area
The information of the loaded type is stored in the method area, as follows:
The basic information
1) fully qualified name of the type;
2) the fully qualified name of the direct superclass of type;
3) whether the type is a class type or an interface type;
4) access modifier of type;
5) the fully qualified name of the direct superinterface;

Other information
1) constant pool of type;
2) field information;
3) method information;
4) all static variables except constants;
5) a reference to the ClassLoader class;
6) a reference to the Class Class;

The heap
1) all class instances and arrays created at runtime are placed in the heap;
2) there is only one heap space in a Java virtual machine instance, so all threads in the virtual machine share the heap;
3) a Java program monopolizes one instance of the Java virtual machine, so each Java program has its own heap space;

Java stack

1) each time a new thread is started, the Java virtual machine allocates a Java stack for it;
2) the Java stack saves the running state of the thread in frame;
3) the Java virtual machine only performs two operations on the Java stack: push and push;
4) stack: every time a thread invokes a Java method, the virtual machine presses a new frame into the thread's Java stack. Use this frame to store parameters, local variables, intermediate results, etc.
5) out of stack: when a Java method returns normally through return or throws an exception to abort, the virtual machine will pop the current frame out of the Java stack and then release it;
6) the stack data is private to the thread, so there is no need to consider the access synchronization of the stack data in the case of multi-threading;

The stack frame
1) stack frame is composed of three parts: local variable area, operand area and stack data area;

PC register
1) for running Java programs, each thread has its own PC register;
2) create when the thread starts;
3) when a thread executes a Java method, the contents of the PC register are always the address of the next instruction to be executed.

Local method stack
1) any local method interface will use the local method stack;

Execution engine
1) the execution engine is the core of the implementation of the Java virtual machine;
2) "execution engine" can be understood in three ways:
A) an abstract specification;
          B) a specific implementation;
          C) a running instance;
3) the behavior of the execution engine is defined by the instruction set. For each instruction, the specification specifies what to do when the instruction is executed, if not what to do;
To be refined...


Related articles: