Depth understanding of java virtual machine (9): bytecode execution engine introduction - stack frame

In a different java virtual machine inside the execution engine in the implementation of java code may be interpreted (for execution through an interpreter) may also be compiled executables (native code produced by the compiler to perform real-time), may also be a combination of both, even It may also contain several different levels of compiler execution engine.

Stack frame is a data structure of the virtual machine method calls and method of operation, a virtual machine is running a virtual machine stack stack elements, storing local variable table method, the operand stack, the dynamic link and return address and other information, each method from the beginning to the call to complete the execution corresponds to a stack frame the stack and the stack in a virtual machine stack. During the code compilation, a method of stack frames require much local variable table, much of the operand stack has been identified, and the method writes a code table attribute, a stack frame so how much memory needs to be allocated, the program is not affected by runtime data influence, depends only on the specific virtual machine implementation. Model is as follows:

 

 1, the local variable table

Local variable table storage space is a set of variables, local variables for storing process variables and parameters. Local variable table to the variable slot Slot as a minimum unit, the virtual machine specification no memory size specified slot, but requires a slot to be memory drop a 32-bit data types, such as boolean, byte, short, int, float, char, reference, referenceAddress. reference java virtual machine does not specify the length, but must be able to achieve the following two functions

1) directly or indirectly to find the starting address of the stack from the index data stored java virtual machine.

2) The direct or indirect references to find the type of object belongs to a type of data stored in the method area. When you call a first example of a method of non-default store this pointer slot, followed sequentially stored parameters and variables and scope in accordance with the order defined storage variable. If the current value of the counter is beyond the scope of a variable, then this variable corresponding slot can be used to other variables, in some cases slot multiplexing affect gc behavior.

package org.xiaofeiyang.classloader;

/**
* @author: yangchun
* @description:
* @date: Created in 2019-11-24 7:03
*/
public class LocalVaribleTest {
public static void main(String[] args){
byte[] res = new byte[64*1024*1024];
System.gc();
}
}
如果改成
org.xiaofeiyang.classloader Package; 

/ **
* @author: Yangchun
* @description:
* @date: the Created 2019-11-24 7:03 in
* /
public class LocalVaribleTest {
public static void main (String [] args) {
{
byte [] = new new byte RES [64 * 1024 * 1024];
}
int A = 0;
System.gc ();
}
}
memory will be recovered.
Local variables and class variables are not the same, class variables assigned zero value in the preparation stage, no assignment is also OK in the initialization phase. Local variables are not assigned an initial value is not acceptable.
2, the operand stack
when a method to begin the operand stack is empty, has bytecode instructions written to or kept in the course of extracting the content execution method. Data type of the operand stack elements must exactly match the byte code instructions, the program is compiled, the compiler should strictly ensure this.
As sharing data between stack frame, to ensure that no additional parameter passed when the copy transfer method.

 

 3, Dynamic Link

Each stack frame contains a reference to the runtime constant pool of the stack frame associated method of holding the reference is to support dynamic linking method calls. The method of the bytecode instruction to call a symbol reference constant pool directed to a method as a parameter. These symbols will translate to quote part of the class loading phase for the first time or when used as a direct reference, which is converted to static resolution. Another part will be converted during each operation to direct reference, is called dynamic linking.

 

4, the method returns the address

A method to begin to return only two ways, the first way is to perform engine encounters any bytecode instructions returned by the method, according to an instruction to return the result to the upper caller, called a normal return. The second method for performing the process encounters an exception, and the exception is not dealt with in the method body, whether it is abnormal or the internal virtual machine athrow thrown exception, as long as there is no method in the table to search for matching exception handler, it will cause the method to quit, will not return any return value to the upper caller. Either quit need to return to a position to be called, the program can continue execution method returns may want to store some information in the stack frame for the implementation of the state to help it restore its upper method, the caller's return as Counter pc address, stack frame may save this value, the abnormal exit the return address is to be determined by the exception handler table, stack frame generally will not save this part of the information.

Method exit process is the current stack frame at the stack exit possible operation, it is to restore the upper called method table of local variables and the operand stack, and returns the result onto the operand stack of the caller's stack frame, adjusting pc counter pointing to the next instruction of the caller.

 

5. Additional information

The main information is not provided for java virtual machine specification, such as debugging information,

Guess you like

Origin www.cnblogs.com/xiaofeiyang/p/11921254.html