当前位置:网站首页>Introduction to JVM class loading
Introduction to JVM class loading
2022-08-05 00:44:00 【xmh - sxh - 1314】
Class loading and initialization is done only once
1, Loading (requires the support of the class loader): In this stage, a java.lang.Class object representing this class will be generated in memory as the entry for various data of this class in the method area.Note that this does not necessarily have to be obtained from a Class file. It can be read from a ZIP package (such as from a jar package and a war package), calculated at runtime (dynamic proxy), or generated by otherFile generation (such as converting JSP files into corresponding Class classes).
2, Verification: The main purpose of this stage is to ensure whether the information contained in the byte stream of the Class file meets the requirements of the current virtual machine, and will not endanger the security of the virtual machine itself.
3, preparation (all default values): The preparation phase is the phase in which memory is formally allocated for class variables and the initial value of the class variable (usually the zero value of the data type) is set, and the memory used by these variables will beAllocate in the method area.At this time, memory allocation only includes class variables (variables modified by static), not instance variables. Instance variables will be allocated in the Java heap along with the object when the object is instantiated.
4, parsing: The parsing phase is a process in which the virtual machine replaces the symbolic references in the constant pool with direct references.
5. Initialization: The process of executing the class constructor clinit() method, which is formed by the compiler automatically collecting the assignments of all class variables in the class and combining the statements in the static statement block
The class initialization phase is the last step in the class loading process. At this stage, the Java program code (or bytecode) defined in the class is actually executed.In the preparation phase, the variable has been assigned the initial value required by the system, and in the initialization phase, class variables and other resources are initialized according to the subjective plan made by the programmer through the program.
Note the following:
1. The order in which the compiler collects is determined by the order in which the statements appear in the source file,
In the static statement block, only the variables defined before the static statement block can be accessed, and the variables defined after it can be assigned values in the previous static statement block, but cannot be accessed. The code is explained as follows:
Copy code
public class Test{
static{
i=0;//Assignment to variables can be compiled and passed
sout(i); //The compiler will prompt an illegal forward reference
} }
static int i = 1;
}
Copy code
2. The order in which the initialization methods are executed. The virtual machine ensures that the initialization method of the parent class has been executed before the initialization method of the subclass is executed.
3. The virtual machine ensures that a class's clinit() method is properly locked and synchronized in a multithreaded environment
4. When accessing a static field of a java class, only the class that actually declares this field will be initialized
Active reference to the class (initialization occurs)
new an object of a class
Calling static members (except final constants) and static methods of a class
When called by reflection
Start the class where the main method is located
The virtual machine ensures that the initialization method of the parent class has been executed before the initialization method of the subclass is executed,
Passive reference to class (no initialization occurs)
Referencing constants does not trigger class initialization: constants are stored in the constant pool of the calling class at compile time
Class references via arrays will not trigger initialization
When accessing a static field (static variable), only the class that actually declares the field will be initialized (subclass)
Classloader:
1. Function: Load the bytecode file into the memory, convert the static data into the runtime data structure of the method area, and generate a Class object in the heap as the access entry for the method of the class data in the method area
p>Hierarchy (tree structure)
2.ClassLoader related methods:
3, proxy for class loader
边栏推荐
猜你喜欢
Helm Chart
刘润直播预告 | 顶级高手,如何创造财富
内存取证系列1
TinyMCE disable escape
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
Lattice PCIe 学习 1
MongoDB construction and basic operations
Inter-process communication and inter-thread communication
leetcode:266. 回文全排列
随机推荐
Countdown to 1 day!From August 2nd to 4th, I will talk with you about open source and employment!
tiup update
leetcode:269. 火星词典
could not build server_names_hash, you should increase server_names_hash_bucket_size: 32
E - Many Operations (bitwise consideration + dp thought to record the result after the operation
EL定时刷新页面中的皕杰报表实例
5.PCIe官方示例
2022 Nioke Multi-School Training Session H Question H Take the Elevator
tiup uninstall
[230]连接Redis后执行命令错误 MISCONF Redis is configured to save RDB snapshots
2022牛客多校第三场 A Ancestor
2022 The Third J Question Journey
Software testing interview questions: What are the three modules of LoadRunner?
2022牛客多校训练第二场 H题 Take the Elevator
机器学习(公式推导与代码实现)--sklearn机器学习库
QSunSync 七牛云文件同步工具,批量上传
DHCP的工作过程
tiup status
2022牛客多校训练第二场 J题 Link with Arithmetic Progression
软件测试面试题:软件测试类型都有哪些?