其他分享
首页 > 其他分享> > JVM序列篇三大块来介绍(JVM前世今生、JVM HotSpot架构、性能调优)之JVM Hotspot架构

JVM序列篇三大块来介绍(JVM前世今生、JVM HotSpot架构、性能调优)之JVM Hotspot架构

作者:互联网

JVM HotSpot架构

JVM HotSpot架构思维导图

JVM HotSpot之类加载子系统

一个Java文件从编码完成到最终执行,一般主要包括两个过程

而我们所说的类加载过程即是指JVM虚拟机把.class文件中类信息加载进内存,并进行解析生成对应的class对象的过程。

举个通俗点的例子来说,JVM在执行某段代码时,遇到了class A, 然而此时内存中并没有class A的相关信息,于是JVM就会到相应的class文件中去寻找class A的类信息,并加载进内存中,这就是我们所说的类加载过程。

由此可见,JVM不是一开始就把所有的类都加载进内存中,而是只有第一次遇到某个需要运行的类时才会加载,且只加载一次。

** 下面以HelloWorld.java为类剖析类加载子系统原理**

package com.example.demo;

public class HelloWorld {
    private static  final int a = 1;
    private static int b = 2;
    public static void main(String[] args) {
         int c = 3;
         test1();
         new HelloWorld().test2();
    }

    public static void test1(){
        System.out.println(a);
    }

    public void test2(){
        int f = 5;
        System.out.println(f);
    }
}

windows下生成查看class字节码过程,打开cmd命令行工具:

编译:cd E:\study\sourcecode\demo\target\classes\com\example\demo: javac HelloWorld.java
查看字节码cd E:\study\sourcecode\demo\target\classes\com\example\demo: javap HelloWorld.class > HelloWorld.txt 将生成的字节码输出到HelloWorld.txt

Classfile /E:/study/sourcecode/demo/target/classes/com/example/demo/HelloWorld.class
  Last modified 2021-10-1; size 892 bytes
  MD5 checksum 725ca55458c6d045ad2c25c4b7b8477b
  Compiled from "HelloWorld.java"
public class com.example.demo.HelloWorld
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #9.#34         // java/lang/Object."<init>":()V
   #2 = Methodref          #3.#35         // com/example/demo/HelloWorld.test1:()V
   #3 = Class              #36            // com/example/demo/HelloWorld
   #4 = Methodref          #3.#34         // com/example/demo/HelloWorld."<init>":()V
   #5 = Methodref          #3.#37         // com/example/demo/HelloWorld.test2:()V
   #6 = Fieldref           #38.#39        // java/lang/System.out:Ljava/io/PrintStream;
   #7 = Methodref          #40.#41        // java/io/PrintStream.println:(I)V
   #8 = Fieldref           #3.#42         // com/example/demo/HelloWorld.b:I
   #9 = Class              #43            // java/lang/Object
  #10 = Utf8               a
  #11 = Utf8               I
  #12 = Utf8               ConstantValue
  #13 = Integer            1
  #14 = Utf8               b
  #15 = Utf8               <init>
  #16 = Utf8               ()V
  #17 = Utf8               Code
  #18 = Utf8               LineNumberTable
  #19 = Utf8               LocalVariableTable
  #20 = Utf8               this
  #21 = Utf8               Lcom/example/demo/HelloWorld;
  #22 = Utf8               main
  #23 = Utf8               ([Ljava/lang/String;)V
  #24 = Utf8               args
  #25 = Utf8               [Ljava/lang/String;
  #26 = Utf8               c
  #27 = Utf8               MethodParameters
  #28 = Utf8               test1
  #29 = Utf8               test2
  #30 = Utf8               f
  #31 = Utf8               <clinit>
  #32 = Utf8               SourceFile
  #33 = Utf8               HelloWorld.java
  #34 = NameAndType        #15:#16        // "<init>":()V
  #35 = NameAndType        #28:#16        // test1:()V
  #36 = Utf8               com/example/demo/HelloWorld
  #37 = NameAndType        #29:#16        // test2:()V
  #38 = Class              #44            // java/lang/System
  #39 = NameAndType        #45:#46        // out:Ljava/io/PrintStream;
  #40 = Class              #47            // java/io/PrintStream
  #41 = NameAndType        #48:#49        // println:(I)V
  #42 = NameAndType        #14:#11        // b:I
  #43 = Utf8               java/lang/Object
  #44 = Utf8               java/lang/System
  #45 = Utf8               out
  #46 = Utf8               Ljava/io/PrintStream;
  #47 = Utf8               java/io/PrintStream
  #48 = Utf8               println
  #49 = Utf8               (I)V
{
  public com.example.demo.HelloWorld();
    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 3: 0
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       5     0  this   Lcom/example/demo/HelloWorld;

  public static void main(java.lang.String[]);
    descriptor: ([Ljava/lang/String;)V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=2, args_size=1
         0: iconst_3
         1: istore_1
         2: invokestatic  #2                  // Method test1:()V
         5: new           #3                  // class com/example/demo/HelloWorld
         8: dup
         9: invokespecial #4                  // Method "<init>":()V
        12: invokevirtual #5                  // Method test2:()V
        15: return
      LineNumberTable:
        line 7: 0
        line 8: 2
        line 9: 5
        line 10: 15
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      16     0  args   [Ljava/lang/String;
            2      14     1     c   I
    MethodParameters:
      Name                           Flags
      args

  public static void test1();
    descriptor: ()V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=0, args_size=0
         0: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
         3: iconst_1
         4: invokevirtual #7                  // Method java/io/PrintStream.println:(I)V
         7: return
      LineNumberTable:
        line 13: 0
        line 14: 7

  public void test2();
    descriptor: ()V
    flags: ACC_PUBLIC
    Code:
      stack=2, locals=2, args_size=1
         0: iconst_5
         1: istore_1
         2: getstatic     #6                  // Field java/lang/System.out:Ljava/io/PrintStream;
         5: iload_1
         6: invokevirtual #7                  // Method java/io/PrintStream.println:(I)V
         9: return
      LineNumberTable:
        line 17: 0
        line 18: 2
        line 19: 9
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      10     0  this   Lcom/example/demo/HelloWorld;
            2       8     1     f   I

  static {};
    descriptor: ()V
    flags: ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
         0: iconst_2
         1: putstatic     #8                  // Field b:I
         4: return
      LineNumberTable:
        line 5: 0
}
SourceFile: "HelloWorld.java"

未完待续

标签:java,Utf8,HotSpot,class,HelloWorld,JVM,demo,架构,example
来源: https://www.cnblogs.com/lwdesire/p/15355273.html