[JVM] (1) In-depth understanding of the JVM runtime data area


1. JVM running process

JVM is the operating basis and operating environment of Java programs, and it is also "一次编译,到处运行"the key to Java implementation. Therefore, a deep understanding of the JVM is crucial to learning and understanding the Java programming language, but how exactly does the JVM work?

The following picture shows the basic operation process of JVM:

The execution process of the JVM involves the following main components:

  1. Convert Java code to bytecode ( .classfile) : After writing, the Java source code needs to be compiled into bytecode by a Java compiler to generate .classfiles, which contain the intermediate code of the Java program.

  2. Class Loader (ClassLoader) : The class loader of the JVM is responsible for .classloading the bytecode file into memory 运行时数据区. The class loader finds and loads the corresponding bytecode file according to the fully qualified name (Fully Qualified Name) of the class, and creates a Class object according to the content of the file to represent this class for subsequent execution engine calls .

  3. Running memory management : JVM is responsible for managing the memory area when the program is running, mainly including the following areas:

    • Virtual Machine Stacks (Java Virtual Machine Stacks) : Stack frames used to store method calls, including local variables, operand stacks, etc.
    • Native Method Stacks : Stacks used to execute native methods.
    • Method Area : store structural information of the class, constant pool, static variables, etc.
    • Heap (Heap) : The memory area that stores object instances and arrays.
    • Program Counter (Program Counter) : Record the address or index of the bytecode instruction executed by the current thread.
  4. Execution Engine : The execution engine is one of the core components of the JVM, responsible for executing bytecode files loaded into memory . The execution engine executes bytecode in two ways:

    • Interpretation and execution : Interpret bytecode instructions one by one and perform corresponding operations . Interpretation and execution are less efficient, but cross-platform, and are suitable for code segments that have just started to be executed or code segments that are executed less frequently.
    • Compile and execute : Compile bytecode into native code for a specific platform, and then hand it over to the CPU for execution . The compilation and execution efficiency is high, but additional compilation time is required, and it is suitable for code segments with frequent execution times.
  5. Native Libraries : The Java code in the JVM cannot directly access the underlying operating system, because the bytecode is just a set of cross-platform instruction set specifications. When Java code needs to execute the underlying operating system or interact with native code (such as C, C++). These functions need to be realized through the local method library . The native method library allows Java programs to call native codes related to the operating system, thereby realizing the functions of the entire program.

The above is the main running process of the JVM and the components involved in the runtime.下文是对其中的运行时数据区的详细介绍。

2. Virtual machine stack (thread private)

The virtual machine stack is a memory area private to Java threads, which is used to store information such as thread method calls and local variables . The life cycle of the virtual machine stack is the same as that of threads. The virtual machine stack describes the memory model of Java method execution:

  • When each method is executed, a stack frame (Stack Frame) is created to store local variable table, operand stack, dynamic link, method exit and other information.
  • When the method ends, the corresponding stack frame will also be destroyed by popping the stack.

The virtual machine stack mainly consists of four parts:

  1. Local variable table : used to store the local variables of the method . When a Java method is executed, it allocates a memory area for storing method parameters and local variables defined inside the method. The size of the local variable table is determined during compilation, and the stored data types include basic data types and object references.
  2. Operation stack : used for computing operations when executing methods . The bytecode instructions of the Java virtual machine are usually based on the operand stack. When a method is called, the parameter value and return value will be pushed into the operand stack. When the method is executed, the bytecode instruction will fetch the value from the operand stack for calculation.
  3. Dynamic link : used to point to the reference to the method to which the stack frame belongs in the runtime constant pool . In the runtime constant pool of the Java virtual machine, the symbolic references of the methods of each class are stored, and the dynamic link associates these symbolic references with actual memory addresses.
  4. Method return address : used to indicate the return address of the method . When the method execution is completed, it is necessary to know where to continue the execution. The method return address is used to record the return target address.

What is thread private:

  • Thread private means that in a multi-threaded environment, each thread has its own independent memory area, and other threads cannot directly access or modify this area. The data in the thread private memory is independent for each thread and does not affect each other. This design enables multi-threaded programs to execute concurrently, with each thread running and maintaining its own data independently.
  • In the JVM, 虚拟机栈, 本地方法栈and 程序计数器are thread-private memory areas . Each thread has its own virtual machine stack and local method stack to support method invocation and execution, and the data of these stacks is invisible to other threads. The program counter is also thread-private, and each thread has its own program counter, which is used to indicate the address or index of the bytecode instruction executed by the current thread. When the thread is switched, the value of the program counter will be saved and restored to ensure that the thread can continue to execute correctly after the thread is switched.
  • Other shared memory areas such as and 方法区(元空间)are shared by threads. The heap is used to store Java object instances and arrays, and the method area is used to store class structure information, constant pools, static variables, etc. Multiple threads can jointly access the data in the heap and method area, so in a multi-threaded environment, a synchronization mechanism is required to protect the consistency and correctness of the shared data.
  • The memory area is divided by thread private and shared:

3. Local method stack (thread private)

The native method stack is similar to the virtual machine stack, and it is also a memory area private to Java threads. It is used to support Java programs to call and execute native methods (Native Method) . A native method is a method written in other languages ​​(such as C, C++), and interacts with Java code through a native method interface (JNI, Java Native Interface). The native method stack and the virtual machine stack are also similar in function, but they are used for calling Java methods and native methods respectively.

