[JVM] runtime data area

illustrate

During the execution of a Java program, the Java virtual machine divides the memory it manages into several different data areas. These areas have their own purposes, as well as the time of creation and destruction. Some areas always exist with the start of the virtual machine process, and some areas are established and destroyed depending on the start and end of user threads. As shown in the figure below, this article briefly introduces the functions of each area

image-20230823011318817

program counter

The Program Counter (PC for short) is a memory area in the Java Virtual Machine (JVM). It is a small memory space that cannot be affected by thread switching. Each thread has its own independent program counter, which is used to store the address of the bytecode instruction executed by the current thread.

The program counter has the following main functions in the JVM:

  1. Thread control : The program counter indicates the address of the instruction to be executed by each thread. The JVM is able to recover to the correct point of execution when the thread is switched.
  2. Bytecode interpreter : In Java, code is compiled into bytecode (bytecode). The program counter is used to keep track of the currently executing bytecode instructions so that the bytecode interpreter can execute instructions one by one.
  3. Exception handling : When a Java program throws an exception, the JVM will determine the location of the exception handling code based on the exception handling table. The program counter plays a key role here, helping the JVM know exactly where the bit-processing code is.
  4. Thread-private : Each thread has its own independent program counter. This enables threads to execute independently without being affected by other threads.

Simply put, the program counter is the address of the next instruction to be executed. Each thread will have it, and the thread is private.

virtual machine stack

The Virtual Machine Stack (Virtual Machine Stack) is a memory area privately created by the Java Virtual Machine (JVM) for each thread, and is used to store information such as local variables, operand stacks, dynamic links, and method exits during method execution. Each method will create a stack frame (Stack Frame) and put it on the stack when it is executed. After the method is executed, the stack frame will be popped out of the stack. The stack frame contains information such as the local variables of the method, the operand stack, and the return address.

The virtual machine stack has the following main characteristics:

  1. Thread private : Each thread has its own independent virtual machine stack, which ensures that the execution status of methods in a multi-threaded environment will not interfere with each other.
  2. Method call : The virtual machine stack is used to save the state of the method call. Each time a method is called, a stack frame is created on the virtual machine stack, and the stack frame contains information such as the local variables of the method and the operand stack.
  3. Local variable and operand stack : The stack frame contains local variable table and operand stack. The local variable table is used to store local variables in the method, and the operand stack is used for temporary storage when executing opcodes (bytecode instructions).
  4. Exception handling : The virtual machine stack also participates in the exception handling mechanism. When an exception occurs inside the method but is not caught, the virtual machine searches the virtual machine stack to locate the location where the exception occurred, so as to facilitate exception handling.

It should be noted that the size of the virtual machine stack can be configured, and a stack overflow (Stack Overflow) exception may occur in the stack space. Stack overflow is usually caused by too deep recursive calls or too much space occupied by the local variable table and operand stack.

To put it simply, each method corresponds to a stack frame, and the method call and execution represent the popping and stacking operations of the stack frame, and some necessary information of the method is stored in the stack frame.

native method stack

The native method stack (Native Method Stack) is similar to the virtual machine stack, which is a memory area prepared by the Java virtual machine for executing the native method (Native Method). Native methods refer to methods written in non-Java languages ​​(usually C, C++, etc.), and these methods can be called in Java programs through Java's native interface (JNI, Java Native Interface).

The native method stack is a memory area that exists to support the interaction between Java programs and non-Java native methods, similar to the virtual machine stack, but used for calling and executing native methods.

Java heap

When we write Java programs, all object instances (such as class instances, arrays, etc.) need to be stored in memory. The Java heap is where these objects are stored. It is a very large memory area shared by all threads.

The key points are as follows:

  1. Object storage : Every time newan object is created using the keyword, the object will be allocated in the Java heap. Whether it is a class defined by ourselves or a built-in class in Java, memory will be allocated on the heap.

  2. Garbage collection : The Java heap is managed by the garbage collector. When an object is no longer referenced by the program, that is, when there are no variables pointing to it, the garbage collector will reclaim the memory occupied by the object in order to allocate space for future objects.

  3. Generational structure : The Java heap is usually divided into different "generations", such as the new generation and the old generation. Newly created objects are allocated to the young generation, while objects that have lived longer are moved to the old generation. This generational structure helps improve the efficiency of garbage collection.

  4. Memory settings : We can set the initial size and maximum size of the Java heap through command line parameters. This can help us optimize the memory usage of the program.

  5. Memory overflow : If our program creates too many objects and exceeds the available space of the heap, a memory overflow error will be triggered and the program will crash.

In short, the Java heap is a memory area used to store objects in a Java program, where the garbage collector manages the allocation and release of objects to keep the program running normally.

method area

The method area (Method Area) is a memory area in the Java virtual machine, which is used to store metadata information, static variables, constant pools, method codes, etc. of the class. It is shared by all threads and, like the heap, is also part of the Java virtual machine.

