The growth path of the java architect-the runtime data area of the overall architecture of the JAVA virtual machine

Luban College Java Architect Growth Path

JVM memory area
Insert picture description here

1. Program counter:

The Java virtual machine can support simultaneous execution of multiple threads, and each Java virtual machine thread has its own PC register (program counter). At any time, a Java virtual machine thread can only execute the code of one method, and the method being executed by the thread is called the current method of the thread. If this method is not native, then the PC register stores the address of the bytecode instruction being executed by the Java virtual machine. If the method is native, then the value of the PC register is undefined. The capacity of the PC register should at least be able to store a returnAddress type of data or a local pointer value related to the platform.

This area is the only memory area in the JVM runtime data area where OOM exceptions will not occur.

2. Virtual machine stack:

Each thread has its own virtual machine stack, which is created at the same time as the thread and is mainly used to store stack frames. The start of a method call to the end of execution corresponds to the stacking and popping of the stack frame. The virtual machine specification allows the virtual machine stack to be set to a fixed size or to dynamically expand and contract based on calculations. If a fixed-size Java virtual machine stack design is adopted, the Java virtual machine stack capacity of each thread should be independently selected when the thread is created. The Java virtual machine implementation should provide programmers or end users with a means to adjust the initial capacity of the virtual machine stack. For the Java virtual machine stack that can be dynamically expanded and contracted, it should provide a means to adjust its maximum and minimum capacity.

2.1. Exceptions that may occur:

If the stack capacity allocated by the thread request exceeds the maximum capacity allowed by the Java virtual machine stack, the Java virtual machine will throw a StackOverflowError exception.

If the Java virtual machine stack can be dynamically expanded, and the expansion action has been tried, but cannot apply for enough memory to complete the expansion, or there is not enough memory to create the corresponding virtual machine stack when creating a new thread, then Java The virtual machine will throw an OutOfMemoryError exception.

2.2, stack frame

The call and return of a method in a thread correspond to the push and pop of a stack frame, that is, a method corresponds to a stack frame. The stack frame mainly includes local variable table, operand stack, dynamic connection, and completion exit. The capacity of the local variable table and operand stack is determined at compile time, and is saved and provided to the stack frame through the Code attribute of the method (instructions in the class bytecode). Therefore, the size of the stack frame capacity only depends on the implementation of the Java virtual machine and the memory that can be allocated when the method is called.

2.2.1, local variable table

A local variable can store data of type boolean, byte, char, short, float, reference, and returnAddress, and two local variables can store data of type long and double. Local variables use indexes for positioning access. The index value of the first local variable is zero, and the index value of the local variable is all integers from zero to less than the maximum capacity of the local variable table.

The Java virtual machine uses the local variable table to complete the parameter transfer when the method is called (that is, the parameters will be placed in the local variable table of the stack frame). When a method is called, its parameters will be transferred to the continuous starting from 0 The location of the local variable table. In particular, when an instance method is called, the 0th local variable must be used to store a reference to the object where the called instance method is located (that is, the "this" keyword in the Java language). Subsequent other parameters will be passed to the continuous local variable table position starting from 1.

2.2.2, operand stack

When the stack frame to which the operand stack belongs is just created, the operand stack is empty. The Java virtual machine provides some bytecode instructions to copy constants or variable values ​​from the fields of the local variable table or object instance to the operand stack. It also provides some instructions to fetch data from the operand stack, manipulate data, and The result of the operation is re-stacked. When the method is called, the operand stack is also used to prepare the parameters of the calling method and the return result of the receiving method.

2.2.3, dynamic connection

Each stack frame contains a reference to the runtime constant pool to support the dynamic linking of the code of the current method. In the Class file, it is described that a method calls other methods, or access to its member variables is represented by symbolic references. The function of dynamic linking is to convert the methods represented by these symbolic references into direct references of actual methods. In the process of class loading, the unresolved symbol references will be resolved, and the variable access will be converted into the correct offset of the runtime memory location where the storage structure of these variables is accessed.

Due to the existence of dynamic linking, the methods and variables of other classes used through late binding will not affect the method that calls them when they change.

