Based on the problem described in the previous section, we know that the final cause is GC-led. In this section, we'll explore the Java memory model that is relevant to the GC in detail.
Noun interpretation: variable, understood as the basic type of java. object, which is understood as an instance of Java new.
The Java program runs in the JRE (Java Runtime Environment) and the JRE includes the Java API and the JVM (Java Virtual machine).
The Java original file is compiled with a Java byte code (. Class) file, and the JRE loads the Java byte code file into the JVM's Run-time data area via ClassLoader.
The Run-time data area of the JVM contains the PC Register, Java Virtual machine Stacks, Heap, Method area, Run-time Constant Pool, Native M Ethod Stacks.
The PC register: each thread has its own register. The main record is the method name, instruction address, returnaddress, current pointer, etc. that are being executed by each point-in-time thread. If you are currently executing Java native method (that is, Java's lowest-level approach), then the value of the PC Register for the front-thread is undefined.
Native method Stacks: This stack is used to execute methods that are not written in the Java language (that is, Native).
Heap: This space is shared by all the threads of the JVM. From this we can see that thread communication inside Java is done through shared memory. All of the instance objects and arrays are stored here. GC's collection of objects and the compression of space occur in this space.
Java Virtual Machine Stacks: When a thread is created, the JVM allocates a stack for this thread. The main storage thread is its own variables, partial results, references to shared memory, and so on. This space is allocated by the heap.
Method area: This space is allocated by the heap. is also shared by all the threads of all JVMs, primarily storing the code of the Run-time constant pool, method. This space is generally not a GC.
Run-time Constant Pool: This space is allocated by method area, which mainly stores constants, variables, and references.
Summary: 1. Diagram of the JVM run-time data area:
Examples of memory usage: When starting a class's main function (in fact, a process is started), the JVM's Run-time data area is allocated. The PC register records the name of this main function, the address of the instruction in the main function, etc., the global variables of this class are stored in the Run-time Constant Pool, the constructor of the class, the method body, and the variables in the method body. The JVM stack stores the local variables of this thread (main function body), the object's reference, the return result, the exception distribution, and so on; the instance of the main function body is stored in the heap.
Program execution process: the thread executes according to the instructions recorded by the PC register (for faster execution, these instructions are reordered, with CPU reordering and memory system reordering (Java Happen-before), which is supposed to be executed sequentially), This thread can read the local variables of the current JVM stack directly, read the global variables in the runtime constant pool, invoke the instance in the heap by reference to the object, and invoke method in the method area. Or Invoke method in the Native method area. it can be abstracted as the thread reads/writes the current thread's data and results through the heap.
Two GC-related Java memory model