What is the JVM
The JVM, short for Java Virtual Machine, is implemented by emulating various computer functions on an actual computer. It consists of a set of bytecode instructions, a set of registers, a stack, a garbage collection heap, and a storage method field. The JVM shields operating system platform-specific information, allowing Java programs to run unmodified on multiple platforms only by generating object code (bytecode) that runs on the Java VIRTUAL machine, which is why Java can “compile once, run anywhere.”
Liverpoolfc.tv: docs.oracle.com/javase/spec…
As you can see, both the JDK and the JRE depend on the JVM at the very bottom. If we don’t understand what the JVM is, we can explain it in a different way.
The lifetime of a.Java source file:
As is shown in
-
Source code to class files
- Javac is the Java compiler that translates.java(high-level language) into.class(binary) files
-
Class files to the JVM
- Class loading mechanism
- Class loader
-
All sorts of JVM hassles (internal structure, execution, garbage collection, local calls, etc.)
- JVM runtime data area
- JVM memory model
- JVM garbage Collection
- .
In general, the JDK includes a JVM, which varies from JDK to JDK. The JVM blocks different instruction sets for different operating systems, enabling the Java language’s “Write once run Anywhere.”
Source code to class files
This process will be analyzed by a demo, which I’ll start with
demo
class Person{
private String name="Jack";
private int age;
private final double salary=100;
private static String address;
private final static String hobby="Programming";
private static Object obj=new Object();
public void say(a){
System.out.println("person say...");
}
public static int calc(int op1,int op2){
op1=3; int result=op1+op2; Object obj=new Object(); return result;
}
public static void main(String[] args){ calc(1.2); }}Copy the code
Compile this Person.java file.
Person. Java -> Lexical Parser -> Tokens Stream -> Parser -> Syntax Tree/Abstract Syntax Tree -> Semantic Parser -> Annotation Abstract Syntax Tree -> Bytecode Generator -> Person.class file
Get the Person.class file
cafe babe 0000 0034 003f 0a00 0a00 2b
002c 0900 0d00 2d06 4059 0000 0000 0000
0900 0d00 2e09 002f 0030 0800 310a 0032
0033 0700 340a 000d 0035 0900 0d00 3607
0037 0100 046e 616d 6501 0012 4c6a 6176
612f 6c61 6e67 2f53 7472 696e 673b 0100
0361 6765 0100 0149 0100 0673 616c 6172
7901 0001 4401 000d 436f 6e73 7461 6e
......
Copy the code
Class file interpretation (.class interpretation)
Reference: docs.oracle.com/javase/spec… Chapter 4 of the JVM documentation explains how to interpret class files.
1. Structure of class files
The structure of the class file provided by the official website :(u2 represents the 4-digit hexadecimal number of the demo class file above, and u4 represents 8 as a hexadecimal number)
1.1 magic
the
magic
item supplies the magic number identifying theclass
file format; it has the value0xCAFEBABE
.
In the demo u4: cafebabe
The beginning 0xCAFEBABE is used to indicate that the file is a. Class file
1.2 minor_version, major_version
Represents the JDK version information
U2 + U2:0000 + 0034 (34 = 52 in base 10, indicating JDK8)
1.3 constant_pool_count
Represents the total number of constants in a constant pool
The value of the constant_pool_count item is equal to the number of entries in the constant_pool table plus one.
U2:003f =63(base 10)
1.4 constant_pool
The
constant_pool
is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within theClassFile
structure and its substructures. The format of eachconstant_pool
table entry is indicated by its first “tag” byte.The constant_pool table is indexed from 1 to constant_pool_count-1.
The constant pool stores two main things: literals and Symbolic References.
-
Literals: text strings, final modifiers, etc
-
Symbolic references: fully qualified names of classes and interfaces, field names and descriptors, method names and descriptors
In Section 4.4, The Constant Pool is given, and The format for each Constant is 👇
cp_info {
u1 tag; //u1 represents two hexadecimal numbers -- a Constant Type identifier,
u1 info[]; // Represents the contents of a Constant
}
Copy the code
The structure of each Constant Type can be found in section 4.4 of the official website. Corresponding to the previous Constant number, you can read all the contents of the ConstantPool.
In the JDK, javap can parse class files for us. We don’t need to analyze class files ourselves. For details, see section 4.4 of the official documentation.
Class files to the JVM
Class loading process (.class– >JVM)
Loading a class simply means reading binary data from a class’s.class file into memory, placing it in the method area of the runtime data area, validating, converting, and initializing the data, and then creating a java.lang. class object in the heap that encapsulates the class’s data structure in the method area.
The process of class loading is divided into three stages: loading, linking and initialization. Linking can be divided into three parts: verification, preparation and parsing
loading
To find and load the binary data of the class, the virtual machine needs to do three things during the load phase:
-
To get the binary byte stream of the class, find the class file by its fully qualified class name
-
Transform the static storage structure represented by this byte stream into the runtime data structure of the method area.
-
A Java.lang. Class object representing this Class is generated in the Java heap as an access point to the data in the method area.
After the load phase is complete, the binary byte streams outside the virtual machine are stored in the method area in the format required by the virtual machine, and an object of java.lang.Class Class is created in the Java heap, through which the data in the method area can be accessed.
link
1. Verify
Validation is the first step in the connection phase, which ensures 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 itself. In the verification stage, four stages of inspection actions will be roughly completed:
- File format validation: verifies whether the byte stream conforms to the Class file format specification; For example, whether to use
0xCAFEBABE
Start, major and minor versions are within the scope of the current VIRTUAL machine, and constants in the constant pool are of unsupported types. - Metadata validation: Perform semantic analysis on the information described by bytecode (note: compare the semantic analysis at the compilation stage of JavAC) to ensure that the information described conforms to the requirements of Java language specifications; For example: does this class have a parent, except
java.lang.Object
Outside. - Bytecode validation: Determine that program semantics are legitimate and logical through data flow and control flow analysis.
- Symbol reference validation: Ensures that the parse action is performed correctly
2. Prepare
Allocates memory for static variables of the class and initializes them to default values (excluding instance variables, which are allocated in the Java heap along with the object when it is instantiated)
For example, suppose a class variable is defined as: public static int a = 10;
The initial value of a after the preparation phase is 0, not 10, and the assignment of a to 10 will be performed during the initialization phase.
3. The parsing
Convert symbolic references to direct references
Symbolic references: Just symbols that the JVM can recognize, have no real meaning, and do not occupy addresses in the physical machine
Direct reference: A pointer directly to a target, a relative offset, or a handle that is indirectly located to the target to be dropped into real physical memory
Initialize the
Assign the actual value to the previously prepared static variable
Class initialization timing: Class initialization occurs only when a class is actively used. Active use of a class includes the following six types:
- Create an instance of the class, which is new
- Accesses or assigns a value to a static variable of a class or interface
- Call a static method of a class
- Reflection (e.g.,
Class.forName()
) - If you initialize a subclass of a class, its parent class is initialized as well
- Java virtual machine startup class is marked as the startup class, directly used
java.exe
Command to run a main class
ClassLoader ClassLoader
There are many types of classloaders: each with its own job can be divided into four categories 👇
JVM class loading mechanism
-
Full responsibility. When a Class loader is responsible for loading a Class, other classes that that Class depends on and references are also loaded by the Class loader, unless it is shown to be loaded using another Class loader.
-
Parent delegate (parent delegate), which first lets the parent loader try to load the class and only tries to load the class from its own classpath if the parent loader is unable to load the class. For example, our classpath directory also defines a java.lang.String class, which would normally be loaded by the App ClassLoader. But in fact, to prevent classes in the Java core library from being corrupted, he tries to load the parent class first, and then the parent class loads the parent class, and if the parent doesn’t load successfully, he returns to load himself. Also known as the parent delegate mechanism.
- The caching mechanism will ensure that all loaded classes will be cached. When a program needs to use a Class, the Class loader first looks for the Class in the cache. Only when the cache does not exist, the system will read the binary data corresponding to the Class, convert it into a Class object, and store it in the cache. Is that why you have to restart the JVM to make Class changes to take effect
JVM series:
JVM garbage collection
This article thoroughly understand the JVM runtime data area and JVM memory structure
[JVM series]一, source -> class file ->JVM process detail (class file interpretation/class loading mechanism/class loader)