4. Method area (metadata area)

The method area is a memory area shared by Java threads , which is used to store structural information of classes, constant pools, static variables, code compiled by the just-in-time compiler, etc. In JDK 8 and earlier versions, the method area is 永久代(Permanent Generation); while in JDK 8 and later versions, the method area is replaced by 元空间(Metaspace). The size of the method area can be set by parameters when starting the JVM.

In the "Java Virtual Machine Specification", this area is called "method area", and in the implementation of HotSpot virtual machine, this area is called Permanent Generation (Permanent Generation) in JDK 7, and Metaspace (Metaspace) in JDK 8 ).

Changes in JDK 1.8 metaspace:

  1. For HotSpot, the memory of the JDK 8 metaspace belongs to the local memory, so the size of the metaspace is no longer affected by the parameters of the JVM's maximum memory, but is related to the size of the local memory.
  2. In JDK 8, the string constant pool was moved to the heap.

Runtime constant pool:

The runtime constant pool (Runtime Constant Pool) is part of the method area, used to store various literal and symbolic references generated during compilation. It is constructed by the virtual machine according to the constant pool table in the bytecode file during the class loading process.

In the runtime constant pool, there are mainly two types of data:

  1. Literal :

    • String literal: the string value directly written in the Java program, for example: "Hello, Java". In JDK 8, string literals are moved to the heap so that they can also be garbage collected, avoiding some memory issues.
    • finalConstants: Constants that can be determined during compilation final, for example: final int MAX_VALUE = 100;.
    • Values ​​of basic data types: For example: literals of basic data types such as integers, floating-point numbers, and characters.
  2. Symbolic reference :
    A symbolic reference is a data structure that is generated during compilation but needs to be used during the class loading phase to describe the referenced target. Symbolic references include:

    • Fully qualified names of classes and interfaces: For example: java.lang.String.
    • Field name and descriptor: used to describe the name and data type of the field, for example: int count.
    • Method name and descriptor: used to describe the method name and parameter list and return value type, for example: void print(String message).

The runtime constant pool is stored in the method area after the class is loaded, and exists throughout the life cycle of the class, and is recycled together with the class itself. It provides functions such as constant pool analysis, dynamic linking, and method invocation when the program is running, providing the necessary support for the execution of Java programs.

5. Heap (thread sharing)

The heap (Heap) is a runtime data area in the Java virtual machine for storing Java object instances and arrays . The heap is the largest memory area in the JVM and the only memory area shared by all threads.

When a Java program is running, when newa keyword is used to create an object, the object instance will be allocated in the heap. At the same time, arrays are also objects, so the elements of the array are also stored in the heap. The size of the heap can be specified by parameters when starting the JVM, or it can be adjusted dynamically (if the size is not specified, the JVM will automatically set the initial size according to the system memory).

Key features of the heap include:

  1. Thread sharing : The heap is an area of ​​memory shared by all threads. All threads can access object instances in the heap, which enables multiple threads to jointly manipulate and share objects.

  2. Dynamic allocation and recycling : The size of the heap can be dynamically adjusted when the program is running. When an object is created, the JVM automatically allocates memory space in the heap. The garbage collector automatically reclaims the memory of unused objects in the heap when the objects are no longer referenced.

  3. Garbage collection : The memory in the heap is managed by the garbage collector. The garbage collector periodically checks the objects in the heap, marks objects that are no longer referenced as garbage, and then reclaims the memory of these garbage objects and releases them to the heap for use by other objects.

  4. Automatic memory management : The heap memory in Java is automatically managed by the JVM, and the programmer does not need to release the memory manually. The JVM will automatically perform garbage collection to release unused memory, avoiding problems such as memory leaks.

In addition, in the heap memory of the Java virtual machine, it is usually divided into two main areas: 新生代(Young Generation)and老生代(Old Generation) .

  1. Young Generation :
    The Young Generation is an area where newly created objects are stored . In the new generation, the heap memory is usually divided into one Eden space and two Survivor spaces (usually called S0 and S1). Newly created objects are first allocated to Eden space. When the Eden space is full, Minor GC (Young GC) will be triggered, and the garbage collector will copy the Eden space and the surviving objects in it to an unused Survivor space. Then, the garbage collector will clear the Eden space and the Survivor space in use, and recycle the garbage objects in it. Surviving objects are promoted to the old generation.

  2. Old Generation (Old Generation) :
    The Old Generation is an area that stores long-lived objects . Objects stored in the old generation are usually objects that survive after a certain number of Minor GCs in the new generation, or large objects. When the old generation space is full, Major GC (Full GC) will be triggered, and the garbage collector will recycle the entire heap, including all objects in the new generation and the old generation.

By dividing the heap memory into the new generation and the old generation, and adopting different garbage collection strategies, the efficiency of garbage collection can be improved. The Minor GC in the young generation is frequently performed to recycle objects with a short life cycle and release memory as quickly as possible; while the Major GC in the old generation is relatively infrequent to recycle objects with a long life cycle to ensure the stability of the old generation and reliability.

6. Program counter (thread private)

The program counter is also a memory area private to Java threads. It is an indicator that indicates what the current thread is executing字节码指令的地址或索引 . When the Java thread is switched, the value of the program counter will be saved and restored to ensure that the thread can continue to execute correctly after the thread is switched .

Guess you like

Origin blog.csdn.net/qq_61635026/article/details/132028246