This article explains the JVM architecture in detail
JVM is an abstract computer based on the stackarchitecture. It has its own instruction set and memory management, and is a Java cross-platform Based on this, the JVM interprets and executes the bytecode, or compiles the bytecode into local code for execution. The Java virtual machine architecture is as follows:
Class File is a platform-independent binary file , contains bytecode that can be executed by the JVM, in which multi-bytes are in big-endian order and characters use an improved UTF-8 encoding. The Class file accurately describes the information of a class or interface, including:
ConstantPool: numerical value and string Literal constants, metadata such as class name, method name, parameters, and various symbols Reference to the bytecode instructions of the
method, number of parameters , local variables, maximum operand stack depth, exception and other information
Class loader, JVM is dynamic when the class is used for the first time loading, linking and initialization. The JVM's default loading model is a parent delegation model. There is a hierarchical structure of parent-child relationships between class loaders, and is implemented internally using combinations. In addition, there are other loading methods, such as Servlet loading. It first tries to load by itself, and then delegates to the upper loader if it fails. Class isolation; there is a network dependency between OSGI loaders, and there is no The distinction between upper and lower levels is relatively flexible.
Loading is to take the class or interface represented by the Class file and create a corresponding java.lang.Class object in the JVM method area , like Class.forName(), ClassLoader.loadClass(), and reflection can trigger class loading. When a class loading is triggered, the detailed process is as follows:
Check whether the class has been loaded
Delegate the loading request to the upper class loading
Try by yourselfSearch for the class and load it
When ClassLoader does not find the class file in the classpath, it will throw ClassNotFoundException; When class A references class B, class A has been successfully loaded, but the class file is not found when loading B, NoClassDefFoundError will be thrown. JVM has the following class loaders:
Bootstrap ClassLoader starts the class loader and loads the Java core in
Extension ClassLoader, extension class loader, loads classes in
System ClassLoader, system class loader, also called ApplicationApplication class loader (Application class loader), loads classes in the CLASSPATH environment variable
Verification: Ensure the correctness of the class file.
Preparation: Allocate memory for class static fields and initialize them to default values, no bytecode instructions will be executed.
Analysis: Convert symbol reference to method area (runtime constant pool) direct reference
Execution class The initialization method, that is, assigning static fields and executing static blocks, is in the order of its definition. The static fields of the parent class will be initialized before the static fields of the subclass.
At this point, a class or interface is loaded into memory, and the JVM will ensure that the entire process is thread safe. It should be noted that the entire process does not involve any instance objects.
Method Area: Thread sharing, which stores the runtime constant pool, class field and method information, static variables and method bytecode, is the logic of the heap component, garbage collection for this part is optional. It is worth mentioning that Hotspot JVM has adjusted the contents of this part of memory since JDK8. The allocation of class meta-data uses local memory, and interned String and class static variables have been moved to the Java heap.
Runtime constant pool: It plays a core role for the JVM. Basically, when it comes to methods or fields, the JVM will search for its specific memory address in the runtime constant pool.
Heap: Thread sharing, storing instance objects, instance variables and arrays, is the main area for garbage collection.
JVM Stack: Thread private, used to store stack frames. When the method is called, a stack frame will be created and pushed onto the stack. The stack frame is grouped by the following Into:
#Local variable table: starts from 0 to store this, method parameters, and local variables.
Operand stack: The work area of the method, data is exchanged between the operand stack and local variables, and intermediate results are stored. The depth of the operand stack can be determined at compile time.
Frame data: method return value, exception dispatch, and reference to the runtime constant pool of the class where the current method is located.
PC Register: Thread private, saves the current instruction address, and points to the next instruction address after execution.
Native Method Stack: Thread private, stores local method information, C or C++ stack.
Read, translate, and execute bytecode. The JVM is based on a stack architecture. This stack is the operand stack, and bytecode instructions perform various operations through it. There are also register-based virtual machines.
Interpreter, translation: Interpreting bytecode is faster and slower in execution. The disadvantage is that each method call must be re-translated and explained.
JIT Compiler, just-in-time compilation: Find out frequently called hotspot methods in the program, compile the bytecode into local code, and improve performance.
Garbage Collector: Recycles invalid objects, determines whether the object is recyclable, and can use different garbage collection algorithms.
JNI, calls local methods, c/c++ library; local method library required by the execution engine.
The mainstream JVM implementations include Oracle's Hotspot JVM, JRockit and IBM's JVM. When it comes to JVM tuning, it refers to Hotspot VM by default, which shows its popularity. Nowadays, it is a bit low to engage in Java without understanding the JVM -v-.
If you want to write high-quality code, you must not only understand the JVM, but also need complete basic computer knowledge such as tuning and troubleshooting. In fact, no matter what language you use for development, it is a process of building and improving your own computer knowledge system. .
The above is the detailed content of Detailed explanation of JVM architecture with pictures and texts. For more information, please follow other related articles on the PHP Chinese website!