The latest version of jdk15 JVM class loader

The official account -JavaEdge 2021-01-21 03:41:31
latest version jdk15 jdk jvm

  Click on the above “JavaEdge”, Official account

Set to “ Star standard ”, Don't miss a good article !

1 Class loader

In the class loader family, there is a power hierarchy similar to human society :

1.1 Bootstrap

from C/C++ Realization , Start class loader , It's at the top ,JVM Create at startup , Usually by and os Related local code implementation , Is the most basic class loader .

JDK8 when

It should be noted that ,Bootstrap ClassLoader Smart loading a class library with a specific name , such as rt.jar. That means we customize jar Throw it <JAVA_HOME>\jre\lib And it won't be loaded .

Responsible for <JAVA_ HOME>/jre/lib or - Xbootclasspath In the path specified by the , And the class library identified by virtual machine is loaded into memory ( Identify by name , such as rt.jar, Do not load unrecognized files ), such as :

  • Object

  • System

  • String

  • Java Runtime rt.jar etc. jar package

  • System attribute sun.boot.class.path Of a specific name in a specified directory jar package

stay JVM Startup time , adopt Bootstrap ClassLoader load rt.jar, And initialization sun.misc.Launcher Thus create Extension ClassLoader and Application ClassLoader Example .
see Bootstrap ClassLoader Which class libraries are initialized :

