JVM系列(1)--详解Class加载过程

1. 概述class的loading、linking和initializing

在这里插入图片描述

2. class的加载

  1. jvm采用双亲委派机制,按需加载classloader
  2. class被加载到内存后,class的二进制内容存储在JVM的Method Area,还会生成一个指向该内容的class类对象
  3. 类加载器的四个层次,自顶向下为:
  • Bootstrap: 加载lib里的JDK的最核心的内容,如果调用getClassLoader()得到null时,说明已经到达顶层类加载器
  • Extension: 加载扩展包里的各种文件,扩展包位于JDK安装目录 jre/lib/ext/*.jar
  • Application: 常用的类加载器,用于加载classpath指定的内容
  • CustomClassLoader: 自定义类加载器
    双亲委派机制
  1. 类加载流程:
    class文件加载流程

3. 自定义ClassLoader

1.ClassLoader源码(关键部分)
protected Class<?> loadClass(String name, boolean resolve)
        throws ClassNotFoundException
    {
        synchronized (getClassLoadingLock(name)) {
            // First, check if the class has already been loaded
            Class<?> c = findLoadedClass(name);
            if (c == null) {
                long t0 = System.nanoTime();
                try {
                    if (parent != null) {
                        c = parent.loadClass(name, false);
                    } else {
                        c = findBootstrapClassOrNull(name);
                    }
                } catch (ClassNotFoundException e) {
                    // ClassNotFoundException thrown if class not found
                    // from the non-null parent class loader
                }

                if (c == null) {
                    // If still not found, then invoke findClass in order
                    // to find the class.
                    long t1 = System.nanoTime();
                    c = findClass(name);

                    // this is the defining class loader; record the stats
                    sun.misc.PerfCounter.getParentDelegationTime().addTime(t1 - t0);
                    sun.misc.PerfCounter.getFindClassTime().addElapsedTimeFrom(t1);
                    sun.misc.PerfCounter.getFindClasses().increment();
                }
            }
            if (resolve) {
                resolveClass(c);
            }
            return c;
        }
    }
2. 如何自定义类加载器
  1. 继承ClassLoader
  2. 重写findClass(),在findClass()中调用defineClass(),实现加密逻辑
  3. 如需打破双亲委派,重写loadClass()
  4. 自定义ClassLoader示例如下
public class MyClassLoader extends ClassLoader {

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
    //class文件放在D盘了
        File f = new File("D:/test/", name.replace(".", "/").concat(".class"));
        try {
            FileInputStream fis = new FileInputStream(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int b = 0;

            while ((b=fis.read()) !=0) {
                baos.write(b);
            }

            byte[] bytes = baos.toByteArray();
            baos.close();
            fis.close();

            return defineClass(name, bytes, 0, bytes.length);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.findClass(name); //throws ClassNotFoundException
    }

    public static void main(String[] args) throws Exception {
        ClassLoader classLoader = new MyClassLoader();
        Class clazz = classLoader.loadClass("com.wangxshen.test.HelloWorld");

        HelloWorld h = (HelloWorld)clazz.newInstance();
        h.m();

        System.out.println(classLoader.getClass().getClassLoader());
        System.out.println(classLoader.getParent());
        System.out.println(getSystemClassLoader());
    }
}
3. 何时打破双亲委派?
  1. how? override loadClass()
  2. when?
  • JDK1.2之前,自定义ClassLoader都要重写loadClass()
  • ThreadContextClassLoader可以实现基础类调用实现类代码,通过thread.setContextClassLoader指定
  • 模块化的时候使用热启动,热部署:osgi tomcat有自己的模块指定的classloader,可以load进同名类,打破了双亲委派

4. 混合模式

  1. 解释器:bytecode intepreter
  2. JIT:Just In-Time compiler
  3. 混合模式:混合使用解释器和热点代码编译
  • 开始阶段采用解释执行
  • 热点代码检测:1. 多次被调用的方法(方法计数器检测方法执行频率);2.多次被调用的循环(循环计数器检测循环执行频率);3.进行编译
  1. VM options:
  • -Xmixed:默认为混合模式
  • -Xint:使用解释模式,启动很快,执行稍慢
  • -Xcomp:使用纯编译模式,执行很快,启动很慢

5.Linking

  1. 分为verification、preparation、resolution三步,见概述里的图
  2. 举个栗子,结果是2
public class Test{
    public static void main(String[] args) {
        System.out.println(T.count);
    }
}

class T {
    public static T t = new T(); //default null
    public static int count = 2; //default 0

    private T() {
        count ++;
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值