In-depth understanding of the Java virtual machine notes (five) virtual machine bytecode execution engine

Outline

From the exterior view, all of the Java virtual machine execution engine is the same: the input is byte code file, the process is equivalent byte code parsing process, the output is the result.

Runtime stack frame structure

Stack frames are used to support virtual machine data structures and methods for performing the method calls, which is a virtual machine runtime data area of ​​virtual machine stack stack elements.

Stack frames: the local variable table method, the operand stack, and a method of dynamic link return address and other information

Each method call process from the beginning to the completion of the execution, the process corresponds to a stack frame in which the virtual machine from the stack to push the stack

Here Insert Picture Description

Local variable table

The capacity of the local variable table to a variable groove (Variable Slot, hereinafter referred to as Slot) as a minimum unit, the virtual machine specification does not explicitly indicated Slot a memory space to be occupied, which allows Slot length may vary with the processor, operating system, the changes or different virtual machines.

A slot: there are boolean, byte, char, short, int, float, reference and returnAddress

reference type represents a reference to an object instance, virtual machine specification that it is neither the length nor made clear that this reference should be what kind of structure. But in general, the virtual machine should achieve at least by this reference do two things, one is to find references from directly or indirectly to the start of the index in the data objects stored in the Java heap, and second, this reference in directly or indirectly, to find the type of data object belongs to the type of information stored in the method area, or can not achieve the Java language syntax constraint constraint defined in the specification.

returnAddress type now rarely met, it is for the bytecode instructions jsr, jsr_w and ret services, pointing to a byte code instruction address, very old Java virtual machine used to use it a few instructions to implement exception handling now replaced by exception table.

Two slot: long and double

To save space stack frame as local variables in the table can be reused Slot, variables defined within the body method, which does not necessarily cover the entire scope of the method thereof, if the current value of the byte code counter has exceeded the PC scope of a variable, then this variable corresponding Slot can be passed on to other variables. However, such a design in addition to saving space outside the stack frame, will be accompanied by some additional side effects, such as, in some cases, Slot reuse will directly affect the behavior of the garbage collection system

Case 1

/**
*-verbose:gc
**/
public class testOne {
    public static void main(String[] args){
        byte[] placeholder=new byte[64*1024*1024];
        System.gc();
    }
}

Here Insert Picture Description

When the execution is still in the placeholder variable gc within the scope of the virtual machine naturally not going to recover.

Case 2

/**
 * -verbose:gc
 */
public class testTwo {
    public static void main(String[] args) {
        {
            byte[] placeholder = new byte[64 * 1024 * 1024];
        }
        System.gc();
    }
}

Here Insert Picture Description

Case 3

/**
 * -verbose:gc
 */
public class testThree {
    public static void main(String[] args) {
        {
            byte[] placeholder = new byte[64 * 1024 * 1024];
        }
        int a = 0;//可手动将placeholder设为null值来替代
        System.gc();
    }
}

Here Insert Picture Description

The reasons for recycling

Root cause placeholder can be recycled are: whether the local variable table Slot there is also an array of references about placeholder object.

The first modification, although the code has left the scope of the placeholder, but after this, no read or write operation on the local variable table, originally occupied by the placeholder Slot yet other variables are multiplexed, so as GC Roots local variable table that is part remained associated to it. This association was not interrupted in time, in most cases the influence is very slight. However, if you encounter a method that takes some of the code behind a long operation, while the front and defines takes up a lot of memory, variables will not actually be used to manually set it to null value (the variable corresponding to the local variable table empty Slot) is not necessarily an absolute meaningless operation, which may be very special cases (large object occupying memory stack frame time this method can not be recovered, the number of method calls reach JIT compile condition) to use.

But note that the JIT to optimize the operation has been cleared, the second case GC will be garbage collected.

Note also points

Local variables are not initialized twice, so local variables need to use the manual programmer after initialization.

Operand stack

When beginning a method of execution, the operand stack is empty this method, during the execution of the method have a variety of bytecode instructions and extracting content to write the operand stack, i.e., the / stack operation.

Data Type Operand stack elements must exactly match the sequence of bytecode instructions.

Most virtual machine to achieve the will to do some optimization, so that two stack frame appears partially overlap. Shared data can be part of such a method is called, without the need for additional copy transfer parameters.