URL[] urLs = Launcher.getBootstrapClassPath().getURLs();
for (URL urL : urLs) {

JDK9 after

Responsible for loading the basic module class at startup , such as :

  • java.base


  • java.xml

1.2 Platform ClassLoader

JDK8 when Extension ClassLoader

There is only one example , from sun.misc.Launcher$ExtClassLoader Realization :

  • Responsible for loading <JAVA_HOME>\lib\ext or java.ext.dirs All class libraries in the path specified by the system variable

  • Load some extended system classes , such as XML、 encryption 、 Compress related function classes, etc

JDK9 Replace with platform class loader

Load some platform related modules , such as java.scriptingjava.compiler*、 java.corba*.

Then why 9 It was abolished and replaced ?

JDK8 The main loading of jre lib Of ext, Expand jar Use when package , This is not recommended , So abolish it . and JDK9 With modularity , There's no need for this kind of extended loader .

1.3 Application ClassLoader

There is only one example , from sun.misc.Launcher$AppClassLoader Realization .

JDK8 when

Responsible for loading system environment variables ClassPath Or system properties java.class.path All class libraries in the specified directory .
If you don't define your own loader in your application , The loader is also the default class loader . The loader can pass through java.lang.ClassLoader.getSystemClassLoader obtain .

JDK9 after

Application class loader , For loading application level modules , such as :

  • jdk.compiler

  • jdk.jartool

  • jdk.jshell

  • classpath All class libraries in the path

second 、 The three-tier loader is Java Language implementation , Users can also

1.4 Custom class loaders

User defined loader , yes java.lang.ClassLoader Subclasses of , The user can customize the loading method of the class ; It's just that the order of loading a custom class loader is at the end of all system class loaders .

1.5 Thread Context ClassLoader

Each thread has a classloader (jdk 1.2 After the introduction of ), be called Thread Context ClassLoader, If the thread was not set when it was created , By default, one... Is inherited from the parent thread , If it is not set in the global application , Then all Thread Context ClassLoader by Application ClassLoader. It can be done by Thread.currentThread().setContextClassLoader(ClassLoader) To set up , adopt Thread.currentThread().getContextClassLoader() To get .

What's the use of thread context loaders ?

This class loader allows the parent class loader to load the required class libraries through the child class loader , That is to say, it breaks the parental delegation model that we will talk about later .
What are the benefits ?
Using thread context loader , We can implement all the code hot replacement , Thermal deployment ,Android The same is true of the principle of thermal renewal in .

2 Verify class loader

2.1 Look at the local class loader

stay JDK8 Environment , The results are as follows

AppClassLoader Of Parent by Bootstrap, It's through C/C++ Realized , It doesn't exist in JVM In system , So the output is null.

Class loader features

  • Class loaders don't have to wait for a class " First active use of ” Load it when it's loaded ,JVM The specification allows class loaders to preload a class when they expect it to be used

  • Java The program cannot directly refer to the bootloader , Set up directly classLoader by null, The boot loader is used by default

  • If at load time .class Missing file , Will be notified when the class is first actively used LinkageError error , If it hasn't been used , You can't report an error

  • If no parent loader is specified , The default is to start the loader

  • Each class loader has its own namespace , The namespace consists of classes loaded by the loader and all its parent loaders . Different namespace , The full pathname of the class can be the same

  • Runtime packages consist of classes of the same class loader , Determine whether two classes belong to the same runtime package , Not only depends on whether the full pathnames are the same , It also depends on whether the definition of class loaders is the same . Only classes belonging to the same runtime package can be visible within each other

The current classloader at the lower level , You cannot override classes that are already loaded by higher-level class loaders
If the low-level classloader wants to load an unknown class , Ask up and up very politely :“ Excuse me, , Has this class been loaded ?”
The high-level subclass loader being asked will ask itself two questions

  • Have I loaded this class

  • without , Can I load this class

Only if the answer to both questions is “ no ” when , The current class loader can load the unknown class
The green arrow on the left asks step by step if this class has been loaded , until Bootstrap ClassLoader, Then try to load this class level by level , If it doesn't load , Notify the current classloader that initiated the load request , Allow loading
In the three little tabs on the right , The representative class libraries that are mainly loaded by the class loader in this layer are listed , In fact, it's more than that

You can see through the following code Bootstrap All loaded class libraries

Execution results

Bootstrap The loading path can be appended , It is not recommended to modify or delete the original loading path
stay JVM Add the following startup parameters , You can go through Class.forName Normally read to the specified class , It indicates that this parameter can be added Bootstrap Class loading path for :

-Xbootclasspath/a:/Users/sss/book/ easyCoding/byJdk11/src

If you want to see which one is loaded at startup jar Which class in the package , Can increase


This parameter is very useful in resolving class conflicts , After all, it's different JVM The environment is not consistent with the order in which classes are loaded
Sometimes I want to observe the loading context of a specific class , Because of the large number of classes loaded , It's hard to catch the loading process of the specified class when debugging , You can use the conditional breakpoint function
such as , Want to see HashMap Loading process of , stay loadClass Place a breakpoint , And in condition Input in the box as shown in the figure

JVM How to establish each class in JVM Uniqueness

The fully qualified name of the class and the class loader that loads the class ID

After learning the implementation mechanism of classloader , Know that the parental delegation model is not a mandatory model , You can customize the classloader , Under what circumstances do you need a custom classloader ?

  • Isolation loading class
    In some frameworks, middleware is isolated from application modules , Load classes into different environments
    such as , Arene's container framework ensures the application's dependencies through a custom classloader jar The package does not affect the middleware runtime usage jar package

  • Modify the class loading method

    Class loading model is not mandatory , except Bootstrap Outside , Other loads do not have to introduce , Or according to the actual situation at a certain point in time for dynamic loading on demand

  • Extended load source

    For example, from the database 、 The Internet , Even the TV set-top box loads

  • Prevent source code leaks

    Java Code is easy to compile and tamper with , It can be compiled and encrypted . Then class loader also needs to be customized , Restore encrypted bytecode .

To implement the custom class loader

  • Inherit ClassLoader

  • rewrite findClass() Method

  • call defineClass() Method

A simple class loader implementation example code is as follows

Because middleware usually has its own dependency jar package , When multiple frames are referenced in the same project , Often forced to arbitrate in classes . According to some rules jar The version of the package is specified uniformly , Causes some classes to have package paths 、 The same class name , Will cause class conflicts , Causes an exception to the application .
The mainstream container class framework will customize the class loader , Realize class isolation between different middleware , Effectively avoid class conflicts .

Previous recommendation

Because I don't know the thread pool bug, some Java He was sacrificed to heaven

Programmers are bursting with repetitive logging ELK Be dismissed !

hug Kubernetes, goodbye Spring Cloud

Gateway in the age of cloud Nativity V.S Reverse proxy

At present, the communication group has  800+ people , It aims to promote technical exchange , Official account can be added to the invitation of WeChat

Like articles , Order one “ Looking at 、 give the thumbs-up 、 Share ” Quality Three company support ~

本文为[The official account -JavaEdge]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云