当前位置:网站首页>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
边栏推荐
猜你喜欢

内存取证系列1

Matlab uses plotting method for data simulation and simulation

仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!

node uses redis

软件基础的理论

倒计时1天!8月2日—4日与你聊聊开源与就业那些事!

Will domestic websites use Hong Kong servers be blocked?

could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

Inter-process communication and inter-thread communication

ora-01105 ora-03175
随机推荐
Theory of Software Fundamentals
Software test interview questions: BIOS, Fat, IDE, Sata, SCSI, Ntfs windows NT?
Pytorch usage and tricks
B站7月榜单丨飞瓜数据B站UP主排行榜发布!
tiup update
软件测试面试题:您如何看待软件过程改进?在您曾经工作过的企业中,是否有一些需要改进的东西呢?您期望的理想的测试人员的工作环境是怎样的?
gorm的Raw与scan
node uses redis
node使用redis
oracle create user
Software testing interview questions: What are the seven-layer network protocols?
DHCP的工作过程
E - Many Operations (按位考虑 + dp思想记录操作后的结果
leetcode:267. 回文排列 II
tiup telemetry
软件测试面试题:网络七层协仪具体?
仅3w报价B站up主竟带来1200w播放!品牌高性价比B站投放标杆!
Software testing interview questions: What is the difference between load testing, capacity testing, and strength testing?
活动推荐 | 快手StreamLake品牌发布会,8月10日一起见证!
TinyMCE禁用转义