Necessary knowledge of JVM runtime data area: Java programmers should not miss

1. The concept of JVM runtime data area

The JVM runtime data area is the memory area used by the Java virtual machine when executing Java programs. These areas include the following sections:

  1. Program Counter (Program Counter Register): The program counter is a small memory area that can be seen as a line number indicator of the bytecode executed by the current thread. Each thread has its own independent program counter, which is used to record the address of the instruction that the thread needs to execute.
  2. Java virtual machine stack: The Java virtual machine stack is composed of stack frames (Stack Frame), and each stack frame corresponds to the call of a Java method. When a method is called, a corresponding stack frame is generated in the Java virtual machine stack and pushed onto the stack. When the method execution is complete, the stack frame is popped from the stack. The Java virtual machine stack is used to store information such as local variables, method parameters, return values, and operands.
  3. Native method stack: The native method stack is similar to the Java virtual machine stack, but it is used to execute the native method (Native Method). Native methods are methods implemented in languages ​​such as C and C++. They are different from Java code and require direct access to operating system resources.
  4. Java heap: The Java heap is the largest memory area in the Java virtual machine, and it is also the only memory area shared by all threads when the program is running. The Java heap is used to store data structures such as Java object instances and arrays. The Java heap can expand and shrink dynamically, and its size can be controlled through command line parameters.
  5. Method Area: The method area is used to store data such as class information, constants, static variables, and code compiled by a just-in-time compiler. It is one of the permanent storage areas in the Java virtual machine. The method area was called the permanent generation (PermGen) before Java 8, and it was gradually replaced by Metaspace starting from Java 8.
  6. Runtime Constant Pool: The runtime constant pool is the runtime representation of the constant pool table for each class or interface. It contains literals and symbol references generated at compile time, as well as string literals generated at runtime, etc. The runtime constant pool is part of the method area.

The above are the main components of the Java virtual machine runtime data area. Different areas differ in memory size and usage, but they are all important components that support the normal execution of Java programs. Understanding the runtime data area of ​​the Java virtual machine is very important for writing efficient and stable Java programs.

The following is the official description of Java:

docs.oracle.com/javase/spec…

2. Related issues

  1. What are the parts of the Java virtual machine runtime data area? Please briefly outline what they do.

The runtime data area of ​​the Java virtual machine includes: the program counter, the Java virtual machine stack, the local method stack, the Java heap, and the method area. The program counter is used to record the address of the instruction executed by the current thread; the Java virtual machine stack and the local method stack are used to store information such as local variables, method parameters, return values, and operands; the Java heap is used to store data structures such as Java object instances and arrays ; The method area stores data such as class information, constants, static variables, and code compiled by the just-in-time compiler; the runtime constant pool is the runtime representation of the constant pool table of each class or interface.

2. What is the difference between the Java virtual machine stack and the native method stack? Under what circumstances may they throw StackOverflowError and OutOfMemoryError exceptions?

Both the Java virtual machine stack and the local method stack are used to support the invocation of methods in Java programs, but there are some differences between them. The Java virtual machine stack stores the call frame of the Java method, while the native method stack stores the call frame of the Native method. The storage space of the Java virtual machine stack and the local method stack is different. The virtual machine stack is thread-private, and the local method stack is also thread-private like the Java virtual machine stack. They all may throw a StackOverflowError exception when the call depth cannot be dynamically extended. When the stack space cannot be further allocated, an OutOfMemoryError exception will be thrown.

3. What are the characteristics of the Java heap? How to set the size of the Java heap?

The Java heap is the largest memory area in the Java virtual machine, and it is also the only memory area shared by all threads when the program is running. The Java heap is used to store data structures such as Java object instances and arrays. The Java heap can expand and shrink dynamically, and its size can be controlled through command line parameters. You can use the -Xms parameter to control the initial size of the Java heap, and the -Xmx parameter to control the maximum size of the Java heap. When the Java heap cannot continue to be allocated, an OutOfMemoryError exception will be thrown.

4. What are the method area and runtime constant pool? What were they called before Java 8? Why is it gradually replaced by Metaspace starting from Java 8?

The method area is one of the permanent storage areas in the Java virtual machine, which is used to store data such as class information, constants, static variables, and code compiled by the just-in-time compiler. Before Java 8, the method area was called the permanent generation (PermGen). The runtime constant pool is the runtime representation of the constant pool table for each class or interface. It contains literals and symbol references generated at compile time, as well as string literals generated at runtime, etc.

Starting from Java 8, Metaspace gradually replaces the permanent generation (PermGen) and becomes a new memory area for storing data such as class information, constants, and static variables. Compared with the permanent generation, Metaspace no longer uses the memory in the Java heap, but uses local memory for management. This design enables Metaspace to automatically adjust the size, avoiding the problem of memory overflow caused by excessive growth of the permanent generation.

5. What is the function of the program counter? Why is the program counter defined as "thread-private"?

The program counter (Program Counter Register) is a small memory area used to record the address of the bytecode instruction executed by the current thread or the address of the next instruction to be executed. The function of the program counter is to indicate the instruction that the Java virtual machine will execute next.

The main reason why the program counter is defined as "thread-private" is that when multiple threads are executed concurrently, each thread can not interfere with each other. Each thread needs an independent program counter to track the address of the next instruction to be executed when executing its own task. If a program counter is shared during multi-thread execution, it will cause mutual interference between threads and cannot Execute the program correctly. Therefore, in order to avoid this situation, the Java virtual machine allocates an independent program counter for each thread to ensure the correctness of concurrent execution of multiple threads.

In addition, the program counter is also a memory area that has no effect on garbage collection, so the performance overhead and complexity brought by garbage collection can also be avoided.

Author: Xuanming Hanko
Link: https://juejin.cn/post/7221037319340245049/
Source: Rare Earth Nuggets
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, for non-commercial reprint, please indicate the source.

おすすめ

転載: blog.csdn.net/citywu123/article/details/130107500