JVM - runtime data area

content:

1 Overview

2. Program Counter

3. Java virtual machine stack (stack memory)

4. Native method stack

5. Java heap

6. Method area

7. Runtime constant pool

8. Direct memory

1 Overview

The java virtual machine divides the memory it manages into several different data areas during the execution of the java program. These areas have their own purposes, as well as the time of creation and destruction, and some areas exist with the startup of the virtual machine process. Some regions are created and destroyed depending on the start and end of user threads.

The memory managed by the java virtual machine will include the following runtime data areas, as shown in the figure.

2. Program Counter

The Program Counter Register is a small memory space. It can be thought of as a line number indicator of the bytecode being executed by the current thread.

Thread-private : In order to restore the correct execution position after a thread switch, each thread needs to have an independent program counter.

If the thread is executing a java method, this counter records the address of the virtual machine bytecode instruction being executed; if the thread is executing a Native method, the counter value is empty (Undefined).

 

3. Java virtual machine stack (stack memory)

Java Virtual Machine Stacks describes the memory model of java method execution: each method creates a stack frame (Stack Frame) for storing local variable tables, operand stacks, dynamic links and method export etc. The process of each method from invocation to completion of execution corresponds to the process of a stack frame from being pushed to the stack in the virtual machine.

Thread private.

The local variable table stores various primitive data types known to the compiler , object references and returnAddress types (pointing to the address of a bytecode instruction).

abnormal:

StackOverflowError exception: If the stack depth requested by the thread is greater than the depth allowed by the virtual machine.

OutOfMemoryError: If the virtual machine stack can be dynamically expanded, an exception will be thrown if enough memory cannot be applied for during expansion.

 

4. Native method stack

The native method stack (Native Method Stack) and the virtual machine stack play a very similar role. The difference between them is that the virtual machine stack executes java method services as a virtual machine, while the native method stack is used by the virtual machine. Native method service.

In the virtual machine specification, there is no mandatory provision for the language, method and data structure used by the methods in the local method stack, so the specific virtual machine can freely implement it.

Like the virtual machine stack, the local method stack area also throws StackOverflowError exception and OutOfMemoryError exception.

 

5. Java heap

For most applications, the Java Heap is the largest piece of memory managed by the Java Virtual Machine. The Java heap is an area shared by all threads, created when the virtual machine starts.

The sole purpose of this memory is to hold object instances. What is described in the Java Virtual Machine Specification is that all object instances and arrays must be allocated on the heap, but with the development of JIT compilers and the gradual maturity of escape analysis techniques, stack allocation and scalar replacement optimization techniques will lead to some subtleties changes occur, and all objects on the heap gradually become less absolute.

The Java heap is the main area managed by the garbage collector, so it is often referred to as the "GC heap" (Garbage Collection Heap).

From the perspective of memory recovery, since the current collectors basically use the generational collection algorithm, the Java heap can also be subdivided into: the new generation and the old generation.

From the perspective of memory allocation, the Java heap shared by threads may be divided into multiple thread-private allocation buffers (Thread Local Allocation Buffer, TLAB).

The Java heap can be in a physically discontinuous memory space, as long as it is logically contiguous, similar to our disk space.

Exception: An OutOfMemoryError exception will be thrown if the instance allocation has not been completed in the heap and the heap cannot be expanded any more.

 

6. Method area

The Method Area, like the Java heap, is a memory area shared by each thread . It is used to store data such as class information, constants, static variables, and code compiled by the real-time compiler that have been loaded by the virtual machine . Although the java virtual machine specification describes the method area as a logical part of the heap, it has a special name called Non-Heap (non-heap), which should be distinguished from the java heap.

The Java virtual machine specification has loose restrictions on the method area, except that it does not require contiguous memory like the Java heap and can choose a fixed size or expandable size, and you can also choose not to implement garbage collection. Relatively speaking, garbage collection behavior is rare in this area, but it is not that the data enters the method area and exists "permanently" as the name of the permanent generation. The goal of memory reclamation in this area is mainly for the reclamation of the constant pool and the unloading of types.

 

7. Runtime constant pool

The Runtime Constant Pool is part of the method area. In addition to the description information of the class version, field, method, interface, etc. in the class file, there is also a constant pool (Constant Pool Table), which is used to store various literals and symbolic references generated by the compiler. This part of the content It will be stored in the runtime constant pool that enters the method area after the class is loaded.

 

8. Direct memory

Direct Memory (Direct Memory) is not part of the virtual machine runtime data area, nor is it a memory area defined in the Java Virtual Machine Specification. However, this part of the memory is also frequently used, and it may also cause an OutOfMemoryError exception.

In jdk1.4, the NIO (New Input/Output) class was newly added to introduce an I/O method based on a channel (Channel) and a buffer (buffer), which can directly allocate off-heap memory using the Native function library. Then operate through a DirectByteBuffer object stored in the Java heap as a reference to this memory.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325466403&siteId=291194637