Runtime data area of JVM architecture

1. Briefly describe the distinction between Java runtime data

 

PC register/program counter

Strictly speaking, it is a data structure used to save the memory address of the currently executing program. Since Java supports multi-threaded execution, the trajectory of program execution cannot always be linear execution. When multiple threads are executed crosswise, the memory address to which the program of the interrupted thread is currently executed must be saved, so that when the interrupted thread resumes execution, it will continue to execute according to the interrupted instruction address. In order to restore to the correct execution position after thread switching, each thread needs to have an independent program counter. The counters between each thread do not affect each other and are stored independently. We call this type of memory area "thread-private" memory. This is somewhat similar to "ThreadLocal" in a way, and is thread-safe.


Java 栈 Java Stack

The Java stack is always associated with a thread. Whenever a thread is created, the JVM will create a corresponding Java stack for the thread. This Java stack will contain multiple stack frames. These stack frames are Associated with each method, a stack frame is created every time a method is run, and each stack frame contains some information such as local variables, operation stacks, and method return values. Whenever a method is executed, the stack frame will pop up the elements of the stack frame as the return value of the method, and clear the stack frame. The stack frame at the top of the Java stack is the active stack currently being executed, that is, the current The method being executed, the PC register will also point to this address. Only the local variables of this active stack frame can be used by the operation stack. When another method is called in this stack frame, a new stack frame corresponding to it is created, and this newly created stack frame is placed on the Java stack. The top of the stack becomes the current active stack. Also now only the local variables of this stack can be used. When all the instructions in this stack frame are completed, this stack frame is removed from the Java stack, the previous stack frame becomes the active stack frame, and the return value of the previous stack frame changes It is an operand of the operation stack of this stack frame.

Since the Java stack corresponds to the thread, the Java stack data is not shared by the threads, so there is no need to care about its data consistency, and there will be no synchronization lock problem.

In the Java virtual machine specification, two exceptions are specified for this area: if the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown; if the virtual machine can be dynamically expanded, if the expansion cannot be applied If there is enough memory, OutOfMemoryError will be thrown. In the Hot Spot virtual machine, you can use the -Xss parameter to set the stack size. The size of the stack directly determines the reachable depth of the function call.

 

Heap

concept

The heap is the largest piece of memory managed by the JVM. It is shared by all Java thread locks and is not thread-safe. It is created when the JVM starts. The heap is a place where Java objects are stored. This is described in the Java Virtual Machine specification: all object instances and arrays must be allocated on the heap. The Java heap is the main area of ​​GC management . From the perspective of memory recovery, since GC basically uses generational collection algorithms, the Java heap can also be subdivided into: the new generation and the old generation; the new generation can be more detailed with Eden Space, From Survivor space, To Survivor space, etc.


Method Area

concept

The method area is a part of the heap, which is what we usually call the permanent area (Permanet Generation) in the Java heap. The size can be set by parameters. The initial value can be specified by -XX:PermSize and the maximum value can be specified by -XX:MaxPermSize.

The method area stores the information of the class to be loaded (name, modifier, etc.), static constants in the class, constants defined as final in the class, Field information in the class, and method information in the class. When passed in the program When methods such as getName.isInterface of the Class object are used to obtain information, these data all come from the method area.

The method area is shared by the Java thread lock. Unlike other parts of the Java heap, it is frequently recycled by the GC. The information it stores is relatively stable and will be GC under certain conditions. When the method area uses more memory than it allows When the size is larger, an OutOfMemory error message will be thrown.

Constant Pool

The constant pool itself is a data structure in the method area. The constant pool stores constants such as strings, final variable values, class names, and method names.

The constant pool is determined during compilation and saved in the compiled .class file.

Generally divided into two categories:

Literals: Strings, final variables, etc.

Quotation: The class name and method name belong to the quotation. The most common is to find the method reference based on the method name when calling the method, and use this as the function body to execute the function code. The reference volume includes: the authority name of the class and interface, the name and descriptor of the field, and the name and descriptor of the method.

 

Native Method Stack

The role played by the native method stack and the Java stack is very similar. The difference is that the Java stack serves the JVM to execute Java methods, while the native method stack serves the JVM to execute Native methods. The native method stack also throws StackOverflowError and OutOfMemoryError exceptions.

2. JVM memory model JMM

Why would such a model be divided?
The life cycle of the object is different when the code is executed, so we are divided into three generations in our heap, and the size of the three generations is different according to the size of the gc allocation, which is actually to avoid wasting space

After 1.8, Meta Space replaced the permanent generation (permanent generation memory is often insufficient or memory leaks, an exception java.lang.OutOfMemoryError:), Meta Space exists in memory, and the size is variable (just like ArrayList) , The disadvantage is that if the Meta Space expands indefinitely, it will occupy other resource space in the memory and cause memory overflow.

3. What kind of object will be gc

Judgment algorithm
Reference counting method: When the object is referenced, it is counted in the object. When the object is not used, the count is 0 and it is recycled. When it encounters a circular reference (ie A refers to B, B refers to A), it does not refer to each other in time, and it cannot be recycled. This is also the reason why the algorithm was finally eliminated by gc

Reachability analysis: Depending on gcRoot, the object is recycled when gc root does not point to an object. The objects that can become gcRoot are: ① the object referenced by the local variable table in the virtual machine stack; ② the object referenced by the class static variable in the method area ③ the object referenced by the constant in the method area; ④ the object referenced by the jni in the local method. Reason: ①④It must exist when the thread is running, ②③is always there.

So will the unreachable be reclaimed? Not necessarily, finalize() can make the lost reference object reachable again, but it can only be adjusted once.

 

Guess you like

Origin blog.csdn.net/shenyuye/article/details/108075114