Detailed explanation of the five major data areas during JVM runtime

 Foreword:

        When the java virtual machine executes the Java program, it divides the memory area it owns into several data areas. These areas have different functions and perform their duties. These areas not only have different functions, but also have different creation and destruction times. Some areas are thread-private, such as: each thread has its own program counter, and the program counter is created when the user thread is created and destroyed when it is destroyed. And some areas are shared by all threads, such as the heap is shared by all threads. According to the "java virtual machine specification", the memory managed by the java virtual machine will include the following runtime data areas:

 Note: The JVM can be said to be a set of specifications.

Let me introduce each of these five areas in detail. 

1. Program counter (thread isolation)

        The program counter is a small memory space. Its function is to record the position of the next instruction of this thread. The multi-threading of the Java virtual machine is realized by switching threads in turn and allocating processor execution time. In multi-threading, the thread will be switched after executing its assigned time slice. We cannot guarantee that the thread will be switched after execution (the size of the time slice is uncertain). In order to ensure that the thread can be restored to the correct execution position after switching back. So we need the program counter to record the address of the next instruction for us. When the thread regains the CPU, go to the program counter to get the address of the next instruction and continue to execute. This is the main function of the program counter. So each thread needs its own program counter, independent of each other. So the program counter is private to the thread.

        To sum up: it is an indicator of program control flow, and basic functions such as branching, looping, jumping, exception handling, and thread recovery all need to rely on this program counter to complete.

2.java virtual machine stack (thread isolation)

        The virtual machine stack, like the program counter, is thread-private and has the same life cycle as its corresponding thread. The virtual machine stack internally stores stack frames (Stack Frames) for storing information such as local variable tables, operand stacks, dynamic links, and method returns. Each method represents a stack frame from the beginning to the end of execution, which is pushed into the virtual machine stack and popped out of the stack. It is the data structure of method call and method execution when the virtual machine is executed, and it is the basic element of the virtual stack.

        The basic data type data and object references are stored in the local variable table in the stack frame. This basic type is indeed stored in the stack frame, and the object reference stored here is not a real object, it may be a reference pointer pointing to the starting address of the object, or it may be the handle pointing to the object or other The position relative to this object.

        The size of the storage space for these data in the local variable table is represented by a local variable slot (Slot). Except for double and long types of data which occupy two variable slots, other data types occupy one variable slot. So how much local variable space this method needs has been determined just after calling this method. The size of the local variable table is not changed during the execution of the method.

Two exceptions are specified in this data area:

  • StackOverflowError exception: When the thread application thread depth is greater than the depth allowed by the Java virtual machine, this exception will be reported.
  • OutOfMemoryError exception: This exception will be reported when the virtual machine stack cannot apply for a sufficient depth when the virtual machine stack can be dynamically expanded.

3. Local method stack (thread isolation)

        This area is similar to the virtual machine stack. The difference between the two is that the virtual machine stack executes the java (that is, bytecode) method for the virtual machine, while the local method stack executes the local (non-Java) method for the virtual machine. There are even some virtual machines that combine the virtual machine stack and the native method stack into one. Like the virtual machine stack, the local method stack will also throw StackOverflowError and OutOfMemoryError exceptions when the stack depth overflows or the stack expansion fails.

4.java heap (thread sharing)

        The heap is the largest memory area in the JVM. The heap is an area shared by all threads. In the heap, the creation and destruction of objects are automatically managed by the garbage collector. It is created when the virtual machine starts. The sole purpose of this memory area is to store object instances, and "almost" all object instances in the Java world allocate memory here. But there are still some cases where it is not on the heap.

  1. Method inline optimization: In some cases, the compiler will perform method inline optimization, directly replacing the method call with the code in the method body. This optimization allows object instances to be created on the stack instead of the heap. In this case, the life cycle of the object instance may be relatively short.

  2. Thread-private objects: Some objects are only owned by one thread and will not be accessed by other threads. Such objects can be allocated on the stack, allowing for more efficient memory access.

        The Java heap can be implemented as either a fixed size or an expandable one. However, the current mainstream Java virtual machines are all implemented in terms of scalability. If there is no memory in the Java heap to complete the instance allocation, and the heap cannot be expanded. , the Java virtual machine will throw an OutOfMemoryError exception.

5. Method area (thread sharing)

In fact, the method area is a memory area that existed in versions before JDK1.8, mainly storing classes loaded from class files, and the constant pool is also in this area.

But after JDK1.8, this area has changed and changed its name to "Metaspace", which translates to "metaspace". Of course, it just changed its name, and the functions it implements remain unchanged.

Method Area (Method Area), like the Java heap, is a memory area shared by each thread. It is used to store data such as type information, constants, static variables, and code caches compiled by the instant compiler that have been loaded by the virtual machine.

5.1 Type information

For each loaded type (class class, interface interface, enumeration enum, annotation annotation), the JVM must store the following type information in the method area: ①The complete and
effective name of this type (full name = package name. class name)
② The full effective name of the direct parent class of this type (for interface or
java
. sequence listing

5.2 Domain information (Field) member variable

The JVM must save information about all fields of a type and the order in which fields are declared in the method area.
Domain related information includes: domain name, domain type, domain modifier (a subset of public, private, protected, static, final, volatile, transient)

5.3 Method information

The JVM must preserve the following information for all methods, as well as domain information including declaration order:

  • method name
  • The method's return type (or void) The number and types of method parameters (in order)
  • Method modifiers (a subset of public, private, protected, static, final, synchronized, native, abstract)
  • Method bytecodes (bytecodes), operand stack, local variable table and size (except for abstract and native methods)

Guess you like

Origin blog.csdn.net/qq_64680177/article/details/132232210