What are the components of the JVM
JVM (Java Virtual Machine) is mainly composed of the following core components:
1. Class Loader Subsystem
Responsibilities:Responsible for loading class files (.class files) into the JVM.
-
Main components:
-
Bootstrap Class Loader:
- Responsible for loading the Java core class library (
<JAVA_HOME>/jre/lib
in the directory, etc.). - It is implemented using native code (C/C++), not a Java class.
- It is the root of all class loaders.
- Responsible for loading the Java core class library (
-
Extension Class Loader:
- Responsible for loading Java extension library (
<JAVA_HOME>/jre/lib/ext
The jar package in the directory, ordirectory specified by system properties).
- yes
$ExtClassLoader
Examples of .
- Responsible for loading Java extension library (
-
Application Class Loader/System Class Loader:
- The class responsible for loading the application (classpath).
- yes
$AppClassLoader
Examples of . - Usually the default class loader.
-
Custom class loader (User-Defined Class Loader):
- Developers can customize class loaders and inherit
Class, implements special class loading logic (for example, loading classes from the network, loading classes from the database, encrypting and decrypting classes, etc.).
- Developers can customize class loaders and inherit
-
Bootstrap Class Loader:
-
Class loading mechanism:
-
Parent Delegation Model:
- When a class loader needs to load a class, it first delegates to its parent class loader to load.
- The subclass loader attempts to load only when the parent class loader fails to load the class (the class cannot be found within its search scope).
-
advantage:
- Avoid duplicate loading of classes.
- Ensure the security of the Java core class library (prevent user-defined classes from replacing core classes).
-
Loading process:
- Loading:Find and load the binary data (.class file) of the class.
-
Linking:
- Verification:Make sure that the loaded class files comply with the JVM specification and have no security issues.
- Preparation:Allocate memory for static variables of the class and set the default initial value (zero value).
- Resolution:Parses symbolic references in the class as direct references (optional, can be delayed until runtime).
- Initialization:Execute the initialization code of the class (static variable assignment, static code block).
-
Parent Delegation Model:
2. Runtime Data Areas
- Responsibilities:The memory area that the JVM manages when running a Java program.
-
Main areas:
-
Method Area:
- Stores data such as class information, constants, static variables, code compiled by the instant compiler that have been loaded by the virtual machine.
- All threads are shared.
- In HotSpot VMs, the method area is also called "non-Heap" or "permanent generation" (Permanent Generation, JDK 1.7 and before)/"metaspace" (Metaspace, JDK 1.8 and after).
- Runtime Constant Pool:Part of the method area, storing various literal and symbolic references generated during the compilation period.
-
Heap:
- Stores object instances and arrays.
- All threads are shared.
- It is the main area of garbage recycling.
- It can be divided into the Young Generation and the Old Generation.
- The new generation can also be divided into Eden area, Survivor from area and Survivor to area.
-
Virtual Machine Stack (VM Stack):
- Stores information such as local variable table, operand stack, dynamic link, method exit and other information of method calls.
- Each thread has its own virtual machine stack, and the stack size can be fixed or dynamically expanded.
- If the stack depth requested by the thread is greater than the allowed depth of the virtual machine, throw
*Error
。 - If the virtual machine stack can be dynamically expanded but cannot apply for sufficient memory, throw
OutOfMemoryError
。
-
Native Method Stack:
- Similar to the virtual machine stack, but is used to support the execution of native methods (methods written in C, C++, etc.).
- Each thread has its own local method stack.
-
Program Counter Register:
- Records the address (line number) of the bytecode instruction being executed by the current thread.
- Each thread has its own program counter.
- Is the only one that does not specify anything in the Java virtual machine specification
OutOfMemoryError
Area of the situation.
-
Method Area:
3. Execution Engine
- Responsibilities:Responsible for executing Java bytecode instructions.
-
Main components:
-
Interpreter:
- Explain bytecode instructions one by one.
- Fast startup, but slow execution.
-
Instant Compiler (JIT Compiler):
- Compile hotspot code (often executed code) into local machine code to improve execution efficiency.
- Compilation takes time, but compiled code executes fast.
-
JIT compiler in HotSpot VM:
- Client Compiler (C1):The optimization speed is fast, but the optimization degree is low.
- Server Compiler (C2):The optimization speed is slow, but the optimization degree is high.
- Tiered Compilation:Depending on the operation of the program, select different compilers for optimization (introduced in JDK 1.7).
-
Garbage Collector:
- Responsible for automatically recycling objects that are no longer used and freeing memory.
- Different JVM implementations have different garbage collectors.
-
Common garbage collectors:
- Serial GC:Single-threaded garbage collector.
- Parallel GC:Multithreaded garbage collector.
- CMS GC (Concurrent Mark Sweep):Concurrent tags remove garbage collectors.
- G1 GC (Garbage-First):A garbage collector for server-side applications.
- ZGC: A low-latency garbage collector.
- Shenandoah: A low-latency garbage collector.
-
Local method interface (JNI, Java Native Interface):
- Allows Java code to call native methods (methods written in C/C++, etc.).
-
Interpreter:
4. Native Interface
- Connect different programming languages to provide support for libraries written in Java using non-Java code.
JVM architecture diagram:
+-----------------------------------------------------------------------------------+ | JVM | +-----------------------------------------------------------------------------------+ | +---------------------+ +------------------------+ +---------------------+ | | | Class loader subsystem | | Runtime data area | | Execution Engine | | | | (Class Loader) | +------------------------+ +---------------------+ | | +---------------------+ | +------------------+ | | +---------------+ | | | | - Start the class loader | | | Method area (Method Area) | | | | Interpreter | | | | | - Extended class loader | | +------------------+ | | | (Interpreter) | | | | | - Application class loader | | | - Runtime constant pool | | | +---------------+ | | | | - Custom class loader | | +------------------+ | | +---------------+ | | | +---------------------+ | +------------------+ | | | Instant compiler | | | | | | | heap (Heap) | | | | (JIT Compiler)| | | | | | +------------------+ | | | - C1 (Client) | | | | | | | - New generation | | | | - C2 (Server) | | | | | | | - Eden | | | | - Layered Compilation | | | | | | | - Survivor | | | +---------------+ | | | | | | - Older age | | | +---------------+ | | | | | +------------------+ | | | Garbage collector | | | | | | +------------------+ | | | (GC) | | | | | | | Virtual Machine Stack (VM Stack) | | | +---------------+ | | | | | +------------------+ | | | | | | | +------------------+ | | +---------------+ | | | | | | Local method stack (Native) | | | |Local method interface | | | | | | +------------------+ | | | (JNI) | | | | | | +------------------+ | | +---------------+ | | | | | | Program Counter (PC) | | | | | | | | +------------------+ | | | | | | +------------------------+ | | | | | | | | +-----------------------------------------------------------------------------------+
Summarize
JVM is mainly composed of class loader subsystem, runtime data area, execution engine, local library interface, etc.
- The class loader is responsible for loading the class files.
- The runtime data area is responsible for managing memory.
- The execution engine is responsible for executing bytecode instructions and garbage collection.
- The local library interface is responsible for connecting to other languages.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.