JVM study notes (c) understand the virtual machine stack and stack frames

 

5. understand the Java virtual machine stack and stack frames

Official website

Stack frame: for each stack frame corresponds to a method to be invoked, may be understood as a method of operating space.

Each stack frame includes a local variable table (Local Variables), the operand stack (Operand Stack), runtime constant reference point (the run-time constant pool), the method returns the address (Return Address) and the additional information.

局部变量表:方法中定义的局部变量以及方法参数存放在这张表中。

局部变量表中的变量不可直接使用,如需使用,必须通过相关指令将其加载至**操作数栈**中作为操作数使用。

操作数栈: 以压栈和出栈的方式存储操作数

动态链接: 每一个栈帧都包含一个指向运行时常量池中该栈帧所属方法的引用,持有这个引用是为了支持方法调用过程中的动态链接。

方法返回地址:当一个方法开始执行后,只有两种方式可以退出,一、遇到方法返回的字节码指令;二、遇见异常,并且这个异常没有在方法内得到处理。

5.1 source code and compiled code

 

class Person{ 
private String name="Jack"; 
private int age; 
private final double salary=100; 
private static String address; 
private final static String hobby="Programming"; 
public void say(){ 
System.out.println("person say..."); 
}
public static int calc(int op1,int op2){ 
op1=3; 
int result=op1+op2; 
return result; 
}
public static void order(){ 
}
public static void main(String[] args){ 
calc(1,2); 
order(); 
} 
}

After compiling the code, see the official website to explain the term

Compiled from "Person.java" 
class Person { 
... 
public static int calc(int, int); 
Code:
0: iconst_3 //将int类型常量3压入[操作数栈] 
1: istore_0 //将int类型值存入[局部变量0] 
2: iload_0 //从[局部变量0]中装载int类型值入栈 
3: iload_1 //从[局部变量1]中装载int类型值入栈 
4: iadd //将栈顶元素弹出栈,执行int类型的加法,结果入栈 
【For example, the iadd instruction (§iadd) adds two int values together. It 
requires that the int values to be added be the top two values of the operand stack, pushed 
there by previous instructions. Both of the int values are popped from the operand stack. 
They are added, and their sum is pushed back onto the operand stack. Subcomputations may be 
nested on the operand stack, resulting in values that can be used by the encompassing 
computation.】 
5: istore_2 //将栈顶int类型值保存到[局部变量2]中 
6: iload_2 //从[局部变量2]中装载int类型值入栈 
7: ireturn //从方法中返回int类型的数据 
... 
}
​

 

5.2 illustrates a stack frame run

 

 

5.3 points to the stack heap

There is a variable stack frame type is a reference type, for example: Object obj = new Object();this is a typical stack of elements pointing to objects in the heap.

5.4 point to heap method area

The method area to store static variables, constants, and other data. example:

private static Object obj = new Object();

 

5.5 point to heap method area

The method area will contain such information, there will heap object, then the object is to know how that class created?

Question: how an object know that it was created out of what class? How record? This requires a slightly specific information Java objects.


When I let go of what I am , I become what I might be.
Out of the comfort zone, meet better themselves.

Published 91 original articles · won praise 63 · views 180 000 +

Guess you like

Origin blog.csdn.net/qq_38423105/article/details/104710300
Recommended