Virtual machine class loading mechanism (initialization)

Virtual machine class loading mechanism

The conversion of the result of code compilation from native machine code to bytecode is a small step in the development of storage formats, but it is a big step in the development of programming languages.

Overview

In the previous chapter, we learned about the specific details of the storage format of the class file, and various information described in the class file. In the end, it needs to be loaded into the virtual machine before it can be run and used. How the virtual machine loads these class files, and what changes will happen to the information in the class files after entering the virtual machine, these are the contents to be explained in this chapter.
The data to describe the class of your dick is loaded from the class file to the memory, and the data is verified, converted, parsed and initialized and finally formed. The Java type that can be used directly by the virtual machine is the class loading mechanism of the virtual machine.
Different from those languages ​​that need to be linked at compile time, type loading in the Java language. The linking and initialization process are all completed during the running of the program. Although this strategy will slightly increase the performance overhead during loading, it will be provided for Java applications. The high degree of flexibility and the language feature that Li Tiansheng can dynamically expand is achieved by relying on the two features of runtime dynamic loading and dynamic linking. For example, if you write an interface-oriented application, you can wait until runtime to specify the actual implementation class of the enterprise. Users can use Java predefined and custom classes in the loader, so that a local application can load a binary stream from the network or other places as part of the program code at runtime. This way of assembling applications has been widely used in add-me ​​programs. From the most basic JSP applied to the relatively complex os gi technology. All use the features loaded during the runtime of the Java language.
In order to avoid possible deviations in language expression, before the official start of this chapter, the author establishes two language conventions. First, in actual situations, each class file may represent an interface or class in the Java language. . The description of the class in the text includes the possibility of the class and the interface, and the scenarios where the class and the interface need to be described separately will be specified. Second, the class file format is introduced in the previous section. It is agreed upon at all times. The class file mentioned by the author on this site does not refer to a file that exists in a specific disk. The class file mentioned here should be a string of binary streams. It can exist in any form.

When to load the class

The memory is loaded into the memory of the virtual machine until it is unloaded. Its entire life cycle includes seven stages of loading, verification, preparation for analysis, initialization, use, and unloading. Which verification is ready to parse. The three parts are collectively referred to as links.
The order of the five stages of loading, verification, preparation, initialization, and unloading is determined. The loading process of the class must be started step by step in this order, while the parsing stage is not necessarily. In some cases, it can be started after initialization. This is to support the operation of the Java language. The binding is also called dynamic binding. Or binding in the past. Note that the author here writes the step-by-step start, not the step-by-step operation or completion. This point is emphasized because these phases are usually intersected and mixed, and are usually called during the execution of a phase. Activate another stage.

When does the first stage of the class loading process start? There are no mandatory constraints in the load kilowatt virtual machine specification, which is okay. As the specific implementation of the virtual machine comes from the assurance that it is in the initialization phase, the virtual machine specification is strictly stipulated, there are only five cases, and it must be corrected immediately. The class is initialized and loaded, and the verification preparation naturally needs to start before that.
1. When encountering the four bytecode instructions of new get static, put static or Invoke static, if tired, it has not been initialized. You need to trigger and initialize the most common Java code that occurs these four instructions first. Unity is when you use the new keyword to instantiate an object. Reading or setting a static field of a class is final modified, except for the static field that has been put into the constant pool by the compiler. And when calling a static method of a class.
2. Use the method of the Java.lang.reflect package to call when the class is reflected. If the tire has not been initialized, phase triggering and initialization are required.
3. When initializing a class, if it is found that its parent class has not been initialized yet. You need to trigger the initialization of its parent class first.
4. When the virtual machine starts, the user needs to specify a main class to execute, the class containing the inner method. The virtual machine first initializes this main class
5. When using the dynamic language support of JDK 7 at that time, if a Java Lang, dear invoke method handler. The final analysis result of the strength is the method handle of Ref, get static ref put static if evoke static. And The class corresponding to this method handle has not been initialized, and its initialization needs to be triggered first.
For these five scenarios where live and hair classes are initialized, a strong qualifier is used in the virtual machine specification. There are and only the behaviors in these five scenarios are called performing on a class. Active reference, other than that, all methods of the referenced class will not be triggered. Initialization is called passive referencing.

Guess you like

Origin blog.csdn.net/weixin_39472101/article/details/110151676