Runtime Data Area (runtime data area)

The runtime data area is the area used by the JVM to manage and store various data required during the execution of the Java program, and it also becomes the JVM memory structure. The JVM memory structure mainly includes the following parts:

  1. Program Counter Register

The program counter is a special memory area that can be implemented in the registers of the physical machine or in the stack of the virtual machine. The program counter is a very important part of the JVM, which is used to store the address of the bytecode instruction executed by the current thread . When the JVM is executing a Java method, the program counter is used to record the current execution position so that it can continue to execute next time.

The program counter is thread-private, and each thread has an independent program counter , so interference and competition between multiple threads can be avoided. The capacity of the program counter is relatively small, generally 32 or 64 bits, and its value is stored in the thread-private virtual machine stack.

  1. Java Virtual Machine Stacks (Java Virtual Machine Stacks)

Stack memory is the memory space used to store stack frames , which contain information such as operand stacks, method exits, and local variable tables. Each thread has its own stack memory, and the size of the stack memory is fixed (default 1MB), which has been determined at compile time and cannot be changed once allocated. When a method is called, the JVM will allocate a memory space (stack frame) for the method, and the space will be released after the method execution ends. Since the stack memory is thread-private , there are no thread-safety issues.

The variables declared inside the method are stored in the local variable table, and the values ​​involved in the operation or assignment operation are stored in the operand stack, which can be various types of data, such as integer, floating point, object reference, etc. The method exit refers to the return address, which method needs to return to after the execution of a method is completed.

public class JVMStacksDemo {
    
    

    static void methodA() {
    
    
        int a = 0;
        int b = 10;
        methodB(a, b);
    }

    static int methodB(int a, int b) {
    
    
        int x = 100;
        int c = a + b + x;
        String str = "str";
        return c;
    }

    public static void main(String[] args) {
    
    
        int a = 20;
        int b = 20;
        methodA();
    }

}

When maina method is called, the JVM creates a stack frame for it and pushes the return address of the method onto the top of the stack. The stack frame contains mainthe method's local variable table and operand stack. mainThe method defines two integer variables a, b, and assigns aand brespectively 20. As shown in the figure below, you can see the stack frame with the method in the current stack , and you can see the command line parameters of the method, defined integer variables , and their values main​​in the local variable table on the right .mainab

Please add a picture description
Then mainthe method is called methodA.

methodAAt this point, the JVM creates a new stack frame for the method and pushes the return address of the method onto the top of the stack. As shown in the figure below, you can see methodAthat the stack frame in the stack is at the top of the stack.

Please add a picture description
methodACalled in , methodBin the local variable table of methodB, you can see methodAthe incoming parameters a, b, defined variables x, cand reference data type variables str.
Please add a picture description

When methodB is executed, the stack frame of methodB is popped. MethodA is the same. Finally, the main method is executed and the stack space is released.

Please add a picture description

Please add a picture description
Please add a picture description

  1. Java Heap

Heap memory is the largest piece of memory in a Java program, used to store object instances and arrays.

When the JVM starts, it will create and allocate an initial size heap space, which can be specified by JVM parameters. When the heap space is insufficient, the JVM will automatically expand the size of the heap. Heap memory is shared by all threads , so thread safety issues may arise. But usually, Java objects are accessed through references, so there is no need to consider the thread safety of the object itself, but the concurrent access to references.

  • Object instances and arrays stored in heap memory need to be created using the new keyword. If the new keyword is not used, they are stored in stack memory.

  • Object instances include those in Java such as String, ArrayList, and HashMap, as well as custom ones.

  • The heap memory also stores instance variables of objects, such as instance variables such as name and age of the Person class. The static variables of the class are stored in the method area, and they are also shared by all threads.

  • Some basic types of wrapper classes are also stored in the heap memory, such as commonly used wrapper classes Integer, Long, Float, Double, Boolean, Byte, Short, and Character.

The heap memory is logically divided into the new generation and the old generation, and the new generation is divided into Eden area, Survivor1 area and Survivor2 area. The young generation mainly stores objects with a short life cycle, while the old generation mainly stores objects with a long life cycle.
When a Java object is created, it is first allocated in the Eden area. When the Eden area is full, Minor GC garbage collection is triggered, and the surviving objects are copied to the Survivor0 or Survivor1 area. When the Survivor0 area or the Survivor1 area is full, Minor GC will also be triggered to copy the surviving objects to another Survivor area. After multiple GCs, surviving objects are moved to the old generation.

  1. Method Area

The method area is a memory space used to store relevant information of the loaded class (including class name, access modifier, constant pool, field description, method description, etc.), as well as literals and symbol references in the constant pool. The method area is logically part of the heap.

  • The method area is shared by all threads , created when the JVM starts, and destroyed when the JVM is destroyed. The JVM will adjust the size of the method area by itself according to the actual situation.

  • Constant pools and static variables After jdk8, constant pools and static variables were transferred to Metaspace.

  • The method area is also one of the goals of garbage collection, which mainly recycles useless class information and constants in the constant pool.

  1. Native Method Stacks

Native methods are methods written in C/C++ or other native languages ​​that are not compiled into bytecode like Java methods, but are compiled into native machine code. The local method stack is similar to the stack memory, and is used to store the call stack of the local method and the local variables of the local method. Native method stacks are also thread-private. When executing a native method, the JVM will create a stack frame in the native method stack to store the parameters and local variables of the native method. Similar to the Java method stack, the stack frame consists of an operand stack and a local variable table, and their functions are the same as the Java method stack.

Guess you like

Origin blog.csdn.net/m0_56170277/article/details/130458033