JVM composition of JAVA learning

The composition of JVM (Java Virtual Machine) is divided into: the overall component and the runtime data area component.

1. The overall composition of the JVM
The overall composition of the JVM can be divided into the following four parts:

类加载器(ClassLoader)

运行时数据区(Runtime Data Area)

执行引擎(Execution Engine)

本地库接口(Native Interface)

The purpose of each component:

Before the program is executed, the java code must be converted into a bytecode (class file). The jvm first needs to load the bytecode into the runtime data area (Runtime Data Area) in a certain way through a class loader (ClassLoader). ), and the bytecode file is a set of instruction set specifications of the jvm, which cannot be directly handed over to the underlying operating system for execution, so a specific command parser execution engine (Execution Engine) is required to translate the bytecode into the underlying system instructions and then It is handed over to the CPU for execution, and in this process, it is necessary to call the interface native library interface (Native Interface) of other languages ​​to realize the function of the entire program. This is the responsibility and function of these four main components.

What we usually call the jvm composition refers to the runtime data area (Runtime Data Area), because the area that usually requires programmers to debug and analyze is the "runtime data area", or more specifically, the "runtime data area". "The Heap (heap) module inside, then let's look at which modules the Runtime Data Area is composed of.

2. Composition of runtime data area

The runtime data area of ​​jvm may be slightly different for different virtual machine implementations, but they all follow the Java virtual machine specification. According to the Java 8 virtual machine specification, the memory managed by the Java virtual machine will include the following runtime data areas:

程序计数器(Program Counter Register)

Java虚拟机栈(Java Virtual Machine Stacks)

本地方法栈(Native Method Stack)

Java堆(Java Heap)

方法区(Methed Area)

Next, we introduce the purpose of each area separately.

①, program counter

The program counter (Program Counter Register) is a small memory space, which can be regarded as the line number indicator of the bytecode executed by the current thread. In the conceptual model of the virtual machine, the job of the bytecode parser is to select the next bytecode instruction to be executed by changing the value of the counter. Basic functions such as branching, looping, jumping, exception handling, and thread recovery are all Need to rely on this counter to complete.

Feature: memory private

Since the multithreading of jvm is realized by switching threads in turn and allocating processor execution time, that is, at any time, a processor (or a core) will only execute instructions in one thread. Therefore, in order to restore the correct execution position after thread switching, each thread has an independent program counter.

Exception Provisions: None

If the thread is executing a method in Java, the program counter records the address of the virtual machine bytecode instruction being executed. If it is a Native method, this counter is empty (undefined), so this memory area is the only one in the Java virtual machine. The area of ​​OutOfMemoryError is not specified in the specification.
②, Java virtual machine stack

Java Virtual Machine Stacks (Java Virtual Machine Stacks) describe the memory model of Java method execution. Each method will create a line frame (Stack Frame) to store local variable tables, operand stacks, dynamic links, Information such as method exit, the process from invocation to execution completion of each method corresponds to the process of a line frame being pushed into the virtual machine stack to popped out of the stack.

Features: Memory is private, and its life cycle is the same as that of threads.

Exception specification: StackOverflowError, OutOfMemoryError

1. If the stack depth requested by the thread is greater than the stack depth allowed by the virtual machine, a StackOverflowError exception will be thrown.

2. If the virtual machine can be dynamically expanded, if the expansion fails to apply for enough memory, an OutOfMemoryError exception will be thrown.
③, local method stack

The native method stack (Native Method Stack) is the same as the virtual machine stack, except that the virtual machine stack serves the Java method, while the native method stack serves the virtual machine to call the Native method.

In the Java virtual machine specification, there is no special requirement for the local method stack, and the virtual machine can implement it freely. Therefore, in the Sun HotSpot virtual machine, the local method stack and the virtual machine stack are directly combined into one.

Features and exceptions: Same as the virtual machine stack, please refer to the above knowledge points.
④, Java heap

The Java heap (Java Heap) is the largest piece of memory in the Java virtual machine. It is shared by all threads and created when the virtual machine starts. The sole purpose of the Java heap is to store object instances. Almost all object instances allocate memory here. , with the development of JIT compilers and the gradual maturity of escape analysis technology, the technology of stack allocation and scalar replacement optimization will lead to some subtle changes. All objects are allocated on the heap and gradually become less "absolute". .

Features: shared memory

Exception specification: OutOfMemoryError

If there is no memory in the heap to complete the instance allocation, and the heap can no longer be expanded, an OutOfMemoryError will be thrown.

The Java virtual machine specification stipulates that the Java heap can be in a physically discontinuous memory space, as long as it is logically continuous, just like our disk space. In terms of implementation, it can also be fixed size or scalable, but the current mainstream virtual machines are all scalable, controlled by -Xmx and -Xms.
⑤. Method area

The method area (Methed Area) is used to store data such as class information, constants, static variables, and just-in-time compiled code that have been loaded by the virtual machine.

Misunderstanding: The method area is not equal to the permanent generation

Many people refer to the method area as the "Permanent Generation" (Permanent Generation). The two are not equivalent in essence, but the HotSpot virtual machine garbage collector team has extended the GC generational collection to the method area, or it is used for permanent generation. The method area is only implemented by the generation, which saves the need to write memory management code specifically for the method area, but the "permanent generation" is also removed in Jdk8, and the method area is implemented using Native Memory.

Features: shared memory

Exception specification: OutOfMemoryError

When the method cannot meet the memory allocation requirements, an OutOfMemoryError exception will be thrown.

3. Expand knowledge

This section will expand some knowledge related to memory allocation.
runtime constant pool

The runtime constant pool is a part of the method area. In addition to the class version, fields, methods, interfaces and other description information in the Class file, there is also a constant pool (Constant Pool Table) used to store various types generated during compilation. Literal and symbolic references, this part enters the method area after the class is loaded and runs in the constant pool, such as the intern() method of the String class.
direct memory

Direct memory (Direct Memory) is not part of the data area when the virtual machine is running, but this part of memory is also frequently used, and may cause OutOfMemoryError. In JDK 1.4, the NIO class is newly added, and an IO method based on Channel and buffer Buffer is introduced. It uses a DirectByteBuffer object stored in the Java heap as a reference operation of this memory, so it is more efficient and avoids The time it takes for the Java heap and the Native heap to exchange data back and forth.

Note: Direct memory allocation will not be limited by the size of the Java heap, but limited by the total memory size of the machine. When setting virtual machine parameters, the direct memory cannot be ignored. Set the actual memory to -Xmx, so that the sum of the memory areas is greater than Due to the limitation of physical memory, an OutOfMemoryError exception occurs during dynamic expansion.

Four. Summary

This article talks about the main components of jvm, and the composition of the most important runtime data area (Runtime Data Area). The Java heap, as the largest memory area, will be the memory area that developers focus on, as well as the relationship between the method area and the runtime constant area and the permanent generation. Finally, the implementation process of direct memory needs to be used when it is used. The main point, I hope to help you better understand jvm.

 

 

Excerpt from blog: http://www.iotjike.com/article/37

 

 

Guess you like

Origin blog.csdn.net/dreamer23/article/details/92080307
Recommended