JAVA JVM 探索栈和栈帧(二)

JAVA JVM 探索栈和栈帧(二)

01 结合字节码指令理解Java虚拟机栈和栈帧

官网 :https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.6

栈帧:每个栈帧对应一个被调用的方法,可以理解为一个方法的运行空间。

每个栈帧中包括局部变量表(Local Variables)、操作数栈(Operand Stack)、指向运行时常量池的引用(A reference to the run-time constant pool)、方法返回地址(Return Address)和附加信息。

局部变量表:方法中定义的局部变量以及方法的参数存放在这张表中
局部变量表中的变量不可直接使用,如需要使用的话,必须通过相关指令将其加载至操作数栈中作为操作数使用。

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

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

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

Java虚拟机栈和栈帧

public 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();
    }

此时你需要一个能够看懂反编译指令的宝典
比如官网的:https://docs.oracle.com/javase/specs/jvms/se8/html/index.html
当然网上也有很多,大家可以自己查找

执行命令: javap -c -l Person.class
执行结果

Compiled from "Person.java" 

public class Person { 
	... 
	public static int calc(int, int); 
		Code:               //op1(局部变量0,iload_0)  op2(局部变量1,iload_1)  result(局部变量2,iload_2)
							//iconst_3:值为3,iload:局部变量,istore:给值存入,iadd:加法,
			0: iconst_3 	//将int类型常量3压入[操作数栈]   3;    
			1: istore_0 	//将int类型值存入[局部变量0]  3 存入op1对象
			2: iload_0 		//从[局部变量0]中装载int类型值入栈  int op1 
			3: iload_1		//从[局部变量1]中装载int类型值入栈  int op2
			4: iadd 		//将栈顶元素弹出栈,执行int类型的加法,结果入栈  op1+op2;
			【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]中 int result=op1+op2;
			6: iload_2 		//从[局部变量2]中装载int类型值入栈  将result 装载 int类型值入栈
			7: ireturn 		//从方法中返回int类型的数据 return result;
	...  
}

main线程

02 折腾一下

2.1栈指向堆

如果在栈帧中有一个变量,类型为引用类型,比如Object obj=new Object(),这时候就是典型的栈中元素指向堆中的对象。
栈指向堆

2.2 方法区指向堆

方法区中会存放静态变量,常量等数据。如果是下面这种情况,就是典型的方法区中元素指向堆中的对象。

private static Object obj=new Object();

方法区指向堆
思考 :一个对象怎么知道它是由哪个类创建出来的?怎么记录?这就需要了解一个Java对象的具体信息咯

Java对象内存布局

一个Java对象在内存中包括3个部分:对象头、实例数据和对齐填充

Java 对象内存布局

03 内存模型

待更新。。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值