JVM memory model
- The stack
- The heap
- Methods area
- Program counter
- Local method stack
The stack
Each thread is private, so there are stacks for every thread. A stack is also a data structure that follows FILO and creates a stack frame whenever a thread creates an execution method. The first frame to be pressed comes out last, and the last frame to be pressed comes out first. It can be understood that the stack is a bucket that seals the bottom, and the stack frame is a wooden block that is put into the bucket with the same sectional area as the bucket. You need to take out the wooden block that is put in the back before you can take out the one in front.
Stack frame structure
Within each stack frame there are several common structures (I don’t know how to describe them) such as local variable tables, operand stacks, dynamic links, and method exits (see Figure 1).
- [1] Local variable table: Stores local variables within the same method, such as (int A,String STR) a and STR here. Object handles in local variables have Pointers to specific object locations in heap memory
- [2] Operand stack: Java files are compiled into class bytecode files, which are translated into machine code. The operand stack is a way station for the operands that the program runs to execute JVM instructions. (Operand stack is also a stack structure.) For example int a = 1; int b = 2; int c = a+b; At this point, the program will be executed to push 1 onto the operand stack, and then the stack will pop 1 out and assign it to A (the first line of code), then push 2 onto the stack, pop 2 onto B (the second line of code), push 1 and 2 onto the stack (in two steps), pop 1 and 2 off the stack, calculate the result 3 and assign it to C (the third line of code). In Java, any data type can be placed on the operand stack. 32-bit data occupies the stack size of 1 and 64-bit data occupies the stack size of 2.
- [3] Method exit: record when the program enters this method, so that after the method is finished, the stack frame is ejected back to the original program execution position.
- [4] Dynamic linking: Replacing symbols in the constant pool with direct references while the program is running
Program counter
Each thread is private and records the number of lines that JVM instructions execute while the program is running.
Local method stack
The stack used to execute local methods is per-thread private. There are many low-level methods in Java that are modified native, and these methods are called native methods, and native methods are written in other languages like C.
The heap
The location of the object. The heap is divided into young generation and old generation. The default memory allocation ratio is 1:2. The young generation is divided into Eden area and Survivor area (divided into two parts,S0 and S1), and Eden:S0:S1:1:1. At the beginning of object creation, objects will be put into Eden area. When this area is full, Young GC will be triggered to clean up dead objects and move the remaining living objects to S0 area. When S0 area is full, Young GC will still be triggered and the remaining living objects will be put into S1 area GC and puts the remaining living objects back into the From section.
This means that when all three partitions of the Young generation are full, a Young GC is triggered and the remaining living objects are moved
Methods area
The method area, also known as the meta-space, holds constant pools, static variables, class meta-information, and so on. Objects in the heap point to information about the corresponding class in the method area.