- Why do we learn about the JVM?
- By studying the JVM, can you better understand why your program works the way it does
- The ability to customize parameters and optimize the JVM for better performance of our programs in the JVM
- Let’s start with a rough flow chart
Let’s start by creating a few classes for testing
public class A {
public A(a) {
System.out.println("Init A class"); }}public class B extends A {
public static void main(String[] args) {
B b = new B();
C c = newC(); c.test(); }}public class C {
public void test(a) {
System.out.println("test method"); }}Copy the code
-
What is bytecode? We all know that Java is cross-platform, so why is it cross-platform? The answer is bytecode, which encapsulates the JVM on top of the operating system, using class bytecode, which decoubles the operating system.
-
How does the JVM use bytecode
The x.java file is first compiled into a. Class file, which the JVM can use to retrieve information from and execute the program, finding the main method by default.
-
The process by which the JVM loads bytecode
- Loading Loading is a process in Class loading, not Class loading. A fully qualified Class name is used to obtain a binary stream that is converted into a method area runtime data structure to generate a Java Class object that represents the Class and serves as the various data entry points for the method area
- The connection
- validation
Verify that the.class file complies with the JVM specification, or at least that it is recognized by the JVM
- To prepare
Allocate the required memory for a.class file, and the variables in its program are initialized toThe initial value
- parsing
Symbolic references are replaced with direct references
- Initialize the
If there is a parent class, the parent class is initialized first
Static block code is executed to assign values to variables in the program - use
- uninstall
-
The JVM class loading
- When to load (when the class is used)
- Class loader
- BootStrap ClassLoader
- Extension ClassLoader
- Applicaition ClassLoader
- Custom loader
- Parent delegation mechanism
- Function (in order to ensure that the core will not be changed, to ensure that the same file will not be loaded multiple times)
- It loads up when it loads
-
JVM runtime data area
- Method areas (shared by threads) are used to store information about classes, constants, and so on
- The program counter (thread private) is used to store the current execution position of the instruction. After switching directly, the thread should know where the last execution was.
- The virtual machine stack (thread private) stores local variables in threads, as well as virtual machine method calls, for virtual machines
- HotShot is a Native method used by virtual machines. HotShot combines the virtual machine stack with the Native method stack
- Heap memory The memory space opened by the object instance, the actual address of the object