【JVM类加载及字节码技术】栈中的执行过程(一)

1、类文件结构

首先获取到**.class**文件
以下就是一段字节码文件:

0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09 
0000020 00 16 00 17 08 00 18 0a 00 19 00 1a 07 00 1b 07 
0000040 00 1c 01 00 06 3c 69 6e 69 74 3e 01 00 03 28 29 
0000060 56 01 00 04 43 6f 64 65 01 00 0f 4c 69 6e 65 4e 
0000100 75 6d 62 65 72 54 61 62 6c 65 01 00 12 4c 6f 63 
0000120 61 6c 56 61 72 69 61 62 6c 65 54 61 62 6c 65 01 
0000140 00 04 74 68 69 73 01 00 1d 4c 63 6e 2f 69 74 63 
0000160 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c 6f 
0000200 57 6f 72 6c 64 3b 01 00 04 6d 61 69 6e 01 00 16 
0000220 28 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 
0000240 69 6e 67 3b 29 56 01 00 04 61 72 67 73 01 00 13 
0000260 5b 4c 6a 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 
0000300 6e 67 3b 01 00 10 4d 65 74 68 6f 64 50 61 72 61 
0000320 6d 65 74 65 72 73 01 00 0a 53 6f 75 72 63 65 46 
0000340 69 6c 65 01 00 0f 48 65 6c 6c 6f 57 6f 72 6c 64
0000360 2e 6a 61 76 61 0c 00 07 00 08 07 00 1d 0c 00 1e 
0000400 00 1f 01 00 0b 68 65 6c 6c 6f 20 77 6f 72 6c 64 
0000420 07 00 20 0c 00 21 00 22 01 00 1b 63 6e 2f 69 74 
0000440 63 61 73 74 2f 6a 76 6d 2f 74 35 2f 48 65 6c 6c 
0000460 6f 57 6f 72 6c 64 01 00 10 6a 61 76 61 2f 6c 61 
0000500 6e 67 2f 4f 62 6a 65 63 74 01 00 10 6a 61 76 61 
0000520 2f 6c 61 6e 67 2f 53 79 73 74 65 6d 01 00 03 6f 
0000540 75 74 01 00 15 4c 6a 61 76 61 2f 69 6f 2f 50 72 
0000560 69 6e 74 53 74 72 65 61 6d 3b 01 00 13 6a 61 76 
0000600 61 2f 69 6f 2f 50 72 69 6e 74 53 74 72 65 61 6d 
0000620 01 00 07 70 72 69 6e 74 6c 6e 01 00 15 28 4c 6a 
0000640 61 76 61 2f 6c 61 6e 67 2f 53 74 72 69 6e 67 3b 
0000660 29 56 00 21 00 05 00 06 00 00 00 00 00 02 00 01 
0000700 00 07 00 08 00 01 00 09 00 00 00 2f 00 01 00 01 
0000720 00 00 00 05 2a b7 00 01 b1 00 00 00 02 00 0a 00 
0000740 00 00 06 00 01 00 00 00 04 00 0b 00 00 00 0c 00 
0000760 01 00 00 00 05 00 0c 00 0d 00 00 00 09 00 0e 00 
0001000 0f 00 02 00 09 00 00 00 37 00 02 00 01 00 00 00 
0001020 09 b2 00 02 12 03 b6 00 04 b1 00 00 00 02 00 0a 
0001040 00 00 00 0a 00 02 00 00 00 06 00 08 00 07 00 0b 
0001060 00 00 00 0c 00 01 00 00 00 09 00 10 00 11 00 00 
0001100 00 12 00 00 00 05 01 00 10 00 00 00 01 00 13 00 
0001120 00 00 02 00 14

根据JVM规范,类文件结构如下:

u4 			   magic(魔数)
u2             minor_version;(小版本)    
u2             major_version;(大版本)    
u2             constant_pool_count;(常量池)    
cp_info        constant_pool[constant_pool_count-1];    
u2             access_flags; (访问标记)   
u2             this_class; (当前类)   
u2             super_class;(父类)   
u2             interfaces_count;(接口个数)    
u2             interfaces[interfaces_count];(接口信息)   
u2             fields_count;(字段个数)    
field_info     fields[fields_count];(字段信息)   
u2             methods_count;(方法个数)    
method_info    methods[methods_count];(方法信息)    
u2             attributes_count;(属性个数)    
attribute_info attributes[attributes_count];(属性信息)

1、魔数:(u4 magic)
对应字节码文件的0~3个字节。(ca fe ba be)

0000000 ca fe ba be 00 00 00 34 00 23 0a 00 06 00 15 09

。。。。。。等等

2、字节码指令

可参考
字节码指令详细内容

javap工具
Oracle 提供了 javap 工具来反编译 class 文件

public class Test2 {    
	public static void main(String[] args) {        
		int a = 10;        
		int b = Short.MAX_VALUE + 1;        
		int c = a + b;        
		System.out.println(c);   
    } 
}

利用javap 反编译后

..../Desktop/java_test/JVM_test/src/test/Test2.class
  Last modified 2021-12-9; size 481 bytes
  MD5 checksum 3f9c28ec903e1e6cbcec8aeccfc200be
  Compiled from "Test2.java"
