Introduction and analysis of data area in Java runtime

Introduction to Java runtime data area

When the Java virtual machine executes a Java program, it divides the memory area it manages into several different data areas. These areas have their own purposes, creation and destruction times.

Insert picture description here

It can be seen that the runtime data area is mainly divided into 5 parts: method area, heap, virtual machine stack, local method area, and program counter. The first two method areas and heap are shared by all threads. The last three are threads Exclusive.

1. Program Counter ( Program Counter Register, PC )

I believe that students who have learned the principles of computer composition should be familiar with PCs, similar to hardware-level PCs**, the PC in the JVM is also used to control the execution flow of the current thread**. So why each thread needs a PC What? Because Java is a multi-threaded program by nature, and the execution of a multi-threaded program on a single processing core is not in sequence but concurrent execution, that is, the CPU is used by multiple threads in turn. If a thread is executed in the middle, the CPU The thread switch is performed, then it needs to record where it is executed, so that the CPU can return to the normal state when it executes it next time.

2. Virtual Machine stack ( Virtual Machine VM Stack Stack )

The life cycle of VM Stack and threads are the same. VM Stack describes the memory model of Java method execution . We often say that the method is
pushed into the stack, and this stack is entered. When each method is executed, the JVM will create one synchronously The stack frame is used to store the local variable table, operand stack, dynamic connection, method export and other information. Each method will be executed, a stack frame will enter the VM Stack, and will be popped out after the execution is completed.

The local variable table stores various JVM basic data types (boolean, byte, char, short, int, long, float, double), object references (reference) and returnAddress types. These data are stored in the local variable table. The storage space is represented by the **local variable slot ( Slot )**. Except for double and long, which occupy two slots , the others occupy only one.

3. Native Method Area

The function of the local method stack is very similar to that of the VM Stack.The difference is that the method of execution is different.The VM Stack executes the java method, and the local method stack executes the Native method.

4.Java Heap

The heap in Java can be said to be the largest memory area, where all object instances and arrays are stored . The memory space here is the responsibility of the garbage collector GC (Garbage Collector), because the memory in Java does not need to be manually managed by the programmer , So the GC will be responsible for the memory recovery in the Java heap. The heap will divide a private allocation buffer for each thread to improve the efficiency of object allocation.

5. Method Area

The method area is also shared by all threads. The method area mainly stores the type information, constants, static variables and other data that have been loaded by the virtual machine. According to my personal understanding, the method area has been determined from the beginning of the compilation of a program and will last for a long time. Will not change. The constant pool is also part of the method area.

Guess you like

Origin blog.csdn.net/qq_44823898/article/details/109702685