[Learn JVM from scratch | Part 5] Quickly understand the runtime data area

Foreword:

        When talking about the running mechanism of Java programs, the runtime data area of ​​JVM (Java Virtual Machine) is an essential topic. The JVM runtime data area is an important area for Java programs to allocate memory and manage data during running. It includes the method area, heap, virtual machine stack, program counter, and local method stack. Understanding the JVM runtime data area not only helps to understand the running principles of Java programs, but also helps to optimize the performance of the program and solve some problems related to memory management. This article will introduce the structure and function of the JVM runtime data area from all aspects, hoping to provide readers with a comprehensive and in-depth understanding.

Table of contents

Foreword:

Runtime data area:

Program counter:

Java virtual machine stack:

Local method stack:

Heap: 

Method area: 

Miscellaneous knowledge points:

Summarize:


 

 

Runtime data area:


 

Next, I will explain the function of each part in detail:

Program counter:

Program Counter Register is a smaller memory area in the JVM (Java Virtual Machine). It is private to the thread, that is, each thread has its own program counter. The function of the program counter is to indicate the address of the virtual machine bytecode instruction being executed by the current thread or the address of the next instruction that needs to be executed. .

The following are some important functions of the program counter:

  1. Indicates the address of the next instruction: The program counter stores the address of the virtual machine instruction being executed by the current thread. When the thread is scheduled and resumes execution, the JVM will use the program counter to obtain the next instruction to be executed.

  2. Keep state when thread switching: Since the program counter is private to the thread, the value in the program counter will be saved and restored when the thread switches. This ensures that the thread can continue executing previous instructions without panic when execution resumes.

  3. Support thread interrupt and recovery: The status of the program counter can be used to support the thread interrupt and recovery mechanism. When a thread resumes execution after being interrupted or blocked, the program counter ensures that the thread can continue execution from where it was before the interruption without jumping to other locations.

  4. Handling exception jumps: The program counter is also used to handle exception jumps. For example, when an exception occurs, the program counter can instruct the JVM to jump to the instruction address of the exception handling code.

In the Java Virtual Machine Specification, the program counter is defined as part of the JVM, and operations on the program counter are part of the JVM instruction set. The size of the program counter is fixed, and memory overflow or memory leaks will not occur because it does not involve object allocation or garbage collection.

Java virtual machine stack:

Java Virtual Machine Stack (JVM Stack) is an important memory area in the Java Virtual Machine (JVM), used to store methods Local variables, operand stack, dynamic link, return address, method exit and other information. Each thread will be allocated a stack frame (Stack Frame) in the virtual machine stack when it is created. Whenever a thread calls a method, the JVM will create a corresponding stack frame in the virtual machine stack to store the relevant information of the method. Information

The so-called stack frame is a container that saves basic information of a method.

The following are some important features and functions of the JAVA virtual machine stack:

  1. Thread-private data area: Unlike the method area and heap, the Java virtual machine stack is a thread-private data area, which means that each thread has its own The virtual machine stack is used to store thread-exclusive method call information.

  2. The structure of stack frame: Each stack frame containsLocal Variable Table ,Operand Stack, Dynamic Linking, method return address and additional additional information. The local variable table is used to store the parameters and local variables of the method, the operand stack is used to store the operands during method execution, the dynamic link is used to point to the method reference of the current method in the runtime constant pool, and the method return address is used to Stores the return address after method call.
  3. Pushing and popping stack frames: When the thread executes a method call, the corresponding stack frame is pushed into the virtual machine stack. When the method execution ends, the stack frame Popped off the stack. This push and pop operation is based on the call and return relationship of the method.

  4. Supports recursive calling of methods: The existence of the virtual machine stack supports recursive calling of methods. Each recursive call will create a new stack frame in the virtual machine stack so that Store local variables and execution information of methods.

  5. Stack depth limit: JVM uses the virtual machine stack to manage method calls and returns, so the depth of the virtual machine stack is limited. If the method call level is too deep, a stack overflow (StackOverflowError) will occur in the virtual machine stack.