public class test.Test2
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #7.#18         // java/lang/Object."<init>":()V
   #2 = Class              #19            // java/lang/Short
   #3 = Integer            32768
   #4 = Fieldref           #20.#21        // java/lang/System.out:Ljava/io/PrintStream;
   #5 = Methodref          #22.#23        // java/io/PrintStream.println:(I)V
   #6 = Class              #24            // test/Test2
   #7 = Class              #25            // java/lang/Object
   #8 = Utf8               <init>
   #9 = Utf8               ()V
  #10 = Utf8               Code
  #11 = Utf8               LineNumberTable
  #12 = Utf8               main
  #13 = Utf8               ([Ljava/lang/String;)V
  #14 = Utf8               Exceptions
  #15 = Class              #26            // java/lang/Exception
  #16 = Utf8               SourceFile
  #17 = Utf8               Test2.java
  #18 = NameAndType        #8:#9          // "<init>":()V
  #19 = Utf8               java/lang/Short
  #20 = Class              #27            // java/lang/System
  #21 = NameAndType        #28:#29        // out:Ljava/io/PrintStream;
  #22 = Class              #30            // java/io/PrintStream
  #23 = NameAndType        #31:#32        // println:(I)V
  #24 = Utf8               test/Test2
  #25 = Utf8               java/lang/Object
  #26 = Utf8               java/lang/Exception
  #27 = Utf8               java/lang/System
  #28 = Utf8               out
  #29 = Utf8               Ljava/io/PrintStream;
  #30 = Utf8               java/io/PrintStream
  #31 = Utf8               println
  #32 = Utf8               (I)V
{
  public test.Test2();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=1, locals=1, args_size=1
         0: aload_0
         1: invokespecial #1                  // Method java/lang/Object."<init>":()V
         4: return
      LineNumberTable:
        line 5: 0

  public static void main(java.lang.String[]) throws java.lang.Exception;
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=4, args_size=1
         0: bipush        10
         2: istore_1
         3: ldc           #3                  // int 32768
         5: istore_2
         6: iload_1
         7: iload_2
         8: iadd
         9: istore_3
        10: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        13: iload_3
        14: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        17: return
      LineNumberTable:
        line 8: 0
        line 9: 3
        line 10: 6
        line 11: 10
        line 12: 17
    Exceptions:
      throws java.lang.Exception
}
SourceFile: "Test2.java"
public class Test2 {    
	public static void main(String[] args) {        
		int a = 10;        
		int b = Short.MAX_VALUE + 1;        
		int c = a + b;        
		System.out.println(c);   
    } 
}

图解上面方法的执行流程

在这里我提一下方法区,不同的虚拟机,不同的JDK版本可能方法区的实现不同

  1. 方法区只是JVM的一种规范并不是具体的实现,每一个JVM虚拟机都可以有不同的实现,JAVA官方的HotSpot虚拟机中,在JDK1.8后使用了“元空间”实现JDK1.7使用的是“永久代”
  1. 因为以前永久代经常会发生OOM报错,所以移除了永久代,转而使用元空间,利用的用户的内存空间。
  1. 所以我们可以说JDK1.8的JVM的方法区是元空间。元空间是使用本地内存(Native Memory)实现的,也就是说它的内存是不在虚拟机内的,所以可以理论上物理机器还有多个内存就可以分配,而不用再受限于JVM本身分配的内存了。

运行时常量池也属于方法区,只不过这里单独提出来了。
在这里插入图片描述

1、方法区载入运行时常量池中的字节码文件

(stack=2,locals=4) 对应(stack)操作数栈有2个空间,每个空间4个字节(所以为啥byte,short,char都是转换为int进行计算,因为数栈的长度为4个字节,不够就会自动补齐),(locals)局部变量表中有4个槽位。

在这里插入图片描述

2、执行引擎开始执行字节码
1. bipush 10

将一个byte数据压入操作数栈(长度不够4字节就补齐),一般比较小的数不会放在常量池。
在这里插入图片描述

2. istore 1

操作数栈栈顶元素弹出并且赋值给第二个局部变量( 1 代表第二位),对应代码a = 10
在这里插入图片描述
在这里插入图片描述

3. ldc #3

读取运行时常量池中 #3 ,即32768(超过short最大值范围的数会被放到运行时常量池中),将其加载到操作数栈中。

代码在执行的时候,会有编译器优化,一些简单的运算,会在编译器就计算好。
32768 = Short.MAX_VALUE + 1 在编译期就计算好。

在这里插入图片描述

4. istore 2

操作数栈栈顶元素弹出并且赋值给第三个局部变量( 2 代表第三位),对应代码b = Short.MAX_VALUE + 1
在这里插入图片描述
在这里插入图片描述

5. iload1 iload2

将局部变量表中1号位置和2号位置的元素放入操作数栈中,因为只能在操作数栈中执行运算操作
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

6. iadd

将操作数栈中的两个元素进行相加。
在这里插入图片描述

7. istore 3

将操作数栈栈顶的数据弹出,放入局部变量表3号位置。
在这里插入图片描述
在这里插入图片描述

8. getstatic #4

在运行时常量池中找到#4,发现是一个对象,在堆内存中找到该对象,并将其引用放入操作数栈中,是引用不是原对象看清楚
在这里插入图片描述
在这里插入图片描述

9. iload 3

将局部变量表中3号位置的元素压入操作数栈中。
在这里插入图片描述

10. invokevirtual 5

找到常量池 #5 项,定位到方法区 java/io/PrintStream.println:(I)V 方法,并且生成新的栈帧(分配 locals、stack等),传递参数,执行新栈帧中的字节码。
在这里插入图片描述
执行完毕,弹出println栈帧,清除 main 操作数栈内容。
在这里插入图片描述

11. return

完成 main 方法调用,弹出 main 栈帧,程序结束。


  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值