As a programmer, we can’t get around the JVM when we interview, which is our eternal pain. Don’t you feel like you have to recit it every time you go to an interview, or you’re going to get fucked.

So the question is, why do you memorize every interview? ‘ ‘why back over broken time to forget? In addition to the memory is not deep, there is an important reason is not understand, do not understand things, of course, remember soon.

So from the beginning of this article, we will start to learn the JVM, try to understand memory, the knowledge points together. Although this piece of knowledge is very boring, very boring, but I try to write simple, interesting point, we refueled together, ok?

Class loading mechanism

1.1 Complete Process (Simple version)

From the figure above, we can see that a simple Java program execution flow is as follows:

1. We write Java code locally

2. The compiler helps us to automatically compile the.class file (you can also manually compile the file through the javac command, because idea does it for us, in fact, the bottom layer still calls the javac command).

3. Next, deploy to a Web container and run it (you can also run it using the java-jar command)

The process feels simple enough to convert Java source files into Java-aware.class files and run them as a package.

1.2 Complete Process (Complex Version)

In fact, the entire life cycle of a class from step 2 (compile to a.class file) to step 3 (run) is not easy to describe. It includes complex and complete processes :(is not cheated, take a look at the picture below, very complex, dizzy 🤣)

The Loading phase is simple. When a program reaches the required class, the JVM loads it into memory through the class loader. Next, let’s take a look at what a class loader is and then walk through the entire class loading process in detail. (Come back later)

Class loaders

The Java Virtual Machine design team implemented the loading action outside of the Java virtual machine so that the program could decide when to fetch the required classes. The code that does this is called a classloader.

Simply put, the code that does the loading is called a classloader, and it is up to the application to decide.

Class loaders fall into three categories:

  1. Bootstrap ClassLoaderIt is responsible for loading core libraries in the JDK installation directory (such as classes in the /lib directory) that the JVM runtime itself needs.
  2. Extension ClassLoaderIt is responsible for loading extension libraries in the JDK installation directory (such as /lib/ext).
  3. Application ClassLoaderResponsible for loading user-developed Java classes.

The above three types of loaders are intended for different class loaders, that is, a class can have only one class loader to load it.

3 Parent delegation mechanism

So the question is, how do you ensure that only one class loader is loaded?

The launcher class loader is the grandfather, the extension class loader is the father, and the application class loader is the son. The launcher class loader has no parent.

Official:

If a class loader class loading requests, he first won’t try to load the class, but to delegate the request to the parent class loader to complete, each level of class loaders, so all the class loader request should be sent to the top of the boot class loader, only when the parent class loader feedback they can’t finish the load request, The subclass loader will try to do the loading itself.

Translation:

If the application classloader (son) gets the request (salary), he should not spend it himself, but give it to his parent, the extension classloader (dad), who does not spend it himself, and then give it to his parent, the launcher classloader (grandpa). If grandpa says he can’t load the request (i.e., it doesn’t cost anything), he gives it to the extension classloader (Dad), if dad needs to spend money, he can spend it (loading the class), and if dad doesn’t need to spend money, he gives it to his son, and the son spends it.

advantages

1. Avoid class reloading. There is no need for the subclass loader to reload the class when the parent has already loaded it.
2. Prevent classes in the Java core API from being replaced at will, avoid risks, and prevent the core API library from being tampered at will.

4. Break the parental delegation model

We need to know that the parent delegate mechanism is not a mandatory constraint model, just the implementation of the class loader that Java designers recommend to us.

For example, Tomcat doesn’t implement the parent delegate model. Let’s think about it. Why doesn’t it implement the parent delegate model? (Moves his head)

Tomcat is a Web container that needs to address the issue of version isolation. That is, a Web container may need to deploy multiple applications, and different applications may rely on different versions of the same third-party class library. It is not necessary to have only one copy of the same class library on the same server. Therefore, it is necessary to ensure that the class libraries of each application are independent and isolated from each other.

Classes that CatalinaClassLoader and SharedClassLoader can load by themselves are isolated from each other. WebAppClassLoader can use classes that SharedClassLoader loads into, but the WebAppClassLoader instances are isolated from each other.

Obviously, the loading mechanism of Tomcat violates the model of the parent delegate mechanism. In order to achieve isolation, each webappClassLoader loads the class file in its own directory without passing it to the parent class loader, breaking the parent delegate mechanism.

Class loading process

2.1 Verification Phase

The details are verified to ensure that the byte stream of the Class file contains information that meets all requirements of the Java Virtual machine specification. This includes information about validating file formats, metadata, bytecode, symbol references, and more.

2.2 Preparation

Formally allocate memory and set initial values for variables defined in a class.

2.3 Analysis Phase

This is actually the process of replacing a symbolic reference to a class with a direct reference.

2.4 Initialization Phase

As mentioned earlier, the JVM allocates space and default values to the static fields of the class in the preparation phase. In the initialization phase, the initialization code of the class is formally executed to initialize the class, such as some information from the configuration, not directly have default values, but can be obtained.

2.5 Use Phase

Use classes or objects in programs to perform business.

2.6 Uninstallation Phase

When it is determined that an object is no longer needed, the JVM needs to do garbage collection. This is also a priority.

conclusion

This article mainly describes the JVM class loader type, how they work together, why to destroy the class loader, and then describes the complete loading process of the file, with a number of examples in the middle, clearly explain the process.

If you feel that writing is also good, trouble to give a praise 👍, your recognition is the power of my writing!

If you think something is wrong, please feel free to comment. You can also pay attention to my public number learning Java little sister, we discuss together.

All right, bye.

reference

Blog.csdn.net/huihuidage/…