Virtual Machine stack and Virtual Machine

Source: Internet
Author: User

Virtual Machine stack and Virtual Machine
Article from http://www.mynawang.com/Citation

If you are not familiar with JVM, please first visit the article "Java VM runtime data zone", which will give you a general understanding of the corresponding functions of each memory zone. Next, let's talk about the virtual machine Stack: "The Virtual Machine stack is private to the thread. Every time a thread is created, the virtual machine creates a virtual machine stack for this thread, the Virtual Machine Stack represents the Memory Model of Java method execution. Each call to a method generates a Stack Frame for each method ), it is used to store information such as local variable tables, operand stacks, dynamic links, and method exits. The process of calling and completing each method corresponds to the process of putting a stack frame into and out of the stack from the virtual machine stack. The life cycle of the VM stack is the same as that of the thread ".

Virtual Machine Stack

Where,A Virtual Machine stack is a post-import, first-out stack.. Stack frames are stored in the Virtual Machine stack. Stack frames are data structures used to store data and stored partial process results. They are also used to process Dynamic links (Dynamic Linking), method return value, and Exception Dispatch (Dispatch Exception ). When a thread is running, only one stack frame is active.Currently active stack frame", The current active stack frame is always the top element of the VM stack. As shown in:

Stack frame

The above content has roughly introduced stack frames. Next, we will carefully describe the operand stack, dynamic connection, method return address, and some additional information in stack frames. As shown in:

1. Local variable table

A local variable table is a set of local variable value storage space used to store method parameters and local variables defined in the method. When the Java file is compiled as a Class file, the maximum capacity of the local variable table to be allocated by this method is determined in the max_locals data item of the Code attribute of the method table.

2. operand Stack

The operand stack is also known as the operation stack, which is a post-import, first-out stack. The JVM underlying bytecode instruction set is based on the stack type, and all operation codes operate on the data on the operand stack. For each method call, JVM will create an operand stack, for computing. Same as local variables. The maximum depth of the operand stack is written to the max_stacks data item of the code attribute of the method table during compilation. Each element of the operand stack can be any Java data type, including long and double. The stack capacity of the 32-bit data type is 1. The stack capacity of the 64-bit data type is 2. The stack capacity is measured in "word width". For 32-bit virtual machines, a "word width" occupies 4 bytes. For 64-bit virtual machines, A "word width" occupies 8 bytes. When a method is just executed, the operand stack of this method is empty. During the method execution, various bytecode points to the written and extracted values in the operand stack, that is, the inbound and outbound operations. For example, during arithmetic operations, parameters are transmitted through the operand stack or other methods. In addition, in the conceptual model, two stack frames act as the Virtual Machine stack elements and are completely independent from each other. However, most Virtual Machine implementations are optimized, make some of the Two stack frames overlap. Make the partial operand stack of the lower stack frame overlap with the partial variable table of the above stack frame, so that a part of data can be shared when a method call is returned, no additional parameter copying is required.

3. Dynamic connection

Each stack frame contains a reference pointing to the method attribute of the stack frame in the runtime pool to support dynamic connections during method calling. There are a lot of symbolic references in the constant pool of the Class file. The method call instruction in the bytecode takes the symbolic reference pointing to the method in the constant pool as the parameter. Some of these symbolic references are converted to direct references during the class loading stage or when used for the first time.Static Parsing. The other part will be converted into direct reference during each runtime, which is calledDynamic connection.

4. Method return address

After a method is executed, there are two ways to exit this method. The first method is that the execution engine encounters the bytecode instruction returned by any method. At this time, the returned value may be passed to the upper-layer method caller (the method that calls the current method is called the caller ), whether there is a return value or a return value type is determined based on the method returned command. This exit method is calledNormal Method Invocation Completion). Another way to exit is to encounter an exception during method execution, and this exception is not processed in the method body, whether it is an exception within the Java virtual machine, or exceptions generated by using the athrow bytecode command in the Code. If no matching exception processor is found in the exception table of this method, the method exits. This exit method is calledExit of exception Completion (Abrupt Method Invocation Completion). If a method exits when an exit is completed with an exception, no return value is generated for its call. No matter which method is used to exit, before exiting the method, you must return to the position where the method is called before the program can continue execution. When returning the method, you may need to save some information in the stack frame, it is used to restore the execution status of its upper-layer methods. Generally, when the method Exits normally, the value of the caller's PC counter can be used as the return address, which may be saved in the stack frame. When the method exits unexpectedly, the returned address must be determined by the exception processor. This information is generally not stored in stack frames. The process of method exit is actually equivalent to taking the current stack frame out of the stack. Therefore, the operations that may be performed during exit include restoring the local variable table and the operand stack of the upper method, push the returned value (if any) into the stack of the operands of the call all stack frames, and call the value of the PC counter to point to an instruction after the method call instruction.

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 highly relevant information, which depends entirely on the specific Virtual Machine implementation. In actual development, dynamic connections, method return addresses, and other additional information are generally classified as stack frame information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.