2.2.4, complete the export

It mainly deals with the normal return or abnormal end of the method execution result.

Example:

package com.example.learn.note.jvm;



public class TestStack {



    public static void main(String[] args) {



        // 对应main中code为0和1的指令

        int a = 1;

        // 对应main中code为2和3的指令

        int b = 2;

        test(a, b);

    }



    public static int test(int a, int b){

        // 对应test中code为0和2的指令

        int c = 10;

        // 对应test中code为3和4的指令

        int d = 2;

        int e = (a + b) * c / d;

        return e;

    }

}

After compiling the javac class to generate the class bytecode file, disassemble it

D:\project\git-project\learn-note-parent\learn-note-common\target\classes\com\example\learn\note\jvm>javap -c TestStack

Warning: The binary file TestStack contains com.example.learn.note.jvm.TestStack

Compiled from “TestStack.java”

public class com.example.learn.note.jvm.TestStack {

  public com.example.learn.note.jvm.TestStack();

    Code:

       0: aload_0

       1: invokespecial #1                  // Method java/lang/Object."

       4: return



  public static void main(java.lang.String[]);

    Code:

       // iconst将一个常量加载到操作数栈

       0: iconst_1

       // istore将一个数值从操作数栈存储到局部变量表

       1: istore_1

       2: iconst_2

       3: istore_2

       // iload将一个局部变量加载到操作栈

       4: iload_1

       5: iload_2

       // invokestatic指令用于调用类方法(static方法)

       6: invokestatic  #2                  // Method test:(II)I

       // pop出栈

       9: pop

      10: return



  public static int test(int, int);

    Code:

       // bipush将一个常量加载到操作数栈

       0: bipush        10

       2: istore_2

       3: iconst_2

       4: istore_3

       5: iload_0

       6: iload_1

       // iadd加法指令

       7: iadd

       8: iload_2

       // imul乘法指令

       9: imul

      10: iload_3

      // idiv除法指令

      11: idiv

      12: istore        4

      14: iload         4

      16: ireturn

}

3. Local method stack

Similar to the virtual machine stack, except that it deals with native modification methods

The following exceptions may occur in the local method stack:

If the stack capacity allocated by the thread request exceeds the maximum capacity allowed by the local method stack, the Java virtual machine will throw a StackOverflowError exception.

If the local method stack can be dynamically expanded, and the expansion action has been tried, but currently cannot apply for enough memory to complete the expansion, or there is not enough memory to create the corresponding local method stack when creating a new thread, then Java Virtual The machine will throw an OutOfMemoryError exception.

4. Heap

In the Java virtual machine, the heap (Heap) is a runtime memory area that can be shared by each thread, and it is also an area for all class instances and array objects to allocate memory. The Java heap is created when the virtual machine starts, and it stores various objects managed by the GC.

The following exceptions may occur in the Java heap:

If the actual required heap exceeds the maximum capacity that the automatic memory management system can provide, the Java virtual machine will throw an OutOfMemoryError exception.

5. Method area

In the Java virtual machine, the method area is a runtime memory area that can be shared by each thread. It stores the structural information of each class, such as the runtime constant pool (Runtime Constant Pool), field and method data, the bytecode content of constructors and common methods, as well as some used in the initialization of classes, instances, and interfaces Special method.

The following exceptions may occur in the method area:

If the memory space of the method area cannot satisfy the memory allocation request, the Java virtual machine will throw an OutOfMemoryError exception.

6. Runtime constant pool

The runtime constant pool is the runtime representation of the constant pool of each class or interface. It includes several different constants: from the numeric literal known at compile time to the method or field reference that must be parsed at runtime. Each runtime constant pool is allocated in the method area of ​​the Java virtual machine. After the classes and interfaces are loaded into the virtual machine, the corresponding runtime constant pool is created.

When creating the runtime constant pool of classes and interfaces, the following exceptions may occur:

When creating a class or interface, if the memory space required to construct the runtime constant pool exceeds the maximum value provided by the method area, the Java virtual machine will throw an OutOfMemoryError exception.

Guess you like

Origin blog.csdn.net/LuBanXue/article/details/108782146