In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains the "JVM class loading mechanism and life cycle of the detailed introduction", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "JVM class loading mechanism and life cycle of the detailed introduction" it!
one。 Goal:
1. What is class loading?
two。 The life cycle of the class?
3. What is a classloader?
4. What is the parental appointment mechanism?
two。 Principle (the loading process of the class and its final product):
JVM loads the class file bytecode file into memory and converts this static data into a runtime data structure in the method area, generating a java.lang.Class object representing this class in the heap (not necessarily in the heap, and HotSpot in the method area) as an access entry to the method area class data.
three。 Process (the life cycle of a class):
The JVM class loading mechanism is divided into five parts: loading, verification, preparation, parsing, and initialization. Let's take a look at these five processes respectively. The order of the five stages of loading, verification, preparation, initialization and unloading is fixed, while parsing is not necessarily. To support dynamic binding, parsing can occur after the initialization phase.
Load:
The loading process accomplishes three main things:
Get the binary byte stream that defines the class through the fully qualified name of the class
Convert the static storage structure represented by this class byte stream into the runtime data structure of the method area
A java.lang.Class object representing this class is generated in the heap as an entry into the access method area for these data structures.
This process is mainly completed by the class loader.
Check:
The main purpose of this phase is to ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine and does not endanger the security of the virtual machine itself.
File format verification: based on byte stream verification.
Metadata validation: storage structure verification based on method area.
Bytecode verification: storage structure verification based on method area.
Symbol reference verification: storage structure verification based on method area.
Prepare:
Allocate memory to the class variable and initialize it to the default value. (this is the default value, and variables are assigned only when initialized.) that is, the memory space used by these variables is allocated in the method area. For example:
Public static int value = 123
At this time, after the preparation phase, the initial value is 0 instead of 123; the putstatic instruction that assigns value to 123 is compiled and stored in the class constructor method. Special case:
Public static final int value = 123
At this point, the value of value is 123 after the preparation phase.
Parsing:
Converts symbolic references in types to direct references.
Symbolic references are independent of the layout implemented by the virtual machine, and the target of the reference does not have to be loaded into memory. The memory layouts of various virtual machine implementations can vary, but the symbolic references they can accept must be consistent, because the literal form of symbolic references is clearly defined in the Class file format of the Java virtual machine specification.
A direct reference can be a pointer to a target, a relative offset, or a handle that can indirectly locate the target. If there is a direct reference, the target of the reference must already exist in memory
There are four main types:
Parsing of classes or interfaces
Field parsing
Class method analysis
Analysis of interface method
Initialize:
The initialization phase is the process of executing class constructor methods. The method is formed by the compiler automatically collecting the assignment of class variables in the class and the statements in the static statement block. Before the virtual machine ensures that the method is executed, the method of the parent class has been executed. If there is no static variable assignment or static statement block in a class, the compiler may not generate a () method for the class.
In java, for the initialization phase, there are and only five situations in which the required class will be "initialized" immediately (loading, verification, preparation, and naturally need to start before):
Instantiating an object using the new keyword, accessing or setting a static field of a class (an exception decorated by final or optimized by the compiler that has been placed in a constant pool), and calling a class method all initialize the static field or class in which the static method resides.
When initializing a class, if its parent class has not been initialized, it should first trigger its parent initialization.
When making reflection calls using the methods of the java.lang.reflect package, if the class is not initialized, initialize it first.
When the virtual machine starts, the user first initializes the main class to be executed (including main)
After jdk 1.7, if the last parsing result of the instance of java.lang.invoke.MethodHandle is REF_getStatic, REF_putStatic, REF_invokeStatic method handle, and the class in which the method belongs is not initialized, initialize it first.
four。 Class loader:
The action of "getting the binary byte stream that describes the class through the fully qualified name of a class" in the class loading phase is handed over to the class loader outside the virtual machine. The advantage of this is that we can implement the class loader ourselves to load classes in other formats, as long as it is a binary byte stream, which greatly increases the flexibility of the loader. There are three kinds of class loaders that come with the system:
Start the classloader.
Extend the class loader.
Application class loader.
five。 Parent appointment mechanism:
The working process of the parent appointment mechanism:
If a classloader receives a request from a classloader. It will not try to load the class itself in the first place. Instead, the request is delegated to the parent loader to complete. The same is true of class loaders at each level. So all load requests will eventually be sent to the Bootstrap classloader (boot classloader). Only when the parent class load feedback cannot load the request (the desired class is not found in its search scope). The child loader will try to load it itself.
The advantage of the parent delegation model: the java class has a hierarchical relationship with priority along with its loader.
For example, class java.lang.Object, which is stored in rt.jart. No matter which class loader loads this class. In the end, the parents delegate the Bootstrap class loader at the top of the model to load. Therefore, the Object class is the same class in all kinds of classloader environments of the program. On the contrary. If the parent delegation model is not used. If each class loader loads on its own. If the user writes a class called java.lang.Object. And stored in the ClassPath of the program. There will be several different Object classes in the system. The most basic behavior in the java type system cannot be guaranteed. The application will also be a mess.
Thank you for reading, the above is the content of "detailed introduction of JVM class loading mechanism and life cycle". After the study of this article, I believe you have a deeper understanding of the detailed introduction of JVM class loading mechanism and life cycle, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.