Example explanation of Java advanced virtual machine loading mechanism

The JVM loads binary streams, which can be Class file can also be in other forms. If it is designed according to the loading standard, there will be no big problem.

The following is an analysis of the mechanisms and standards:

First, the loading mechanism of Java class files is similar to that of variables. It first loads the class files into memory, then validates, parses and initializes the data, and finally forms a Java type that can be directly used by the virtual machine. Because Java adopts JIT mechanism, it will be slow to load, but it has obvious advantages, has high flexibility, and supports dynamic loading and dynamic connection.

Next, let's talk about the loading process of classes:

The basic process of loading a class is in the following order, but some do not strictly follow this order or disrupt the order. For example, dynamic loading must be initialized before parsing.

1. Loading

It is up to the virtual machine to decide, but there are also cases where the above stage is executed because the following stage is to be executed.

The virtual opportunity does three things:

First, read the binary stream of the file through the fully qualified name;

Second, put the static methods and variables in the file into the method area;

Third, generate an object into the heap as an access entry.

Note that the first one is only to read binary streams. It doesn't say what files to read from or where to read from. Therefore, Java has strong scalability, which can be from jar and zip, network layer, database layer, etc.

It is mainly the declaration of object and method area.

2. Verify

Ensure that the binary stream meets the requirements of the virtual machine. If not, verifyerror will be reported.

First, file format verification, whether there are magic numbers and whether they meet the requirements of java files;

Second, metadata verification, whether it conforms to Java code specification, such as whether abstract class is directly instantiated, whether ordinary class has an indirect or direct parent class object, etc; Third, bytecode verification analyzes the data flow and control flow to ensure that no behavior endangering the virtual machine will be made, such as whether to call non-existent instructions, whether to assign a parent class to a child class, whether to assign an object to an object other than this type, etc;

Fourth, symbolic reference verification mainly refers to whether the class, variable and method description can be found, such as whether the fully qualified name can find the file, accessibility, etc.

It mainly determines the internal structure

3. Prepare

Assign an initial value to a class variable, usually a value of 0, such as a static variable, without assigning a value to an instance variable.

4. Analysis

The process of converting a symbolic reference in a constant pool to a direct reference. Here, the symbolic reference refers to the variable type, and the direct reference refers to the handle that can be directly located to the object. Class, method, field and interface are parsed. The related object is obtained according to the fully qualified name and its type is obtained. If there is no access to the class, the illegalaccesserror, field nosuchfielderror and method nosuchmethoderror will be thrown. If the class is not the interface, the incompatibleclasschangeerror will be thrown

5. Initialization

Load classes and necessary resources according to program requirements. There are only four cases. The following operations can only be performed after active initialization, so the above four steps should be performed first.

First, for classes with new or static keywords, new generates objects and static loads them statically. These two obviously need to be initialized;

Second, the user class has a parent class, which can't be helped;

Third, the methods in the reflection class must be initialized, right;

Fourth, the main class of execution, the class of main method. Other cases of passive initialization do not need to be considered.

Small example:

The execution result shows a problem: when a subclass calls a parent variable, the subclass is not initialized, because the code relationship at this time has nothing to do with the subclass; When the child class is initialized, the parent class is not initialized again because the parent class has been initialized in the current method body. The only difference between an interface and a parent class is that the parent interface is not required for interface initialization. It is initialized only when the parent interface is used. Similarly, a class constructor will be generated.

At this time, loading the class constructor will initialize all variables in the class. Of course, the parent class initializes before the child class

6. Use

After loading, how to call, draw, calculate, etc

7. Unload

Class is no longer called

Whether the two classes are equal or not mainly lies in that the first class is loaded with the same loader, and the second class has the same fully qualified name and address

Why ask the above question? Next, let's talk about a loading mechanism of virtual machine.

From the perspective of Java virtual machine, there are two kinds of loaders, one is called system loader (bootstrap classloader) and the other is called extensions classloader. This kind is divided into two, one is called Application Loader and the other is called extension class loader. Generally, the former is the default; Our application loading is mainly completed by the cooperation of the above three loaders. For example, applicationC > extensionc > bootrap. The parent delegation mechanism refers to the combination of two. The child loader calls the method of the parent loader first, and then uses the child loader when the target object is not found

The pseudo code is as follows:

Java advocates that we write the logic of our calling class in findclass, which is conducive to the normal use of the parent delegation mechanism.

Destroy 1. Rewrite loadclass

Destroy 2. Use the thread context loader to let the parent loader call the methods of the child loader

3. Hot loading is now commonly used to customize the class loader and overwrite the original bug module - OSGi

However, due to the confusion of rules between custom loaders and the problem of simultaneous reference, the class will not be found eventually, resulting in thread deadlock and memory leakage.

Hot repair, also known as plug-ins, is popular at present, such as hotfix, Nuwa, droidfix, andfix, etc. these frameworks can be found in GitHub or other places. The principle is as above. There are various methods, including coverage, redirection, etc. through configuration, setting action, etc; As a plug-in, you need to meet the following conditions:

1. It can be installed independently, but it cannot run independently

2. With downward compatibility, it can be expanded

3. It can only run in the host program, and can be disabled and replaced

The above example explanation of Java advanced virtual machine loading mechanism is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>