Here Insert Picture Description

Dynamic Link

Class file constant pool contains a lot of symbolic reference, the method call instruction byte code symbol on a 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, this transformation is called static resolution.

Another part will be converted directly referenced during each operation, this part is called dynamic linking.

Methods return address

When a method begins execution, there are only two ways to exit this method.

  • The first way is to perform any engine encounters a bytecode instruction method returns, this time there may be a method return value to the caller upper (called the current method is a method called caller), and whether there is a return value type of return value will be determined according to what method encounters a return instruction, which quit method is called normal completion exports
  • Another way is to exit, an exception is encountered during execution of the method, and this exception is not handled in vivo, either internal abnormalities produced by a Java virtual machine, or the use of athrow bytecode instruction code generated abnormal, as long as in this method, the exception table no exception handler to search for a match, it will cause the method to quit, quit this kind of method is called abnormal completion exports. One way to use the exception to complete the export way to the exit, will not give it the upper caller produce any return value.

Regardless of the exit way, after the method exits, need to return to the position method is called, the program can
continue, you may need some of the information stored in the stack frame when the method returns, to help restore its upper method of execution
status.

In general, when the method exits normally, the value of the counter PC of the caller as the return address, the stack frame is likely to save the counter values. A method while abnormal exit, the return address is to be determined by the exception handler table, the stack frame information in this section is generally not saved.

Method exit process is actually equivalent to the current stack frame of the stack, so the operation may be performed when there exit: Recovery
local variables and operand stack upper table method, the return value (if any) into the caller operand stack the stack frame
, the adjustment value of the counter PC to point to a method call instruction subsequent instructions and the like.

Method Invocation

Method calls the method execution is not equal to the task method call is to determine the method to be called version (that is, which method)

Resolve

Parsing class loading stage, a portion of which will be converted to direct reference symbol references.

Premise: there is a version called a method that can be determined before the program actually run, and call the version of this method at runtime is immutable. That compiler shows that run immutable

Including static methods and private methods into two categories, they can not override the other versions by inheritance or otherwise

And as long as the method can be invokestatic invokespecial command called, can be determined only in the analysis stage version of the call, in line with the conditions of static methods, private methods, instance constructor, the parent class method Class 4, load them in class time the symbol will be resolved to the reference method of direct reference. These methods may be referred to as a non-virtual methods , in contrast, the other method is called virtual methods (final removal method will be mentioned later).

Non-Java virtual method in addition to the method using invokestatic, invokespecial called Another is the final modification method.

Resolution call must be a static process, it is completely determined at compile time, the symbol will involve all references into a direct reference to the class load can be determined in the resolution phase, will not run again delayed to complete.

The dispatch call may be static or may be dynamic

Assignment

Static assignment

public class StaticDispatch {
    static abstract class Human {
    }

    static class Man extends Human {
    }

    static class Woman extends Human {
    }
    public void sayHello(Human guy){
        System.out.println("guy");
    }
    public void sayHello(Man man){
        System.out.println("man");
    }
    public void sayHello(Woman woman){
        System.out.println("woman");
    }
    public static void main(String[] args){
        Human man=new Man();
        Human woman=new Woman();
        StaticDispatch staticDispatch=new StaticDispatch();
        staticDispatch.sayHello(man);
        staticDispatch.sayHello(woman);
    }

}

Here Insert Picture Description
Here Insert Picture Description

Static type is not changed, the actual type will change many times, Javac compiler will decide which overloaded version to use based on the static type of the parameter

All rely on static type version of the positioning method for performing an operation called static dispatch dispatch, typical application is the method overloading.

But the choice may not be the only heavy-duty versions will choose a more appropriate version

Dynamic assignment

Another important and reflects the multi-state - have great relevance to rewrite

In operation determines the actual type of the recipient

Single and multiple dispatch dispatch

Single dispatch are selected in accordance with a method of the target variables in

Selection of multiple dispatch is more than a certain method VARIABLES

Static dispatch method of selecting a target types and methods based on static parameters, belong to multiple dispatch

Selecting a target polymorphic dispatch method is based on the actual type of the recipient, are single dispatch

Virtual machine to achieve dynamic dispatch

The establishment of virtual method table

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43958969/article/details/95315043