Introduction to Java Virtual Machine Stack Frames (Pictures and Text)
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 as local 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, only there 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:
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
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 table defaults 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Essentials for Java development: Detailed explanation of Java virtual machine installation steps, specific code examples required. With the development of computer science and technology, the Java language has become one of the most widely used programming languages. It has the advantages of cross-platform and object-oriented, and has gradually become the preferred language for developers. Before using Java for development, you first need to install the Java Virtual Machine (JavaVirtualMachine, JVM). This article will explain in detail the installation steps of the Java virtual machine and provide specific code examples.

The Java virtual machine uses reference counting to manage memory usage. When the reference count of an object reaches 0, the JVM will perform garbage collection. The reference counting mechanism includes: each object has a counter that stores the number of references pointing to the object. When the object is created, the reference counter is set to 1. When an object is referenced, the reference counter is incremented. When the reference ends, the reference counter is decremented.

With the continuous development of the Internet, more and more applications and businesses require the use of programs developed in the Java language. For the running of Java programs, the performance of the Java Virtual Machine (JVM) is very important. Therefore, optimizing configuration is an important means to improve the performance of Java applications. Pagoda panel is a commonly used server control panel that can help users manage servers more conveniently. This article will introduce how to use the Pagoda panel to optimize the configuration of the Java virtual machine. Step one: Install Java virtual machine

The stack frame is the basic data structure for executing methods in the Java Virtual Machine (JVM), and includes the following parts: Local variable table: stores the local variables of the method. Operand stack: stores operands and intermediate results. Frame data: Contains return address and current program counter. The functions of the stack frame include: storing local variables. Perform operand operations. Handle method calls. Assist with exception handling. Assisted garbage collection.

Detailed explanation of JVM principles: In-depth exploration of the working principle of the Java virtual machine requires specific code examples 1. Introduction With the rapid development and widespread application of the Java programming language, the Java Virtual Machine (JavaVirtualMachine, referred to as JVM) has also become indispensable in software development. a part of. As the running environment for Java programs, JVM can provide cross-platform features, allowing Java programs to run on different operating systems. In this article, we will delve into how the JVM works

Explore: The working principle and core functions of the Java Virtual Machine Introduction: The Java Virtual Machine (JavaVirtualMachine, JVM for short) is the core part of Java program running. It is responsible for compiling Java source code into executable bytecode and executing it. This article will delve into the working principles and core functions of the Java virtual machine, and use specific code examples to help readers better understand. 1. Working Principle of Java Virtual Machine 1.1 Class Loader (ClassLoader) J

Java core technology stack: In-depth understanding of the Java language, Java virtual machine, and JavaSE library. With the continuous development of computer science and technology, the Java language has become one of the most popular programming languages in the world. As a cross-platform high-level programming language, Java is widely used in various fields, especially in enterprise-level application development and cloud computing. To become an excellent Java developer, you must be proficient in the Java core technology stack, namely Java language, Java virtual machine and Java

Starting from scratch: Detailed explanation of Java virtual machine installation and configuration [Introduction] Java is a cross-platform programming language, and its execution platform depends on the Java Virtual Machine (JavaVirtualMachine, JVM). By installing and configuring the Java virtual machine, you can run Java programs on different operating systems. This article will take you from scratch, detail how to install and configure a Java virtual machine, and provide some commonly used Java code examples. Let’s start learning! [Part 1: J