The Java virtual machine stack plays a vital role during program execution. It not only stores the local variables and execution information of the method, but also supports the calling and return of the method. If the JAVA virtual machine stack has too many stack frames and the memory occupied exceeds the maximum size that the stack memory can allocate, a memory overflow will occur.

Local method stack:

Native Method Stack is a memory area in the Java Virtual Machine (JVM), used to support the execution of Java virtual machines The data structure when the machine calls the native method. Similar to the virtual machine stack, the local method stack is also private to the thread. Each thread has its own local method stack, which is used for method calls and returns when executing local methods.

The following are some important features and functions of the local method stack:

  1. Support local method calling: The local method stack can be understood as the part of the virtual machine stack used to execute local methods. When the Java virtual machine calls a local method, the local method stack records the call information, including parameters, local variables, etc.

  2. The difference between the virtual machine stack and the virtual machine stack: The virtual machine stack is mainly used for the data structure when executing Java methods, while the local method stack is used for the data structure when executing local methods. There are similarities in structure between the two, but there are obvious differences in function.

  3. Depth limit of local method stack: Similar to the virtual machine stack, the local method stack also has a certain depth limit. When executing a native method call, if the depth of the native method stack exceeds the limit, a stack overflow error will occur.

  4. Security and performance: The existence of the local method stack is mainly to improve the security and performance of the Java virtual machine and the local method library, so that the Java virtual machine can communicate with local code. Seamless integration.

It should be noted that, like the virtual machine stack, the local method stack is also part of the definition in the Java virtual machine specification. Different virtual machines may have slightly different sizes and structures of the local method stack, but their roles and functions are similar. .

Generally speaking, the local method stack is an important memory area used by the Java virtual machine to support local method calls. By understanding the structure and function of the local method stack, you can better understand the interaction process between the Java virtual machine and the local method library, and improve the code operational efficiency and safety.

In the Hotspot virtual machine, the Java virtual machine stack and the local method stack implementation use the same stack space.


Heap: 

Heap is a memory area used for dynamic memory allocation, used to store object instances and arrays. In Java, all object instances and arrays allocate memory on the heap. Heap memory supports dynamic allocation and release, and is allocated through the memory allocation pointer on the heap. This means that objects can be created dynamically while the program is running, and the heap can be dynamically resized as needed. The heap memory is managed by the garbage collector in the Java virtual machine and is used to recycle objects that are no longer used and release the memory they occupy. Java's garbage collection mechanism mainly targets heap memory to ensure reasonable utilization of memory and program stability.

Structural division of the heap:

  1. Young Generation: used to store newly created objects. It is usually divided into Eden area and two Survivor areas. Most objects are created here, and then if they are still alive after several rounds of garbage collection, they will be moved to the old generation.

  2. Old Generation: Used to store long-lived objects, that is, objects that have survived multiple garbage collections from the new generation.

  3. PermGen/Metaspace: In earlier Java versions, the permanent generation (PermGen) was used to store class metadata, constant pool and other information. But in newer versions, Metaspace is used instead. This part of memory is mainly used to store meta-information of classes and methods, as well as some static data.

Division of heap memory:

  1. Used (used): Indicates the current heap memory size that has been used, that is, the memory that has been allocated to object instances and arrays The size of the space.

  2. Total (total): Indicates the total size of the current heap memory, that is, the total amount of heap memory currently allocated by the JVM. Includes used memory and unused memory.

  3. Max (maximum value): Indicates the maximum available space size of the heap memory, that is, the maximum heap memory size that the JVM can apply for.

In actual business, we will directly set Total to the same size as Max, thus avoiding the time overhead of applying for and allocating memory. At the same time, there will be no heap shrinkage after excess memory. The heap can also overflow.

Method area: 

Method Area is an important part of the Java Virtual Machine (JVM). It is used to store loaded Class information, constants, static variables, code compiled by just-in-time compiler and other data. In the virtual machine specification, the method area is shared by threads.

The method area is created when the virtual machine starts and is a continuous memory area. Its size can be fixed or dynamically expanded as needed. In the HotSpot virtual machine, the size of the method area is fixed and can be adjusted by setting JVM parameters.

The method area mainly stores the following content:

  1. Class information: includes the complete structure, fields, methods, inheritance relationships, interfaces, etc. of the class.

  2. Runtime constant pool: Each class has a runtime constant pool, which is stored in the method area. It contains literal and symbolic references. In the early days, the string constant pool was part of the runtime constant pool, but later the two were split

  3. Static variables: Static variables of all classes are stored in the method area.

  4. Code compiled by just-in-time compiler: JVM will compile the hot code just-in-time and store the generated local machine code in the method area.

The following are some important features and functions of the method area:

  1. Storage metadata: The method area is mainly used to store information such as metadata, constants, static variables, and symbol references in the class. This information is stored in the method area when the class is loaded and is shared by all instance objects.

  2. No manual memory management required: The method area does not need to be garbage collected like heap memory. This is because the data stored in the method area is not dynamically created and destroyed like objects in heap memory, but is determined when the class is loaded and usually remains unchanged during the running of the program.

  3. Transition from permanent generation to metaspace: In older JVM versions, the method area is usually implemented as the permanent generation (PermGen). But starting from Java 8, the permanent generation is replaced by Metaspace. The metaspace is no longer limited by the default maximum permanent generation size, but dynamically expands based on system memory.

  4. Dynamics: Unlike the permanent generation, the size of the metaspace is not limited by the default settings or the -Xmx parameter. It can dynamically change according to the actual needs of the application.

  5. Class information storage: The method area stores the structural information of the class, just-in-time compiled code, constant pool, static variables, etc. in the memory. These data are important for the execution of the program. Class loading, method calling, etc. play a key role.

Although the method area has always been very large in previous designs, it still has the risk of memory overflow.

Miscellaneous knowledge points:

1. How to determine whether a character object is stored in the string constant pool or the heap?

In Java, string objects may be stored in heap memory or in the string constant pool, depending on how the string object is created.

  1. String constant pool: When creating a string object using literal form, for example String s = "Hello", the string object will be stored in the character String constant pool. The string constant pool is a part of the Java heap memory used to store string objects created in the form of literals. This design can avoid repeatedly storing strings with the same content.

  2. Heap memory: When using the new keyword to explicitly create a string object, such as String s = new String("Hello"), this String objects will be stored in heap memory. This method creates a new string object in the heap memory, even if the string content already exists in the constant pool.

Therefore, string objects can be stored either in heap memory or in the string constant pool, depending on how the string object is created. It should be noted that can manually put the string object in the heap memory into the string constant pool through the intern() method

Summarize:

The JVM (Java Virtual Machine) runtime data area is a memory area that stores and manages data during the execution of a Java program. It is divided into multiple different areas, including the method area, heap, virtual machine stack, local method stack and program counter. wait.

First, the method area (called the permanent generation in Java 8 and before) stores the structural information, static variables, constants, and compiled method bytecode of each class. It can be shared by multiple threads at runtime and is one of the memory areas shared by all threads.

Secondly, the heap is a memory area that stores object instances and arrays. It is one of the most commonly used data structures in Java programs. The characteristic of the heap is that it can dynamically allocate memory, create object instances dynamically when the program is running, and it is a memory area shared by all threads.

The virtual machine stack is used to store local variables, operand stacks, dynamic links, method exits and other information of thread execution methods. When each method is executed, a stack frame is created to store local variables and operands, and the stack frame is destroyed when the method execution ends.

The local method stack is similar to the virtual machine stack. The difference is that the virtual machine stack serves Java methods, while the local method stack serves native methods (methods written in languages ​​such as C and C++).

Finally, the program counter is the line number indicator of the bytecode executed by the current thread. It allocates an independent program counter to each thread in a multi-threaded environment. It is used to record the execution position of the current thread and is private to the thread. memory area.

In general, the JVM runtime data area plays a vital role in the execution of Java programs. By properly managing these data areas, you can optimize program performance and memory utilization, and also help understand the execution mechanism of Java programs. and memory management principles

If my content is helpful to you, pleaselike, comment, and collect it. Creating is not easy, and everyone’s support is what keeps me going!

Guess you like

Origin blog.csdn.net/fckbb/article/details/134865127