- JVM uses a parent delegation mechanism to load classloader on demand
- After the class is loaded into the memory, the binary content of the class is stored in the Method Area of the JVM, and a class object pointing to the content is also generated
- The four levels of class loader, from top to bottom:
- Bootstrap: Load the core content of the JDK in the lib. If you call getClassLoader() to get null, it means that you have reached the top class loader
- Extension: Load various files in the extension package, the extension package is located in the JDK installation directory jre/lib/ext/*.jar
- Application: commonly used class loader, used to load the content specified by the classpath
- CustomClassLoader: custom class loader
- Class loading process:
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;
}
}
- Inherit ClassLoader
- Rewrite findClass(), call defineClass() in findClass() to implement encryption logic
- To break the parental delegation, override loadClass()
- An example of a custom ClassLoader is as follows
public class MyClassLoader extends ClassLoader {
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
//class file is placed on D drive
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());
}
}
- how? override loadClass()
- when?
- Before JDK1.2, custom ClassLoader must override loadClass()
- ThreadContextClassLoader can implement the basic class call implementation class code, specified by thread.setContextClassLoader
- Use hot start and hot deployment when modularizing: osgi tomcat has its own classloader specified by the module, which can load into the class of the same name, breaking the parent delegation
- Interpreter: bytecode intepreter
- JIT:Just In-Time compiler
- Mixed mode: mix interpreter and hot code compilation
- Interpreted execution
- Hot code detection: 1. Methods that are called multiple times (method counter detection method execution frequency); 2. Loops called multiple times (loop counter detection cycle execution frequency); 3. Compile
- VM options:
- -Xmixed: The default is mixed mode
- -Xint: Use the interpretation mode, start quickly, and execute slightly slower
- -Xcomp: Use pure compilation mode, execute very fast, start very slowly
- It is divided into three steps: verification, preparation, and resolution, see the figure in the overview
- Give a chestnut, the result is 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 ++;
}
}
Beginning with the class loading process, let me explain the timing of class loading. At this point it can be divided into two cases: Active reference Passive reference The active reference of a class...
JVM definition Java virtual machine is a fictional computer used to run java applications. effect: Is the key to the java program "compile once, run everywhere", when the java program is com...
This article lets us continue to sort through the process of class loading, the analysis code is: Here are two questions: Why do you want to create klassKlass first? Because klassKlass is the end of t...
(a) loading During the load phase, the virtual machine does three things: - Get the binary byte stream defining this class by the fully qualified name of a class; - converts the static storage structu...
I saw the timing of class loading. This article records the process of class loading, that is, what is done at each stage of loading. Class life cycle load "Loading" is a phase in the class ...
1. Load, load the binary byte stream into the method area, and instantiate an object of java.lang.Class class in the java heap 2. Verification: file, verify class file format specification - magic num...
The previous articles introduced the creation of a bunch of klass objects. In this article, let's introduce the creation of basic arrays. This is different from the previous introduction. The code inv...
The previous article explained the creation process of klassKlass. This article shows the way klass has been created in the Universe::genesis(TRAPS) method. Note that the way it is created is the same...
[Original Address] In general, the process of loading a class into a JVM is divided into three steps: loading, linking, and initializing. load Loading is the process of finding a byte stream and creat...
Class loader Class loading process load: connection: initialization: Static block can only access variables defined before the static block, defined after the variable, can only be assigned, can not a...