Bored to rely on JavaEE from entry to abandon (3) JVM memory mechanism

1. The memory area belonging to the thread

In the memory division of JVM, some areas are private to threads, and some belong to the entire JVM process; we classify this part into one category.

1. Program Counter Register

In the JVM specification, each thread has its own program counter. This is a relatively small memory space that stores the JVM instruction address of the Java method being executed by the current thread, that is, the line number of the bytecode. If the Native method is being executed, this counter is empty.

2. Java Virtual Machine Stack (ava Virtal Machine Stack)

It also belongs to the thread private area. When each thread is created, a virtual machine stack is created. The life cycle is the same as that of the thread. When the thread exits, the thread's virtual machine stack is also recycled. Stack frames are kept inside the virtual machine stack, and the stack is pushed every time a method is called. The JVM has only two operations on the stack frame: popping and pushing the stack, and the popping operation is performed when the method call ends. This area stores the local variable table, various basic types of data, object references, method exits and other information that can be known at compile time.

3. Native Method Stack

Similar to the virtual machine stack, the local method stack is the stack used when calling local methods, and each thread has a local method stack.

2. Heap

Heap+(Heap), almost all Java object instances created are directly allocated to the heap. The heap is shared by all threads, and the area on the heap will be further divided by the garbage collector, such as the new generation and the old generation.
When the Java virtual machine is started, you can use parameters such as "Xmx" to specify the size of the heap area.

3. Method Area

The method area, like the heap, is also shared by all threads. It stores meta data loaded by the virtual machine, including data such as class information, constants, static variables, and code compiled by the just-in-time compiler.
The method area is a specification of the java virtual machine. Since the data stored in the method area is consistent with the data stored in the heap, it is essentially a heap. Therefore, the method area is implemented differently in different JDK versions.
Before JDK7, the method area was the "permanent generation" in the heap.
JDK7 began to "permanent generation", moving the static variable star and string constant pool to the heap memory.

After JDK8, the "permanent generation" no longer exists. The stored class information, compiled code data, etc. have been moved to the MetaSpace (meta space), the meta space is not on the heap memory, but the local memory (NativeMemory) directly occupied by the (direct memory)

Four. Run-Time Constant Pool (in the method area)

This is part of the method area. The constant pool mainly stores two types of constants:
1. Literal, such as text strings and final constant values.
2. Symbol references, which store some constants related to compilation, because Java does not have a connection process like C++, so these symbol references in field methods need to be converted during runtime to get the real memory entry address.

Five. Direct Memory (Direct Memory)

Direct memory is not part of the runtime data area of ​​the Java virtual machine specified by the Java specification. Java's NIO can use the Native method to directly allocate memory outside the Java heap, and use the DirectByteBuffer object as a reference to this off-heap memory.

6. Characteristics of the three major regions

1. Virtual machine stack

The characteristics of the virtual machine stack (abbreviation: stack) are as follows:
1. The stack describes the memory model of method execution. Each method is called to create a stack frame (storing local variables
, operands, method exits, etc.)
2.JVM creates a stack for each thread to store information about the thread's execution method (actual parameters, local changes,
etc.) )
3. The stack is private to the thread and cannot be shared between threads!
4. The storage feature of the stack is "first in, last out, last in, first out"
5. The stack is automatically allocated by the system and is fast ! The stack is a contiguous memory space!

2. Heap

The characteristics of the heap are as follows:
1. Heap is used to store created objects and arrays (arrays are also objects)
2. JVM has only one heap, which is shared by all threads
3. Heap is a discontinuous memory space, flexible allocation, slow speed !

3. Method area (static area)

The method area (also called static area) has the following characteristics:
1. The method area is a JAVA virtual machine specification, which can be implemented in different ways.

i. JD7 used to be the "permanent generation"
ii. JDK7 partially removed the "permanent generation", static variables and string constant pools were moved to the heap memory
ili . JDK8 was a combination of "metadata space" and the heap.
2. JVM has only one method area, which is shared by all threads!
3. The method area is actually a heap , just for storing information about classes and constants!
4. It is used to store the content that is always the same or unique in the program . (Class information [Class object, the reflection mechanism will
focus on teaching], static variables, string constants, etc.)

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_44593822/article/details/115229985