Run-time data area
1. Program counter
Takes up a small amount of memory space, which can be thought of as the line number indicator of the byte code executed by the current thread. It is mainly used to record which statement the thread executes, and functions such as branching, looping, jumping, exception handling, and thread recovery need to rely on this counter to complete.
If the thread is executing a Java method, this counter records the address of the executing virtual machine bytecode instruction, or null if the native method is being executed. This memory area is the only area in the Java Virtual Machine specification that does not stipulate any outofmemoryerror conditions.
2. Java Virtual Machine stack
The thread is private and the life cycle is the same as the thread. The virtual machine stack describes the memory model that the Java method executes: Each method creates a stack frame to store information such as local variable tables, operand stacks, dynamic links, method exits , and so on.
The local variable table holds the various basic data types, object references, and ReturnAddress types (which point to the address of a bytecode directive) that are known at compile time. The memory that is required for the local variable table is allocated during compilation and does not change the size of the local variable table while the method is running.
In the Java Virtual Machine specification, two exceptions are specified for the jury area: 1) If the thread requests a stack depth greater than the virtual machine allows, the Stackoverflowerror exception is thrown. 2) If the virtual machine stack can be dynamically extended, the OutOfMemoryError exception will be thrown when the extension cannot request enough memory.
3. Local Method Stack
The local method stack is very similar to the virtual machine stack, but the difference between them is that the virtual machine stack executes the Java method service for the virtual machine, and the local method stack is the native method service used by the virtual machine.
4. Java Heap
The Java heap is a piece of memory that is shared by all threads and created when the virtual machine is started. Almost all of the object instances are allocated memory here.
Java heap can be divided into: The new generation and the old age. The Cenozoic can be subdivided into: Eden Space, from Survivor space, to survivor space.
A OutOfMemoryError exception will be thrown if there is no memory in the heap to complete the instance assignment and the heap can no longer be expanded.
5. Method area
The method area, like the Java heap, is an area of memory shared by each thread that stores data such as class information, constants, static variables, and code compiled by the immediate compiler that have been loaded by the virtual machine. Some people also call the method area "permanent generation", but they are not equivalent in nature. There is no concept of a permanent generation for some virtual machines.
6. Run a constant-rate pool
Running a constant pool is part of the method area. class file In addition to the class version, fields, methods, interfaces, and other descriptive information, there is also a constant pool, used to represent the various literal and symbolic references generated during the compilation period, which will be stored in the run-time pool of the class load backward into the method area.
7. Direct Memory
Direct memory is not part of the data area when the virtual machine is running, nor is it an area of memory defined in the Java VM specification. However, this part of memory is also used frequently, and may cause outofmemoryerror exceptions.
A new NiO class was added to JDK 1.4, which introduces a channel-and buffer-based I/O approach. It can use the native library to directly allocate out-of- heap memory and then manipulate it through a Directbytebufer object stored in the Java heap as a reference to that memory. This can significantly improve performance on some occasions because it avoids copying data back and forth in the Java heap and native heap.
In-depth understanding of Java Virtual Machine Note 01--run-time data area