zoukankan      html  css  js  c++  java
  • 深入理解JVM(一)类加载器部分、类变量、常量、jvm参数

    类加载概述

    1. 在java代码中,类型加载、连接初始化过程都是在程序运行期间完成的

    类型:class、interface(object本身)、类型可在运行期间生成,如动态代理。一种runting概念

    加载:最常见的方式是将已经存在的类的字节码文件(.class文件)从磁盘上加载到内存中;

    连接:将类与类之间的关系确立下来,对于字节码的一些相关处理、校验在连接阶段完成

    初始化:对于一些静态的变量进行复制

    过程不是一定按照上述顺序,按照规范即可

    1. 提供了更大的灵活性,增加了更多的可能性

    本身是静态类型的语言,但是很多特点又使得java具有动态的特点

    类加载器深入剖析

    • Java虚拟机与程序生命周期: 在下面的情况下,Java虚拟机将结束生命周期

    执行了System.exit()方法

    程序正常结束

    程序在执行过程中遇到了异常或者错误而异常终止

    由于操作系统出现错误而导致Java虚拟机进程终止

    类的加载、连接、初始化进一步介绍

    • 加载:查找并加载类的二进制数据

    class文件(不一定是文件 也许来源网络 数据库等) 加载进内存

    • 连接
      • 验证:确保被加载的类的正确性
      • 准备:为类的静态变量分配内存,并将其初始化为默认值

    此时类的实例对象还没创建

    //假设代码中有
    class Test{
        public static int a = 1; 
    }
    //在准备这个阶段,虚拟机在准备阶段不会将1赋值给a,它会为a分配内存并且把0赋值给a
    
    
      • 解析:把类中的符号引用转换为直接引用

    符号引用:间接的引用方式

    直接引用:直接将引用的对象指向内存中的位置

    • 初始化:为类的静态变量赋予正确的初始化值
    //假设代码中有
    class Test{
        public static int a = 1; 
    }
    //在准备阶段,虚拟机在准备阶段不会将1赋值给a,它会为a分配内存并且把0赋值给a
    //在初始化阶段,将a=0替换为a=1
    

    类的使用与卸载

    • 类的使用

    创建对象、使用方法

    • 类的卸载

    从内存中销毁,osgi


    类加载

    主动使用与被动使用

    所有的java虚拟机实现 必须在每个类或接口被java程序“首次主动使用”时才初始化它们

    • java程序的使用方式分为两种
    1. 主动使用(七种)
    • 创建类的实例

    • 访问某个类或接口的静态变量,或者对该静态变量赋值(助记符:getstatic,putstatic)

    • 调用类的静态方法(助记符:invokestatic)

    • 反射(如Calss.forName("com.test.Test"))

    • 初始化一个类的子类

    初始化一个类的时候,如果该类有父类,也会对父类进行初始化,如果父类也有父类,也会往上进行初始化

    • Java虚拟机启动时被标记为启动类的类(Java Test)

    • JDK1.7开始提供的动态语言支持

    java.lang.invoke.MethodHandle实例的解析结果 REF_getStatic, REF_putStatic,REF_invokeStatic句柄对应的类没有初始化,则初始化

    1. 被动使用
    • 除了上述其中情况,其他使用Java类的方式都被看作是对类的被动使用,都不会导致类的初始化

    被动使用也许会加载这个类,只是不进行初始化

    类的加载

    • 类的加载

    类的加载是指将类的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在内存中创建一个java.lang.Class对象(规范并未说明Class对象位于哪里,HotSpot虚拟机将其放在方法区内)用来封装类在方法区内的数据结构

    加载.class文件的方式

    1. 从本地文件系统中直接加载(最常用)
    2. 通过网络下载.class文件
    3. 从zip,jar等归档问价那种加载.calss文件
    4. 从专有的数据库中提取.calss文件
    5. 将Java源文件动态编译为.calss文件(动态代理,运行期创建,web开发会用到)

    举例

    eg1:
    public class MyTest {
    
        public static void main(String[] args) {
            System.out.println(MyChild1.str1);
        }
    
    }
    
    class MyParent1 {
        public static String str1 = "hello world str1";
        static {
            System.out.println("MyParent1 static block");
        }
    }
    
    class MyChild1 extends MyParent1 {
        public static String str2 = "hello world str2";
        static {
            System.out.println("MyChiled1 static block");
        }
    }
    
    //输出结果:
    MyParent1 static block
    hello world str1
    

    对于静态字段来说,只有直接定义了该字段的类才会被初始化。所以在本例中,直接使用的是MyParent1的str1,所以会对MyParent1进行初始化,不会对MyChild1进行初始化,对MyParent1是主动使用

    符合上述所有的java虚拟机实现必须在每个类或接口被java程序“首次主动使用”时才初始化它们

     eg2:
    public class MyTest {
    
        public static void main(String[] args) {
            System.out.println(MyChild1.str2);
        }
    
    }
    
    class MyParent1 {
        public static String str1 = "hello world str1";
        static {
            System.out.println("MyParent1 static block");
        }
    }
    
    class MyChild1 extends MyParent1 {
        public static String str2 = "hello world str2";
        static {
            System.out.println("MyChiled1 static block");
        }
    }
    //输出结果:
    MyParent1 static block
    MyChiled1 static block
    hello world str2
    

    初始化一个子类时,要求其父类全部已经初始化,在本例中,直接使用的是MyChild1的str2,所以会对MyChild1进行初始化,但是由上述的主动使用中初始化一个类的子类可知,对于父类也会进行初始化。(每个类只会被初始化一次)

    所以本例中会先初始化MyParent1在初始化MyChild1,最后再执行输出MyChild1.str2操作

    eg3:
    //执行主类的 VM arguments 中 添加 -XX:+TraceClassLoading 
    //打印类加载信息
    public class MyTest {
    
        public static void main(String[] args) {
            System.out.println(MyChild1.str1);
        }
    
    }
    
    class MyParent1 {
        public static String str1 = "hello world str1";
        static {
            System.out.println("MyParent1 static block");
        }
    }
    
    class MyChild1 extends MyParent1 {
        public static String str2 = "hello world str2";
        static {
            System.out.println("MyChiled1 static block");
        }
    }
    //输出结果
    [Loaded java.lang.Object from shared objects file]
    [Loaded java.io.Serializable from shared objects file]
    ...
    [Loaded java.security.UnresolvedPermission from shared objects file]
    [Loaded java.security.BasicPermissionCollection from shared objects file]
    [Loaded cn.lillcol.classloader.MyTest from file:/E:/lteworkspace/gaSvr/target/classes/]
    [Loaded sun.launcher.LauncherHelper$FXHelper from shared objects file]
    [Loaded java.lang.Class$MethodArray from shared objects file]
    [Loaded java.lang.Void from shared objects file]
    [Loaded cn.lillcol.classloader.MyParent1 from file:/E:/lteworkspace/gaSvr/target/classes/]
    [Loaded cn.lillcol.classloader.MyChild1 from file:/E:/lteworkspace/gaSvr/target/classes/]
    MyParent1 static block
    hello world str1
    [Loaded java.lang.Shutdown from shared objects file]
    [Loaded java.lang.Shutdown$Lock from shared objects file]
    

    加载的第一个类是java.lang.Object,它是所有类的父类

    虽然 MyChild1 没有初始化,但是虚拟机还是加载了MyChild1

    与我们编写的相关类加载顺序MyTest、MyParent1、MyChild1,其中MyTest类为Java虚拟机启动时被标记为启动类的类

    eg4:
    public class MyTest {
        static {
            System.out.println("MyTest static block");
        }
    
        public static void main(String[] args) {
            System.out.println(MyChild1.str1);
        }
    
    }
    
    class MyParent1 {
        public static String str1 = "hello world str1";
        static {
            System.out.println("MyParent1 static block");
        }
    }
    
    class MyChild1 extends MyParent1 {
        public static String str2 = "hello world str2";
        static {
            System.out.println("MyChiled1 static block");
        }
    }
    //输出结果:
    [Loaded cn.lillcol.classloader.MyTest from file:/E:/lteworkspace/gaSvr/target/classes/]
    [Loaded sun.launcher.LauncherHelper$FXHelper from shared objects file]
    [Loaded java.lang.Class$MethodArray from shared objects file]
    [Loaded java.lang.Void from shared objects file]
    MyTest static block
    [Loaded cn.lillcol.classloader.MyParent1 from file:/E:/lteworkspace/gaSvr/target/classes/]
    [Loaded cn.lillcol.classloader.MyChild1 from file:/E:/lteworkspace/gaSvr/target/classes/]
    MyParent1 static block
    hello world str1
    [Loaded java.lang.Shutdown from shared objects file]
    [Loaded java.lang.Shutdown$Lock from shared objects file]
    

    MyTest 加载顺序在MyParent1、MyChild1之前,为Java虚拟机启动时被标记为启动类的类

    eg5:
    public class MyTest2 {
    
        public static void main(String[] args) {
    
            System.out.println(MyParent2.str2);
        }
    
    }
    
    class MyParent2 {
        public static final String str2 = "hello world";
        static {
            System.out.println("MyParent2 static block");
        }
    }
    //输出结果
    hello world
    

    常量在编译阶段,会被存入到调用这个常量的方法所在的类常量池中.

    本质上,调用类并没有直接引用到定义常量的类,因此并不会触发定义常量的类初始化

    注意:这里指的是将常量str2存放到MyTest2的常量池中,之后MyTest2与MyParent2就没有任何关系了,甚至此时将MyParent2的.class文件删除也没有关系

    eg6:
    public class MyTest3 {
        public static void main(String[] args) {
            System.out.println(MyParent3.str3);
        }
    }
    class MyParent3{
        public static final String str3= UUID.randomUUID().toString();
        static {
            System.out.println("MyParent3 static block ");
        }
    }
    //输出结果:
    MyParent3 static block 
    62322324-7b63-49c8-a8c4-f6607421f7ff
    

    如果一个常量值不是在编译期间可以确定,那么其值就不会放到调用类的常量池中

    这时在运行程序的时候,会导致主动使用这个常量所在的类,显然会导致这个类被初始化

    eg7:在终端下用 javap -c 命令反编译MyTest2的.calss文件
    javap -c MyTest2
    
    #输出结果
    Compiled from "MyTest2.java"
    public class cn.lillcol.classloader.MyTest2 {
      public cn.lillcol.classloader.MyTest2();
        Code:
           0: aload_0
           1: invokespecial #8                  // Method java/lang/Object."<init>":()V
           4: return
    
      public static void main(java.lang.String[]);
        Code:
           0: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
           3: ldc           #22                 // String hello world
           5: invokevirtual #24                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
           8: return
    }
    

    助记符:ldc 表示将int、float或是String类型的常量值从常量池中推送至栈顶

    eg8:将代码作如下修改
    public class MyTest2 {
    
        public static void main(String[] args) {
    
            System.out.println(MyParent2.s);
        }
    
    }
    
    class MyParent2 {
        public static final String str2 = "hello world";
        public static final short s = 7;
        static {
            System.out.println("MyParent2 static block");
        }
    }
    //输出:
    7
    
    //反编译结果:
    Compiled from "MyTest2.java"
    public class cn.lillcol.classloader.MyTest2 {
      public cn.lillcol.classloader.MyTest2();
        Code:
           0: aload_0
           1: invokespecial #8                  // Method java/lang/Object."<init>":()V
           4: return
    
      public static void main(java.lang.String[]);
        Code:
           0: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
           3: bipush        7
           5: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
           8: return
    }
    
    

    助记符:bipush 表示将单字节(-128 - 127)的常量值从常量池中推送至栈顶

     eg9:将代码作如下修改
    public class MyTest2 {
    
        public static void main(String[] args) {
    
            System.out.println(MyParent2.i);
        }
    
    }
    
    class MyParent2 {
        public static final String str2 = "hello world";
        public static final short s = 7;
        public static final int i = 128;
        static {
            System.out.println("MyParent2 static block");
        }
    }
    //输出:
    128
    //反编译结果:
    Compiled from "MyTest2.java"
    public class cn.lillcol.classloader.MyTest2 {
      public cn.lillcol.classloader.MyTest2();
        Code:
           0: aload_0
           1: invokespecial #8                  // Method java/lang/Object."<init>":()V
           4: return
    
      public static void main(java.lang.String[]);
        Code:
           0: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
           3: sipush        128
           6: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
           9: return
    }
    

    助记符:sipush 表示将单字节(-32768 - 32767)的常量值从常量池中推送至栈顶

    eg10:将代码作如下修改
    public class MyTest2 {
    
        public static void main(String[] args) {
    
            System.out.println(MyParent2.m);
        }
    
    }
    
    class MyParent2 {
        public static final String str2 = "hello world";
        public static final short s = 7;
        public static final int i = 128;
        public static final int m = 0;
        static {
            System.out.println("MyParent2 static block");
        }
    }
    //输出:
    0
    //反编译结果:
    Compiled from "MyTest2.java"
    public class cn.lillcol.classloader.MyTest2 {
      public cn.lillcol.classloader.MyTest2();
        Code:
           0: aload_0
           1: invokespecial #8                  // Method java/lang/Object."<init>":()V
           4: return
    
      public static void main(java.lang.String[]);
        Code:
           0: getstatic     #16                 // Field java/lang/System.out:Ljava/io/PrintStream;
           3: iconst_0
           4: invokevirtual #22                 // Method java/io/PrintStream.println:(I)V
           7: return
    }
    

    助记符:iconst_0 表示将int类型1从常量池中推送至栈顶(iconst_m1 - iconst_5)

    助记符存储的位置:jdk的rt.jar中

    eg11:
    public class MyTest4 {
        public static void main(String[] args) {
            MyParent4[] myTest4s = new MyParent4[1];
            System.out.println(myTest4s.getClass());
    
            MyParent4[][] myTest4s1 = new MyParent4[1][1];
            System.out.println(myTest4s1.getClass());
    
            System.out.println(myTest4s.getClass().getSuperclass());
            System.out.println(myTest4s1.getClass().getSuperclass());
            
            int[] ints=new int[1];
            System.out.println(ints.getClass());
            System.out.println(ints.getClass().getSuperclass());
        }
    }
    class MyParent4{
        static {
            System.out.println("MyParent4 static block");
        }
    }
    //输出:
    class [Lcom.aaa.test.MyParent4;
    class [[Lcom.aaa.test.MyParent4;
    class java.lang.Object
    class java.lang.Object
    class [I
    class java.lang.Object
    

    对于数组实例来说,其类型是由JVM在运行期间动态生成的,表示为class [Lcom.aaa.test.MyParent4;这种形式,动态生成的类型其父类是java.lang.Object。

    对于数组来说,JavaDoc经常将构成数组的元素称为Component,实际上就是将数组降低一个维度后的类型。

    eg12:
            int[] ints=new int[1];
            System.out.println(ints.getClass());
    
            short[] showts=new short[1];
            System.out.println(showts.getClass());
    
            boolean[] booleans=new boolean[1];
            System.out.println(booleans.getClass());
    
            char[] chars=new char[1];
            System.out.println(chars.getClass());
    
            byte[] bytes=new byte[1];
            System.out.println(bytes.getClass());
    
            float[] floats=new float[1];
            System.out.println(floats.getClass());
    
            double[] doubles=new double[1];
            System.out.println(doubles.getClass());
    //输出:
    class [I
    class [S
    class [Z
    class [C
    class [B
    class [F
    class [D
    

    助记符:

    anewarray: 表示创建一个引用类型(如类、接口、数组)数组,并将其引用值压入栈顶

    ewarray:表示创建一个指定的原始类型(如int、float、char等)的数组,并将其引用值压入栈顶

    JVM参数

    -XX:+<option>,表示开启option选项
    -XX:-<option>,表示关闭option选项
    -XX:<option>=<value>,表示将option选项的值设置value
    

    +、-是因为有些参数默认开启或者关闭

    本文为学习张龙老师深入理解JVM的笔记与心得,转载请注明出处!!!

  • 相关阅读:
    对于bilibili主页head部分的代码的总结以及疑问。
    小项目使用框架(B/S),简单实现mvc分离
    petshop4学习_重构DataList实现分页
    照书抄了个小的sniffer。不过有问题测试不能通过,请求各位帮帮我
    《灵魂的迷茫》
    vs2005中gridview的RowCommand事件
    快速得到google的Gmail(已申请的免看)
    命令行下重启/启动停止IIS服务器的命令
    ES6之const命令
    不容忽视的js面试题
  • 原文地址:https://www.cnblogs.com/lillcol/p/11129928.html
Copyright © 2011-2022 走看看