5. The method of stack frame runtime

Method 5 Stack frame runtime

  • Stack frame is a data structure of a virtual machine for executing the call stack of the virtual machine is a stack of elements in the stack.
  • A stack frame of a method of storing a local variable table , the operand stack , dynamic link , the method returns the address information and the like.
  • Calls and returns each method corresponds with a push and pop operations in a virtual machine stack frame of the stack.
  • Stack frame size of the local variable table , the depth of the operand stack are identified at compile time. Thus the memory stack frame size has been determined.
  1. Local variable table

    • Storing a local variable table method parameters and local variables defined in the method .
    • Local variable table to a variable slot (Slot) as the basic unit , the size of the slot is usually 32/64, depending on the particular implementation.
    • double, long may be present in two slot.
    • slot存储:boolean,byte,char,short,int,float,reference,returnAddress,long,double。
      • reference representative of a reference to the object, which can be found directly or indirectly by an object in the heap location and the type of storage object belongs in the process zone information type .
    • Completion method performed by the local variable table values to the variable argument lists pass process. Method for non-static instance of a storage slot of the object (i.e. this reference).
    • slot can be reused , after the variables can be stored in the slot beyond the scope of the variable.
    • Local variables exist preparatory stage (accompanied by default initial value), unlike class variables, and therefore it must be initialized .
  2. Operand stack

    • jvm execution engine is the stack structure, the use of the operand stack to store the number of operation instruction currently being executed.
    • For example, an add operation corresponding to: the top two stack elements of the stack, after the addition, the results stack.
    • Element stored in the operand stack must strictly correspond to the bytecode instructions.
  3. Methods return address

    • The method exits only two ways:
      • Method returns the command: There may return value to the upper method caller.
      • Unhandled exception: there will be no return value to the upper caller.
    • Equivalent to the current stack frame from the stack when the method exits:
      1. The method of recovery of the upper table of local variables, an operand stack.
      2. And to some return value onto the operand stack of the caller.
      3. Adjust the PC register.
  4. Method Invocation

    • Each stack frame contains a pointer to the runtime constant pool references the stack frame belongs approach, this reference method used to implement dynamic link call.

    • Which is used to confirm the call method. class files compiled with only save the symbol method of calling references. Therefore, you need to load the class / run to determine the method of direct reference.

    • Direct confirmation class loading phase reference :( obtained directly referenced process is called parsing)

      • Such processes must be run without change
      • Mainly: static methods, private methods, instance constructor, super method, final approach.
      • Such methods can not override the other versions by inheritance or otherwise. Parsing stage version will be uniquely determined call.
    • Direct reference to confirm the runtime :( called dispatch)

      • Static assignment :

        • java in method overloading is a static assignment, static assignment occurs during compilation.

        • Method overloading rely on static type parameters to locate method.

          • Human man=new Man();
            //Human是静态类型,是编译器可知的。
            //Man是实际类型,是运行时才可知的。
            void say(Human human){}
            void say(Man man){}
            //若调用上述两个重载的方法
            say(man);//根据man的静态类型是Human则调用的实际上是第一个方法
            say((Man)man);//强制类型转换导致了man的静态类型变换调用的是第二个
        • Static type matching order: char-> int-> long-> float-> double-> Packaging -> parent class or interface -> vararg

      • Dynamic dispatch :

        • java the rewriting process is a dynamic assignment.

        • To call an object method, for example:

          • By looking for the actual type of the object, a method to find the actual type of
            • Found: judge Access
            • I did not find: be looking at all of its parent class.
        • Therefore polymorphism, the object calls a method overridden is a priority calls itself rewritten version, followed by the parent class.

        • Because every time you need to find dynamic link so creating a data structure called a virtual method table in the process area. Looking to improve efficiency.

        • It is not all methods of records in the table. It is a dynamic dispatch service, constructors and static methods and private methods are not recorded.

Published 27 original articles · won praise 1 · views 900

Guess you like

Origin blog.csdn.net/hu853996234/article/details/103771128