类加载机制

类加载机制

运行时数据区

类生命周期

类加载器

类加载器负责装入类,搜索网络、jar、zip、文件夹、二进制数据、内存等指定位置的类资源。

一个java程序运行,最少有三个类加载器实例,负责不同类的加载。

验证问题

🌾查看类对应的加载器

🌾JVM如何知道我们的类在何方

🌾类不会重复加载

🌾类的卸载

🌾双亲委派模型

1、查看类对应的加载器

demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.wyj.classloader;

/**
* 查看类的加载器实例
*/
public class ClassLoaderView {
public static void main(String[] args) throws Exception {
// 加载核心类库的 BootStrap ClassLoader
System.out.println("核心类库加载器:"
+ ClassLoaderView.class.getClassLoader().loadClass("java.lang.String").getClassLoader());
// 加载拓展库的 Extension ClassLoader
System.out.println("拓展类库加载器:" + ClassLoaderView.class.getClassLoader()
.loadClass("com.sun.nio.zipfs.ZipCoder").getClassLoader());
// 加载应用程序的
System.out.println("应用程序库加载器:" + ClassLoaderView.class.getClassLoader());

// 双亲委派模型 Parents Delegation Model
System.out.println("应用程序库加载器的父类:" + ClassLoaderView.class.getClassLoader().getParent());
System.out.println(
"应用程序库加载器的父类的父类:" + ClassLoaderView.class.getClassLoader().getParent().getParent());
}
}

console

1
2
3
4
5
核心类库加载器:null
拓展类库加载器:sun.misc.Launcher$ExtClassLoader@2a84aee7
应用程序库加载器:sun.misc.Launcher$AppClassLoader@18b4aac2
应用程序库加载器的父类:sun.misc.Launcher$ExtClassLoader@2a84aee7
应用程序库加载器的父类的父类:null
2、JVM如何知道我们的类在何方

class信息存放在不同的位置,桌面jar、项目bin目录、target目录等待…

查看openjdk源代码:sun.misc.Launcher.AppClassLoader

结论:读取java.class.path配置,指定去哪些地址加载类资源

验证过程:利用jps、jcmd两个命令

1、jps查看本机JAVA进程

2、查看运行时配置:jcmd进程号 VM.system_properties

查看过程

先运行demo

打开终端

就是通过这个配置信息,让JVM能够从目录里读数据

3、类不会重复加载

类的唯一性:同一个类加载器,类名一样,代表是同一个类。

识别方式:ClassLoader Instance id + PackageName + ClassName

验证方式:使用类加载器,对同一个class类的不同版本,进行多次加载,检查是否会加载到最新的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.wyj.classloader;

import java.net.URL;
import java.net.URLClassLoader;

/**
* 指定class 进行加载e
*/
public class LoaderTest {
public static void main(String[] args) throws Exception {
URL classUrl = new URL("file:D:\\");//jvm 类放在位置

URLClassLoader parentLoader = new URLClassLoader(new URL[]{classUrl});

while (true) {

// 问题:静态块触发
Class clazz = loader.loadClass("HelloService");
System.out.println("HelloService所使用的类加载器:" + clazz.getClassLoader());

Object newInstance = clazz.newInstance();
Object value = clazz.getMethod("test").invoke(newInstance);
System.out.println("调用getValue获得的返回值为:" + value);

Thread.sleep(3000L); // 1秒执行一次
System.out.println();

// help gc -verbose:class
newInstance = null;
loader = null;

}

// System.gc();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/** 类加载测试类 */
public class HelloService {

```
public static String value = getValue();

static {
System.out.println("静态代码块运行");
}

private static String getValue() {
System.out.println("静态方法被运行");
return "netease";
}

public void test() {
System.out.println("hello.." + value);
}
```

}

console

1
2
3
4
5
6
7
8
9
10
11
12
HelloService所使用的类加载器:java.net.URLClassLoader@279f2327
静态方法被运行
静态代码块运行
hello..netease
调用getValue获得的返回值为:null

HelloService所使用的类加载器:java.net.URLClassLoader@5caf905d
静态方法被运行
静态代码块运行
hello..netease
调用getValue获得的返回值为:null
...

程序运行过程中,修改system的值发现,输出并没有改变,证明类不会重复加载

4、类的卸载

验证方式:jvm启动中增-verbose:class参数,输出类记载和卸载的日志信息

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.wyj.classloader;

import java.net.URL;
import java.net.URLClassLoader;

/**
* 指定class 进行加载e
*/
public class LoaderTest {
public static void main(String[] args) throws Exception {
URL classUrl = new URL("file:/Users/wanyajing/Desktop/");//jvm 类放在位置

URLClassLoader parentLoader = new URLClassLoader(new URL[]{classUrl});

while (true) {
if (parentLoader == null) break;

// 问题:静态块触发
Class clazz = parentLoader.loadClass("HelloService");
System.out.println("HelloService所使用的类加载器:" + clazz.getClassLoader());

Object newInstance = clazz.newInstance();
Object value = clazz.getMethod("test").invoke(newInstance);
System.out.println("调用getValue获得的返回值为:" + value);

Thread.sleep(3000L); // 1秒执行一次
System.out.println();

// help gc -verbose:class
newInstance = null;
parentLoader = null;

}

System.gc();
Thread.sleep(10000L);
}
}

console

5、双亲委派模型

上面的LoaderTest类,会发现修改类的内容的时候,重新javac HelloService.java后,类不会被重新加载

将URLClassLoader loader = new URLClassLoader(new URL[]{classUrl});这段加载代码放到while循环里面后,就能马上加载到最新的代码,这就是很多很多框架中所说的热加载功能。一个class不能被一个加载器重复加载,所以每次加载的时候都创建一个新的加载器。一旦检测到代码有变化就创建一个新的类加载器,这是热加载。例如tomcat中加载jsp就是利用这个方法实现热加载。

修改demo为双亲委派模型

发现热加载功能消失了,修改类再重新编译后,运行结果没有发生变化,这是为什么?

这就是双亲委派模型的功能,随意每次运行代码的时候都创建了一个新的类加载器,但是实际上每个一直都是同一个加载器parentLoader在加载,而parentLoader一直都没有重复加载,所以无法热加载。