JVM architecture and jvm Architecture
JVM is an abstract computer. Based on the stack architecture, it has its own instruction set and memory management. It is the basis for Java cross-platform implementation. JVM interprets and executes bytecode, or compile the bytecode for code execution. The Java virtual machine architecture is as follows:
Class File
Class File is a platform-independent binary File that contains the bytecode that can be executed by JVM. The multibyte adopts the large-Endian order, and the character uses an improved UTF-8 encoding. The Class file accurately describes the information of a Class or interface, including:
- Constant pool: numeric and string literal constants, metadata such as class name, method name, parameter, and various symbol references
- Method bytecode instructions, number of parameters, local variables, maximum operand stack depth, exceptions, and other information
Class Loader
Class Loader. When JVM is used for the first timeDynamic. The default JVM load model is a parent-parent delegation model. The class loaders have parent-child relationship hierarchies and are implemented using combinations internally. In addition, there are other loading methods, such as Servlet loading. It first tries to load it by itself, but fails to delegate the upper-layer loader to isolate classes. OSGI loaders are a kind of mesh dependency between them, there is no distinction between the upper and lower layers, which is more flexible.
Load
Loading is to create a corresponding java in the JVM method area for the Class file Class or interface. lang. class object, such as Class. forName (), ClassLoader. loadClass () and reflection can trigger class loading. When a class load is triggered, the detailed process is as follows:
- Check whether the class has been loaded
- Delegate the load request to the upper class loader
- Search for classes and load them by yourself
When the ClassLoader does not find A class file in classpath, ClassNotFoundException will be thrown. When Class A references Class B, Class A has been successfully loaded, but class file is not found when class B is loaded, it will throw NoClassDefFoundError. JVM has the following types of loaders:
- Bootstrap ClassLoader: Start the class loader and load the Java core class library in <java_home> \ jre \ lib
- Extension ClassLoader, Extension class loader, loading classes in <java_home> \ jre \ lib \ ext
- System ClassLoader is also called the Application class loader.
Link
- Verify: ensure the correctness of the class file.
- Preparation: allocate memory for static fields and initialize it as the default value. No bytecode instruction is executed.
- Resolution: convert a symbolic reference to a method area (runtime frequent pool) for direct reference
Initialization
The execution class initialization method is to assign values to static fields and execute static blocks. The sequence is defined in sequence. The static fields of the parent class are initialized before the static fields of the Child class.
So far, a class or interface is loaded into the memory, and the JVM will ensure that the entire process isThread Security. Note that the entire process does not involve any instance objects.
Runtime data Zone
- Method Area:Thread sharingIt stores the runtime constant pool, class fields and method information, static variables, and method bytecode, which is a logical component of the heap. This part of garbage collection is optional. It is worth mentioning that since JDK8, the Hotspot JVM has adjusted the memory content. The class meta-data allocation uses the local memory, and the interned String and class static variables are moved to the Java heap.
- Runtime constant pool: For JVM, it plays a core role. Basically it involves methods or fields, and JVM will search for its specific memory address in the runtime pool.
- Heap:Thread sharing, Storage instance objects, instance variables, and arrays are the main areas of garbage collection.
- JVM Stack:Thread privateUsed to store stack frames. When the method is called,Stack frameThe stack frame consists of the following parts:
- Local variable table: stores this, method parameters, and local variables from 0.
- Operand Stack: the workspace of the method. data is exchanged between the operand stack and local variables, intermediate results are stored, and the depth of the operand stack can be determined during compilation.
- Frame data: the return value of the method, the exception distribution, and the reference of the Class running frequent pool where the current method is located.
- PC Register:Thread private, Save the current command address, and point to the next command address after execution.
- Native Method Stack:Thread privateStores local method information, C or C ++ stack.
Execution engine
Read, translate, and execute bytecode. JVM is based on the stack architecture. This stack is the operand stack, and bytecode commands are used to perform various operations. In addition, there are register-based virtual machines.
- Interpreter.
- JIT Compiler, instant Compilation: Find hotspot methods that are frequently called in programs, compile bytecode into local code, and improve performance.
- Garbage Collector, spam Collector: Collects invalid objects and determines whether objects can be recycled. Different Garbage collection algorithms can be used.
Local method interfaces and libraries
JNI: Call the local method, c/c ++ library, and the local Runtime Library required by the execution engine.
Summary
Mainstream JVM implementations include Oracle's Hotspot JVM, JRockit, and IBM's JVM. When it comes to JVM tuning, the default value is the Hotspot VM, which shows its popularity. Nowadays, it seems a little low-v-without learning about JVM -.
To write high-quality code, you must understand not only JVM, but also basic computer knowledge, such as tuning and troubleshooting. In fact, no matter what language is used for development, it is a process of building and improving its computer knowledge system.