Here are some key points about the method area:

  1. Metadata information : The method area is mainly used to store the metadata information of the class, including the name of the class, access modifiers, field information, method information, etc. This information is used by the Java virtual machine at runtime, such as during class loading, bytecode parsing, and method invocation.

  2. Static variables : Static variables, also called class variables, are stored in the method area. These variables are created and allocated during class loading, and they remain constant throughout the life of the class.

  3. Constant pool : The constant pool is a data structure stored in the method area, which is used to store various literals and symbol references generated at compile time. It includes information such as string constants, fully qualified names of classes and interfaces, names and descriptors of fields and methods.

  4. Method code : The method area also stores the method code of the class. These codes are executed when the class is called. The method bytecode stored in the method area is executed by an interpreter or a just-in-time compiler (such as HotSpot's C2 compiler).

  5. Runtime constant pool : In Java 7 and earlier versions, the constant pool also includes some constants generated at runtime. But starting with Java 8, the runtime constant pool has been moved to a part of the heap called the runtime constant pool.

  6. Memory overflow : Method area memory overflow errors are often referred to as "permanent generation overflow", because in Java 7 and earlier versions, the method area was implemented as a permanent generation. As the class loading and unloading continues, the space in the method area will also be exhausted, causing the program to crash.

It should be noted that starting from Java 8, the method area is replaced by Metaspace. Metaspace uses local memory instead of virtual machine memory, so it is more flexible and avoids problems such as persistent generation overflow.

In short, the method area is a memory area that stores information such as metadata, static variables, constant pools, and method codes of a class, and is one of the important components of the Java virtual machine.

runtime constant pool

When a Java class file is loaded into memory, a runtime constant pool (Runtime Constant Pool) is created, which is a runtime representation of the constants in the class. The run-time constant pool contains part of the content extracted from the compile-time constant pool of the class file, as well as constants generated at run time.

The compile-time constant pool is located in the class file, which contains various constants in the class, such as strings, numbers, class names, method names, and so on. The runtime constant pool is built when the class is loaded to support constant references and operations during program execution.

The runtime constant pool contains not only the contents of the compile-time constant pool, but also some constants generated at runtime. For example, the results of string splicing, dynamic method calls, etc. can all be reflected in the runtime constant pool.

It should be noted that starting from Java 8, the constant pool has been moved to the metaspace (Metaspace), replacing the previous permanent generation. Metaspace has more flexibility and is no longer constrained by a fixed size. In this case, the runtime constant pool still exists, but it is managed differently from the constant pool.

In short, the runtime constant pool is a data structure built after class loading, which contains part of the compile-time constant pool and constants generated at runtime. It provides constant reference and operation support for Java programs.

direct memory

When we use memory in a Java program, it usually involves Java heap memory, stack memory, etc. Direct memory is a memory allocation method different from traditional memory management methods, and is mainly used to improve the performance and efficiency of I/O operations. Direct memory is a memory allocation method used to improve the performance of I/O operations and is widely used in the Java NIO library. While it can provide some performance benefits, it requires developers to manage allocation and deallocation themselves to avoid potential risks.

In the traditional Java memory management method, the allocation and release of Java heap memory are managed by the garbage collection mechanism of the JVM. But in some specific scenarios, especially when I/O operations are involved, traditional memory management methods may cause performance problems. At this time, direct memory can be used as a medium to act as a bridge between the Java program and the operating system to improve performance and efficiency.

The traditional way of Java heap memory allocation involves the following steps:

  1. Copying of the application program to the Java heap memory : When data is transferred from the application program to the Java heap memory, data copying is required.
  2. Copying of Java heap memory to the operating system : When performing I/O operations, data needs to be copied from the Java heap memory to the kernel buffer of the operating system.
  3. Copying from the operating system to the Java heap memory : After the I/O operation is completed, the data needs to be copied from the kernel buffer of the operating system back to the Java heap memory.

And in the case of using direct memory:

  1. Application to direct memory copy : When data is transferred from the application to the direct memory, no data copy is required, and the data is directly stored in the direct memory.
  2. Direct memory to operating system copy : When performing I/O operations, data can be passed directly from direct memory to the operating system's kernel buffer, avoiding data copying.
  3. Copying from the operating system to direct memory : After the I/O operation is completed, the data can be passed directly from the kernel buffer of the operating system back to the direct memory, which also avoids data copying.

When performing I/O operations, data can be passed directly from direct memory to the operating system's kernel buffer, avoiding data copying.
3. Copying from the operating system to the direct memory : After the I/O operation is completed, the data can be directly transferred from the kernel buffer of the operating system back to the direct memory, which also avoids data copying.

In short, using direct memory can reduce the copying of data between memories, thereby improving the performance of I/O operations. This method is especially suitable for scenarios that require frequent large-scale I/O operations, such as file reading and writing, network transmission, and so on. However, it should be noted that the management of direct memory is the responsibility of the developer, and if not managed properly, it may lead to memory leaks and other problems.

Guess you like

Origin blog.csdn.net/m0_51545690/article/details/132440246