当前位置:网站首页>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 .
边栏推荐
- Unity tool Luban learning notes 1
- 1111 online map (30 points)
- Shell script Basics - basic grammar knowledge
- Introduction to JVM principle
- [research materials] 2021 China's game industry brand report - Download attached
- Retirement plan fails, 64 year old programmer starts work again
- [research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
- Number of 1 in binary (simple difficulty)
- Online VR model display - 3D visual display solution
- [set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
猜你喜欢
关于开学的准备与专业认知
Leetcode simple question: check whether the string is an array prefix
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
Retirement plan fails, 64 year old programmer starts work again
Number of 1 in binary (simple difficulty)
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
Thesis reading_ Chinese medical model_ eHealth
Preparation for school and professional cognition
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
随机推荐
The consumption of Internet of things users is only 76 cents, and the price has become the biggest obstacle to the promotion of 5g industrial interconnection
Market status and development prospect prediction of global fermented plant protein industry in 2022
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
Preparation for school and professional cognition
1110 complete binary tree (25 points)
第十九届浙江省 I. Barbecue
Thesis reading_ Tsinghua Ernie
General undergraduate college life pit avoidance Guide
Oracle SQL table data loss
联发科技2023届提前批IC笔试(题目)
[tools run SQL blind note]
Do you know UVs in modeling?
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
Source insight garbled code solution
普通本科大学生活避坑指南
Valentine's day limited withdrawal guide: for one in 200 million of you
Day 51 - tree problem
On typescript and grammar
雇佣收银员(差分约束)
[research materials] annual report of China's pension market in 2021 - Download attached