JVM series runtime data area

Table of Contents of Series Articles

Chapter 1 Operation Area Experiment



Preface

The JVM (Java Virtual Machine) running area is the memory area managed by the JVM during the running of the Java program. It includes the Heap, Stack, Method Area, Native Method Stacks, Program Counter and Direct Memory when the Java program is running.
Insert image description here


1. Heap

The Heap is an area used to dynamically allocate memory when a Java program is running. It is also the main area where the Java garbage collector performs garbage collection. Heap memory is shared by all threads.

Taking JDK8 as an example, the heap is used to store objects. Almost all (escape analysis technology, allocation on stack, scalar replacement) objects are allocated on the heap. The purpose of subdividing the Java heap is just to better reclaim memory. Or allocate memory faster. When there is no memory allocation object in the heap and it cannot be expanded, an OutOfMemoryError exception is thrown.

The Heap generally manages the new generation/Young area, the old generation (old area), and the String constant pool.

1.1. New generation/Young area

The new generation includes two areas: Eden area and Survival area.

1.1.1, Eden area

The Eden area is located in the young generation of the Java heap and is where new objects are allocated memory. Since the heap is shared by all threads, allocating memory on the heap requires locking. In order to improve efficiency, Sun JDK allocates an independent space on Eden for each newly created thread, which is exclusively used by that thread. This space is called TLAB (Thread Local Allocation Buffer). Allocating memory on TLAB does not require locking, so the JVM will try to allocate memory on TLAB when allocating memory to objects in the thread. If the object is too large or the TLAB is exhausted, allocation is still done on the heap. If the Eden area memory is also used up, a Minor GC (young GC) will be performed.

1.1.2、Survival section

The Survival area and the Eden area are both in the young generation of the Java heap. The Survival area has two blocks, one is called the from area and the other is the to area. These two areas are relative. After a Minor GC occurs, the from area will be interchanged with the to area. When Minor GC occurs, the Eden area and Survival from area will copy some surviving objects into the Survival to area and clear the memory. The Survival to area will move some objects that are old enough to the old generation.

1.2. Old generation (old area)

The old generation stores long-lived and larger objects, so the old generation uses a mark sorting algorithm. When the capacity of the old generation is full, a Major GC (full GC) will be triggered to recycle object resources that are no longer used in the old and young generations.

2. Virtual machine stack (Stack)

Each thread creates a private JVM stack when it is created. When each method is executed, a stack frame is created to store information such as local variables, operand stacks, and constant pool references. As shown in the figure below, stack frame 1, stack frame 2, stack frame 3, stack frame N.
Insert image description here

2.1. Stack top caching technology

Explain that in the execution state, the most frequently accessed data (program counter, stack top) are cached in the registers of the physical CPU, thereby reducing the number of reads/writes to the memory and improving the execution efficiency of the execution engine.

2.2. Overflow

If the stack depth requested by the thread is greater than the depth allowed by the virtual machine, a StackOverflowError exception will be thrown; if the Java virtual machine stack capacity can be dynamically expanded, an OutOfMemoryError exception will be thrown if sufficient memory cannot be applied for when the stack is expanded.

A StackOverflowError exception occurs and can be reproduced using infinite loop recursive code.
Insert image description here
You can use the parameter -Xss option to set the maximum stack space of the thread, for example -Xss265k

2.3. Stack frame

The storage unit of the stack is the stack frame. A method being executed on the thread corresponds to a stack frame. The stack frame is a memory block and a data set that maintains various data information during the execution of the method.

Each stack frame contains five parts, including local variable table, operand stack, dynamic linked list (method reference pointing to runtime constant pool), method return address and some additional information.
Insert image description here

2.3.1. Local variables table (local variables)

  • The local variable table is defined as a numeric array, mainly used to store method parameters and local variables defined in the method body. These data types include various basic data types, object references (reference), and returnAddress types.

  • Since the local variable table is the private data of the thread, there is no thread safety issue.

  • The required capacity of the local variable table is determined by the compiler and stored in the maximum local variables data item of the Code attribute of the method. The size of the local variable table will not change during method execution.

  • The variables in the local variable table are only valid during the current method call. When the method call ends, the local variable table will be destroyed as the method stack frame is consumed.

  • The most basic storage unit of the local variable table is Slot (variable slot). Types within 32 bits only occupy one slot (including returnAddress type and reference type), and 64-bit types (long and double) occupy two slots.

  • Each slot is assigned an access index, which occupies the variables of two slots. You only need to use the previous index.

  • Constructor method or instance method (non-static), the object reference this will be stored in the slot with index 0

  • The variables in the local variable table are also important garbage collection root nodes. As long as the objects are directly or indirectly referenced in the local variable table, they will not be recycled.

2.3.2. Operand Stack (Operand Stack LIFO)

  • The max_stacks data of the Code attribute of the .Class file after compilation records the maximum depth of the operand stack.
  • Operand stack. During method execution, data is written to or extracted from the stack according to the bytecode instructions, that is, pushed or popped from the stack. It is implemented using an array.
  • The operand stack is mainly used to save the intermediate results of the calculation process and also serves as a temporary storage space for variables during the calculation process.

2.3.3. Dynamic link

The process of converting symbol references in the constant pool into direct references during runtime is called dynamic linking;
each stack frame contains a reference to the method to which the stack frame belongs in the runtime constant pool. This reference is held to support method calls. Dynamic linking in process.

2.3.4. Method return address

The method return address stores the value of the pc register that called the method. When the method exits normally, the value of the caller's pc counter is used as the return address, that is, the address of the next instruction following the instruction that called the method.

2.3.5. Additional information

The Java virtual machine specification allows virtual machine implementation to add some information to the stack frame that is not described in the specification, such as information related to debugging and performance collection.

3. Method Area (MetaData Space)

Method area (MetaData Space metaspace)/Non-Heap is used to store data such as class information, constants, static variables, and code compiled by the just-in-time compiler that have been loaded by the JVM.
Insert image description here

3.1. The relationship between method area and permanent generation

Before JDK1.8, it was customary to call the method area the "permanent generation". The HotSpot virtual machine used the permanent generation to manage (implement) the method area before 1.7, so that the jvm's garbage collector can manage the memory of the permanent generation in the same way as heap memory, without having to write memory management code specifically for the method area. However, this design makes the virtual machine more prone to memory overflow problems. Unlike J9 and JRockit, there is no problem as long as the upper limit of the available memory of the process is not touched (32-bit 4GB).
JDK1.7 removes the string constant pool, static variables, etc. that were originally placed in the permanent generation (main remaining type information).
JDK1.8 completely abandons the concept of the permanent generation and uses it instead, like JRockit and J9, implemented in local memory. When the metaspace method area cannot meet the new memory allocation requirements, an OutOfMemoryError exception will be thrown.

4. Native Method Stacks

Native Method Stacks serve the native (Native) methods used by the virtual machine. The native method stack will also throw StackOverflowError and OutOfMemoryError exceptions respectively when the stack depth overflows or the stack expansion fails.

5. Program counter

The program counter is a line number indicator of the bytecode executed by the current thread. When the bytecode interpreter is working, it executes the next instruction by changing the value of the counter. This memory is the only area in the JVM specification that does not have an OutOfMemoryError.

6. Direct Memory

DM 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 memory is also frequently used, and may also cause OutOfMemoryError exceptions. For example, DirectByteBuffer in NIO directly operates off-heap memory to avoid copying data back and forth between the Java heap and Native heap.

At the same time, you can also pay attention to the application of zero copy (DMA) technology.


Summarize

The management and optimization of these memory areas are critical to the performance and reliability of Java programs.

Guess you like

Origin blog.csdn.net/s445320/article/details/132799685