Java class load and class life cycle

Class loader

What is the class loader

Class loader to do the work:

  1. The .class file class read binary data into memory, a method in which region the runtime data area
  2. Java.lang.Class then create a heap object, the object class to encapsulate the data structure in the method area.

Class load of the final product:

Class objects created in the heap area is located, the secondary packaging Class object class data structure methods zone, and provides the interface access method for a data structure of the region to Java programmers.

Class loading timing

Learn what a class is loaded, then a class at what time will it be loaded?

  1. And then load it when the class loader does not need to wait for a class to be the "first active use", that is, without waiting for the new time to reload this category.
  2. JVM specification allows a class loader when a class is expected to be used, it is pre-loaded with it, if you encounter a .class files during pre-loaded missing or there is an error, the class loader when the class must first take the initiative to use the program report an error (LinkageError error)
  3. If the missing .class class has not been actively using the program, the class loader will not report an error

Load .class files the way

  1. Loaded directly from the local system
  2. .Class files downloaded through the network
  3. Load .class files from the zip, jar archive, etc.
  4. .Class extract files from a proprietary database
  5. The Java source files dynamically compiled into .class files

Class life cycle

Understand the main points:

  1. Class loading process, including loading, validation, preparation, analytical, initializing five stages. Some time will also verify, ready, to resolve these three stages called the link stage
  2. Among them, loading, validation, preparation and initialization sequence of four stages occur is determined, and the stage is not necessarily resolved, it can start after the initialization phase, in some cases, this is tied to the Java language to support the running time set (also called dynamic binding or late binding).
  3. Note that the additional stages are started sequentially, rather than complete or sequentially , because these stages are generally intersect one another mixing typically call or activate a stage at another stage during execution.

load

Load class is the first stage of the loading process, the main job is to do it to find and load the binary data type, in the loading phase, JVM needs to do three things at this stage:

  1. To obtain a binary byte stream, which is defined by the fully qualified name of a class.

  2. This represents the byte stream of static storage structure into a run-time data structure area method.

  3. Generating a representative of the Java heap java.lang.Class object of this class, such as a method of access to the data area entry.

Relative to other stages of class loading, loading phase (specifically, the operation is a binary stream of bytes acquired class loading phase) is controllable strongest stage, because the developer can use the system to provide both class loader is to finish loading, you can also customize your own class loader to finish loading.

After loading phase is completed, the external storage format of virtual machines on a binary stream of bytes required for virtual machines in accordance with the method area, but also create a class java.lang.Class objects in the Java heap, so that we can through the these data access objects area.

verification

Authentication is the first step in the link stage, it is to ensure the correctness of the loaded class .

The purpose of this stage is to ensure that the information byte stream file included in Class meet the requirements of the current virtual machine and the virtual machine will not jeopardize their own safety.

Validation phase will be completed roughly an inspection operation four stages:

  1. File format validation: validation of a byte stream for compliance with Class file format; for example: whether the beginning 0xCAFEBABE, major and minor version number in the current processing range of the virtual machine, if there is the constant in the constant pool type is not supported .

  2. Metadata authentication: described bytecode information semantic analysis (Note: the semantic analysis phase contrast javac compiler), which describes the information to ensure compliance with the requirements of the Java language specification; for example: whether the class has a parent class, except java .lang.Object outside.

  3. Bytecode verification: the data flow and control flow analysis to determine the semantics of the program is legitimate, logical.

  4. Verification of symbolic references: to ensure that the analysis operation is performed properly.

Validation phase is very important, but not necessarily, has no effect on the program run , if the referenced class after repeated verification, consider using -Xverifynone parameter to shut down most of the class verification measures to reduce virtual machine class loading time.

ready

Work preparation phase is to allocate memory for static variables class ( note that static variables ), these memory will be assigned in the method area , and initializes it to the default value.

The following points need to pay attention to this stage:

  1. This time includes only the memory allocation class variables (static), and does not include the instance variables, instance variables in the heap assigned as a Java objects when an object is instantiated.

  2. Typically the initial value set here is the data type of the default value of zero (e.g., 0,0L, null, false, etc.), rather than the values ​​are given explicitly in Java code.

For chestnut:

假设一个类变量的定义为:public static int value = 3;
那么变量value在准备阶段过后的初始值为0,而不是3,把value赋值为3的动作将在初始化阶段才会执行。
复制代码
  1. The basic data types, class variables (static) and global variables, if not explicitly assign its direct use, the system will assign it a default value of zero, and for the local variables, it must prior to use explicitly assign it, or do not compile time.

  2. For the modified static final constants and at the same time, it must be explicitly stated at the time of its assignment, or do not compile time

  3. But only the final modification of the constants you can either explicitly assign it when you declare, it can also explicitly assign a value (ie constructor), in short, before use must be explicitly assigned its initialization in the class The system does not assign it a value of zero default.

  4. Reference is a reference to the data type, such as an array reference, references to other objects, if not explicitly be used directly assigned a value of zero for the system will be given a default, i.e. null.

  5. If no value is assigned to each element in the array during array initialization, wherein the element will then be given a default value of zero in accordance with the corresponding data type.

  6. If ConstantValue property class field attribute table present field, i.e. while being modified final and static, it will be initialized to the value specified in the attribute ConstValue preparation stage variable value.

For chestnut:

假设上面的类变量value被定义为: public static final int value = 3;
编译时Javac将会为value生成ConstantValue属性,在准备阶段虚拟机就会根据ConstantValue的设置将value赋值为3。我们可以理解为static final常量在编译期就将其结果放入了调用它的类的常量池中
复制代码

Resolve

Parsing stage to do is to type in the symbol references converted to a direct reference

The main analysis operation for the class or interface, fields, methods class, interface method, type method, and calls the method handle 7 points class qualifiers for symbolic references. Symbolic reference is a set of symbols to describe the target can be any literal.

Direct reference is the direct pointer to the object, an indirect or offset relative to the positioning target handle.

initialization

Initialization, given the correct initial value for the class of static variables, JVM classes responsible for initializing the main class variables are initialized.

Initial value set in the Java class variable in two ways:

  1. Specifies the initial value when you declare a class variable

  2. Using static code block is assigned an initial value for class variables

JVM initialization step

  1. If this class has not been loaded and connected, and then connected to a program loads the class

  2. If the direct parent class has not been initialized, it first initializes its direct parent

  3. If the class initialization statements, the system sequentially performs the initialization statements

Class initialization timing:

Only when the active use of the class will lead the class initialization, note that this class is loaded and different classes of actively used include the following six:

  1. Create an instance of the class, which is the new way

  2. Access static variables of a class or interface, or assignment of the static variables

  3. Static method call class

  4. Reflector (e.g. Class.forName ( "com.shengsiyuan.Test"))

  5. Initialize the subclass of a class, the parent class will be initialized

  6. When the Java virtual machine started to be marked to start class class (Java Test), to run a master class directly java.exe command

End of life

In the following situations, Java virtual machine will end the life cycle

  1. Implementation of the System.exit () method

  2. Normal program execution ends

  3. Encountered an exception or error during execution aborted

  4. Because the operating system error caused the process to terminate the Java Virtual Machine

Guess you like

Origin juejin.im/post/5d6dfcf551882559f971d796