当前位置:网站首页>Class loading mechanism (detailed explanation of the whole process)
Class loading mechanism (detailed explanation of the whole process)
2022-07-03 04:57:00 【Day by day, the dish chicken Jie!!】
One : background
The class loading mechanism is in our real java One of the operation phases of .
Two : What is a happy planet ( Class loading mechanism )
We wrote Java All the documents are based on .java A file with a suffix , The compiler will write .java The files are compiled into .class file , In short, the class loading mechanism is jvm From the file system, a series of class file z Convert to binary stream loading JVM In memory and generate a of this class Class object , The act of providing resources for subsequent program operations .
3、 ... and : Class loading process
1: Overall flow chart
2: Analysis of each stage
(1): load
a: The bytecode file of the loaded class and the source of the binary file
Find such bytecode files through the full path of a class (class Files are binary files ). Convert the static storage structure of binary file to the runtime data structure of method area , And use binary stream file to create a Class object , Stored in Java The entry in the heap for the data structure reference to the method area ;
class The source of the document : It should be noted that the class loading mechanism can not only be read from the file system class file , It can also be obtained through the Internet , other jar Package or other program generation , Such as JSP application .
b: Class loader
Class loader : When it comes to class loading, we have to talk about the order of class loading and class loader .Java There are about four types of loaders , Namely : Start class loader (Bootstrap ClassLoader), Extend the classloader (Extension ClassLoader), system class loader (System ClassLoader), Custom class loaders (Custom ClassLoader), In turn, it belongs to the inheritance relationship ( Note that inheritance here is not Java The inside of the class extends)
Start class loader (Bootstrap ClassLoader): Mainly responsible for loading and storage in Java_Home/jre/lib Next , Or be -Xbootclasspath Under the path specified by the parameter , And can be recognized by the virtual machine class library ( Such as rt.jar, be-all java.* The first classes are all Bootstrap ClassLoader load ), Boot class loader is unable to be Java The program directly refers to .Extend the classloader (Extension ClassLoader): Mainly responsible for the loader by sun.misc.Launcher$ExtClassLoader Realization , It's responsible for loading Java_Home/jre/lib/ext Directory , Or by java.ext.dirs All class libraries in the path specified by the system variable ( Such as javax.* Initial class ), Developers can use the extension class loader directly .
system class loader (System ClassLoader): Mainly responsible for the loader by sun.misc.Launcher$AppClassLoader To achieve , It is responsible for loading the user classpath (ClassPath) The specified class , Developers can use this kind of loader directly , If there is no custom classloader in the application , In general, this is the default classloader in the program .
Custom class loaders (Custom ClassLoader: Self developed classloader
c: Parents delegate
If a class loader needs to load classes , First of all, it will delegate the class loading request to the parent class loader , If the parent class still has a parent class, then delegate , Every layer is like this .
Recursion all the way to the top level , When the parent loader cannot complete the request , Subclasses will try to load .
(2): verification
As long as the verification process is to guarantee class Security and correctness of documents , Make sure that the class Documents do not lead to JVM There are any exceptions , Will not harm JVM Our own safety . Validation includes validation of file formats , Metadata and bytecode validation .
(3): Get ready
The preparation phase is the process of allocating memory and initializing zero values for class variables . Note that the memory allocated at this time is the class variable , This memory is allocated in the method area . Memory for instance variables will not be allocated at this time , Because instance variables are created together when an object is instantiated Java Heap . And the class variable is assigned to zero , namely int The initial value of type is 0, The initial value of reference type is null, Instead of displaying the assigned value in the code .
(4): analysis
Convert the symbolic reference of constant pool to direct reference . A symbolic reference can be understood as just an alternative label , For example, you need to make a plan at this time , There are no candidates yet , You set up a A To do this . And then when the plan really comes to fruition, we must find a definite candidate , At that time, Xiao Ming will do one thing .
Analysis is to put A( Symbol reference ) Replace it with Xiao Ming ( Direct reference ). A symbolic reference is a literal quantity , It doesn't make any sense , Just a representative . Direct reference refers to a real reference , You can find the target through this reference in memory .
(5): initialization
The initialization phase is the last step in class loading , This stage is mainly the implementation of java Code , Perform the relevant initialization actions ; At this point, some static code blocks are executed , Assign values to static variables , The assignment here is the assignment in the code , The preparation stage is just to set the initial value to occupy a pit .
边栏推荐
- Shuttle + Alluxio 加速内存Shuffle起飞
- Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
- Notes | numpy-07 Slice and index
- Analysis of proxy usage of ES6 new feature
- Market status and development prospect prediction of global fermentation acid industry in 2022
- The 19th Zhejiang I. barbecue
- Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
- Retirement plan fails, 64 year old programmer starts work again
- 关于开学的准备与专业认知
- 【XSS绕过-防护策略】理解防护策略,更好的绕过
猜你喜欢
2022-02-11 daily clock in: problem fine brush
Apache MPM model and ab stress test
The reason why the entity class in the database is changed into hump naming
Interface frequency limit access
论文阅读_ICD编码_MSMN
Actual combat 8051 drives 8-bit nixie tube
Review the old and know the new: Notes on Data Science
[luatos sensor] 1 light sensing bh1750
Thesis reading_ Chinese medical model_ eHealth
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
随机推荐
1103 integer factorization (30 points)
[develop wechat applet local storage with uni app]
1110 complete binary tree (25 points)
JDBC database operation
Actual combat 8051 drives 8-bit nixie tube
sql语句模糊查询遇到的问题
Shuttle + Alluxio 加速内存Shuffle起飞
Introduction to message queuing (MQ)
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
RT thread flow notes I startup, schedule, thread
document. The problem of missing parameters of referer is solved
Market status and development prospects of the global autonomous marine glider industry in 2022
SSM framework integration
1115 counting nodes in a BST (30 points)
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
MPM model and ab pressure test
MediaTek 2023 IC written examination approved in advance (topic)
The reason why the entity class in the database is changed into hump naming
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT