Java Virtual Machine stack frame [knowledge]

  Java Virtual Machine stack frame stack structure, each stack frame has its own local variable table, the operand stack, and a method for dynamic link return address and other information, further additional information related to allowed JVM implementations, e.g., the program support for debugging information.

1 local variable table

  Local variable table stores elementary data types (8 types), object reference, returnAddress type. Slot the variable slot as the minimum unit, a Slot can store a 32-bit data type within, therefore, long and two double Slot need to save. Stack frame using a smaller index value to locate, for example, double the value of the index n type, in fact, with the index values ​​of n and n + 1 two local variable to store the value, when accessed by an index value n of locate the double.

  Java Virtual Machine using the local variable table to complete the parameters passed when the method call. When calling a class method (static class methods), the parameters passed to the local variable table sequentially from the successive positions of zero. When calling instance method (new), the 0-th local variable must be used to store the reference (this keyword) where the subject example method.

  To save space, Slot can be reused, when the value of PC counter exceeds the scope of a variable, then this variable corresponding Slot can be passed on to other variables. However, such a design can also cause side effects, such as it will affect the GC behavior. The following questions:

Question: can be expressed by setting null objects recyclable?

  This operation can be used as an extremely clever under special circumstances (the object occupying large memory, the stack frame of this method for a long time can not be recycled, the number of method calls reach the JIT compiler conditions). Usually after the JIT compiler optimization will eliminate null assignment operator, so generally you do not need to assign null.

Non-atomic agreement long and double the

  For 64-bit data types, JMM defines a relatively loose requirements: dividing the virtual machine allows read and write operations will not be modified in the volatile 64-bit data into two 32-bit operation is performed, i.e., selection may be implemented to allow the virtual machine 64 does not guarantee that the data type of load, store, read, write atomic operation, which is long and double non atoms agreements.

  That is, if there are multiple threads share a variable is not declared as volatile in the long or double, and at the same time they are read and modify operations, some threads might read it to the original value of a neither nor It represents the value of "half variable" other threads to modify values.

  However, almost all current commercial virtual machine to select the 64-bit data write operation is treated as an atomic operation, and therefore generally do not need to write code when used long and double specifically declared as volatile.

Operand stack 2

  When the stack frame just created, operand stack is empty. Java virtual machine provides a number of bytecode instructions to copy from a constant or variable or a local variable table field object instance value to the operand stack, also provides some instructions for removal of data from the operand stack, operating data, and the operating results New stack. Method call, used to prepare the operand stack also receives the call parameters and methods results returned.

  Each location of the operand stack may be any type of value saved in a Java virtual machine definition, including long and double. Operand stack has a stack depth determined by the depth of the stack, long or double occupies two units, other types of data unit occupies a stack depth.

  Data operand stack must be properly operate sequentially, e.g., not two int type data stack, and to operate as long type. A small part of the Java virtual machine instructions can not focus on specific types of data operands, the data all at run-time data area of ​​the type of data to operate as a bare, these instructions can not be used to modify the data, not the original can not be dismantled split the data, the accuracy of these operations will be ensured by mandatory inspection process class file.

  A stack frame overlap area, a portion of data common method is called when convenient, without additional transmission parameter assignment.

3 Dynamic Link

  Each internal pointer to the current stack frame comprises a method of the type where the runtime constant pool of reference, in order to achieve a dynamic link (during each run into direct reference) code of the current method.

4 Method Invocation

 4.1 normal completion

  When the process execution method, the method encounters bytecode instructions returned representation normally. The method of bytecode instructions depending on the type of return value data type. In this case, assume the current stack frame to restore the caller's responsibility, the operand stack after the caller code onto the call at the called method's stack frame of the return value, continue normally.

 4.2 Abnormal completion

  Process execution method, if a virtual machine or experiencing athrow internal bytecode instructions cause the Java Virtual Machine throws an exception to indicate abnormal completion.

Reference: "in-depth understanding of the Java Virtual Machine (Second Edition)," "Java Virtual Machine Specification (Java SE version 8)."

Guess you like

Origin www.cnblogs.com/bigshark/p/11219917.html