The relationship between JVM and JMM

The relationship between the java memory model (hereinafter referred to as jmm) and the java virtual machine model (hereinafter referred to as jvm).

A jvm structure

The internal structure of the JVM is shown in the following figure. This figure clearly depicts the internal structure of the entire JVM, as well as the interaction and role of each part.

1 Class Loader (class loader) is to load the Class file into the memory. To be more specific, it loads the data describing the class from the Class file to the memory, and verifies, converts, parses and initializes the data, and finally forms a data that can be Java types used directly by the virtual machine, this is what the class loader does.

2 Run Data Area (runtime data area) is the memory managed by the JVM that we often say, and it is also the part we mainly discuss here. Running the data area is the focus of the entire JVM. All the programs we write are loaded here before they start running. This part is also the focus of our discussion here.

3 Execution engine (execution engine) is one of the core components of the Java virtual machine. The execution engine is used to execute instructions. In different internal implementations of the java virtual machine, the execution engine may have interpretation execution (interpreter execution) and compilation execution (local code execution generated by a real-time compiler, such as BEA JRockit) when executing Java code. , and possibly both. The core of any JVM specification implementation (JDK) is the Execution engine. The quality of different JDKs such as Sun's JDK and IBM's JDK mainly depends on the quality of the Execution engine they implement.

4 Native interface Interacts with native libraries and is an interface for interaction with other programming languages. When you call the native method, you enter a new world that is no longer restricted by the virtual machine, so it is easy to have the native heap OutOfMemory that the JVM cannot control.

Two Run Data Area (runtime data area)

data describe
Program Counter Register Program counter, thread-private , points to the next instruction to be executed
Java Stack The Java virtual machine stack is private to the thread , and the life cycle is the same as that of the thread. Describes the memory model of Java method execution: when each method is executed, a stack frame (Stack Frame) is created at the same time to store the local variable table, operation stack, dynamic link, method exit
Native Method Stack Serving Native methods used by virtual machines
Heap Thread sharing , due to the generational collection algorithm basically adopted by the collector, the Java heap can also be subdivided: the new generation and the old generation; more detailed ones include Eden space, From Survivor space, To Survivor space, etc. All object instances and arrays are allocated on the heap , which is the main area managed by the garbage collector
Method Area The method area, alias called Non-Heap, is a memory area shared by threads . The purpose is to distinguish it from the Java heap and store class information, constants, static variables, and code compiled by the just-in-time compiler .
The information stored in the method area includes:
Basic information of class A:
1. The fully qualified name of
each class 2. The fully qualified name of the direct superclass of each class (type conversion can be constrained) 3.
Whether the class is a class or an interface4
.Access modifier of this type
5. Ordered list of fully qualified names of direct superinterfaces
B Detailed information of loaded classes
1. Runtime constant pool: In the method area, each type corresponds to a constant pool, which stores the All constants used by the type, constants such as literal strings, final variable values, class names, and method names are stored in the constant pool. They are accessed by index in the form of an array, which is a bridge between external calls and class association and type objectification. (It may be an ordinary string, and after parsing by the constant pool, it becomes a reference to a class)
2. Field information: Field information stores the information of each field declared in the class, including the name and type of the field , modifier. The field name refers to the instance variable or class variable of the class or interface, and the descriptor of the field is a string indicating the type of the field, such as private A a=null; then a is the field name, A is the descriptor, and private is the modification symbol.
3. Method information: Information about each method declared in the class, including method name, return value type, parameter type, modifier, exception, and bytecode of the method. (At the time of compilation, the local variables of the method, the size of the operand stack, etc. have been determined and stored in the bytecode. When loading, the method area is loaded with the class.)
4. Static variables: the class Variables, all instances of the class are shared, we only need to know that there is a static area in the method area, and the static area is dedicated to storing static variables and static blocks.
5. Reference to class classloader: A reference to the class loader of the class.
6. Reference to class class: JVM creates an instance of java.lang.Class for each loaded type (translator: including classes and interfaces). And the jvm must somehow associate this instance of Class with the type data stored in the method area.

three jmm

Java Memory Model (Java Memory Model, JMM) JMM is mainly to specify some relationships between threads and memory. According to the design of JMM, there is a main memory (Main Memory) in the system. All variables in Java are stored in the main memory and are shared for all threads . Each thread has its own working memory (Working Memory). The working memory stores a copy of some variables in the main memory. The thread's operations on all variables are performed in the working memory, and threads cannot directly access each other. , the variable transfer needs to be completed through the main memory.

The relationship between jvm and jmm

The main memory and working memory in jmm and the Java heap, stack, method area, etc. in jvm are not the same level of memory division. The two are basically unrelated. If the two must be barely corresponding, then from From the definition of variables, main memory, and working memory , the main memory mainly corresponds to the object instance data part in the Java heap , while the working memory corresponds to a part of the virtual machine stack . At a lower level, the main memory directly corresponds to the memory of the physical hardware, and in order to obtain better running speed, the virtual machine (or even the optimization measures of the hardware system itself) may allow the working memory to be preferentially stored in registers and high-speed In the cache, because the main access to read and write when the program is running is the working memory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325722246&siteId=291194637