Home>Article>Java> Introduction to Java Virtual Machine Stack Frames (Pictures and Text)

Introduction to Java Virtual Machine Stack Frames (Pictures and Text)

不言
不言 forward
2019-02-01 11:47:05 3514browse

This article brings you an introduction (pictures and texts) about the stack frame of the Java virtual machine. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. Helps.

Written before: Java virtual machine is a science and a masterpiece of many Java masters. Due to my limited personal level and limited energy, I cannot guarantee that everything is correct. The content here is After careful consideration, some of the contents are quoted from the original work, which is already very good, so I won’t repeat them again. Of course, it is impossible to analyze all the details in depth here. We only talk about some more important concepts. Due to the lack of deep understanding of the principles of computer composition, most of them can only be analyzed using black box theory.

The stack frame structure at runtime (what is a stack frame?)

The stack frame is the data structure used by the virtual machine to make calls and method execution. Simply put, stack frame In fact, it is the stack element of the JVM runtime data area virtual machine stack (JVM Stack). The execution and call of each method corresponds to a stack frame. To give a simple example, define a Stack and put some objects called stack frames into this Stack. This object contains properties such aslocal variable table, operand stack, dynamic link and method return address. Next, let’s talk about the structure of the stack frame object.

First of all, you should understand that the call chain of a method in a thread may be very long, and many methods are in the execution state. For our execution engine, onlythere is one on the top of the stack. The stack frame is valid. This is called the current stack frame (Concurrent Satck Frame). The method related to this stack frame is called the current method (Concurrent Method). The corresponding conceptual model is as follows:

Introduction to Java Virtual Machine Stack Frames (Pictures and Text)

Through the conceptual model of the stack frame, let’s talk about what are the relevant attributes of the stack frame object? What is the data structure?

1. Local Variable Table

It is a storage space for a set of variables, used to store methods and internal methods. Variables. The maximum capacity allocation of the local variable table has been completed during Java compilation. To put it bluntly, the local variable table is a table that stores local variables and is used to store variables; its capacity is determined by the variable slot (Variable Slot, referred to as Slot), Slot is also the smallest unit; for relevant information, please refer to Zhou Zhiming's "In-depth Understanding of Java Virtual Machine 2 Edition" P238; the following is the memory space occupied by related types of data

下载 (1).png

As can be seen from the figure, the basic data types except double and long are divided into two 32-bits (that is, 2 Slots) for storage, that is, high-bit alignment; while other types only occupy one 32-bit Slot; In addition, the

reference type may be 32-bit or 64-bit, which is not clearly specified in Java. So how does the virtual machine access local variables?

The virtual machine uses the local variable table through the index positioning method. The index value ranges from 0 to the maximum number of Slots. When a method is executed, especially when an instance method is executed, the 0th index of the instance variable tabledefaults to the reference "this" object of the instance object to which the method belongs, followed by 1 to the Slot parameter variable to the method Internal local variables. In addition, in order to save stack frame space, the Slot of local variables can be reused, that is to say, method parameters and local variables within the method! =Maximum number of Slots. Since Slot can be reused, it not only saves space overhead, but also plays an unexpected role in the system's garbage collection. Refer to P239

2. Operand Stack (Operand Stack)


The operand stack is a last-in-first-out stack (LIFO). The basic principles are The storage method is the same as that of local variables. The stack capacity for 32-bit data types is 1, and for 64-bit data types it is 2; at any time when the method is executed, the depth of the operand stack will not exceed the value set in the max_statcks data item. maximum value. Refer to P242 and make a summary below


1. When the stack frame is first created, the operand stack inside is empty.

2. The Java virtual machine provides instructions to push some data onto the operand stack. For example, you can push data in the local variable table, instance fields, and other data onto the stack.

3. There are also instructions to support pop operations.

4. Parameters passed to other methods also exist in the operand stack.

5. The results returned by other methods are stored in the operand stack when returned.

6. There is a certain overlap between some operand stacks in the stack frame and the local variables of the previous stack frame, mainly for sharing data.

3.Dynamic Linking

Each stack frame contains a reference to the method attributed to the stack frame in the runtime constant pool. This reference is held to support dynamic connections during method calls. There are a large number of symbol references in the constant pool of the Class file. The method call instructions in the bytecode take the symbol references pointing to the methods in the constant pool as parameters. Some of these symbolic references will be converted into direct references during the class loading phase or when used for the first time. This conversion is called static resolution. The other part will be converted into a direct reference during each runtime, this part is called dynamic connection.

4. Method return address

When a method is executed, there are two ways to exit the method. The first way is that the execution engine encounters a bytecode instruction returned by any method. At this time, there may be a return value passed to the upper method caller (the method that calls the current method is called the caller). Is there a return value? The type of the return value will be determined based on which method return instruction is encountered. This method exit method is called normal method completion exit (Normal Method Invocation Completion).

Another way to exit is when an exception is encountered during method execution, and the exception is not handled within the method body, whether it is an exception generated within the Java virtual machine or using athrow in the code. Exceptions generated by bytecode instructions will cause the method to exit as long as no matching exception handler is found in the exception table of this method. This exit method is called Abrupt Method Invocation Completion. If a method exits using an exception completion exit, it will not generate any return value for its calls.

No matter which method is used to exit, before the method exits, you need to return to the location where the method was called so that the program can continue to execute. When the method returns, you may need to save some information in the stack frame to help with recovery. The execution status of its upper-level method. Generally speaking, when the method exits normally, the value of the caller's PC counter can be used as the return address, and this counter value is likely to be saved in the stack frame. When a method exits abnormally, the return address must be determined by the exception handler, and this part of the information is generally not saved in the stack frame.

The process of method exit is actually equivalent to popping the current stack frame. Therefore, the operations that may be performed when exiting include: restoring the local variable table and operand stack of the upper method, and returning the return value (if any). ) is pushed into the operand stack of the calling stack frame, and the value of the PC counter is called to point to an instruction following the method call instruction, etc.

5. Additional information

The virtual machine specification allows specific virtual machine implementations to add some information not described in the specification to the stack frame, such as height Related information, this part of information completely depends on the specific virtual machine implementation. In actual development, dynamic connections, method return addresses and other additional information are generally grouped into one category, called stack frame information.

The above is the detailed content of Introduction to Java Virtual Machine Stack Frames (Pictures and Text). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete