In-depth understanding of Java Virtual Machines-Learning notes-Virtual machine class loading mechanism

Source: Internet
Author: User

The virtual machine loads the data of the description class from the class file, verifies the data, transforms the parsing and initialization, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading mechanism of the virtual machine. In Java, the loading, joining, and initialization of types are done during the run of the program, which, while adding some performance overhead to the class load, provides a high degree of flexibility for Java applications.

First, the time of class loading

Class starts from being loaded into the virtual machine's memory, and its entire lifecycle includes: Load (Loading), validate (verification), prepare (preparation), Parse (Resolution), Initialize ( initialization), use (using), and unload (unloading) 7 stages. Where validation, preparation, parsing 3 parts collectively referred to as the connection.

Initialization of the class

The virtual machine specification strictly stipulates that there are only 5 cases where the class must be "initialized" (load, validate, prepare must start before this):

1) When instantiating an object using the New keyword, when reading or setting a static field of a class (except for a static field that is final decorated, has placed the result in the constant pool at compile time), and when a static method of a class is called. (a variable is final decorated and is determined at compile time.) Then this variable can be called a macro variable. A macro variable can point a reference to Chang)


2) When using the Java.lang.reflect package method to make a reflection call to a class, if the class has not been initialized, it needs to trigger its initialization first.


3) When starting a class, if it is found that its parent class has not yet been initialized, it needs to trigger the initialization of its parent class first


4) When the virtual machine starts, the user needs to specify a main class to execute, and the virtual opportunity initializes the main class first.


5) When using jdk_1.7 's dynamic language support, if the final parsing result of a Java.lang.invoke.MethodHandle instance is ref_getstatic,ref_putstatic,ref_ Invokestatic the method handle, and the class for the method handle has not been initialized, it is necessary to trigger its initialization first.


interface and class initialization is only a little different, an interface at the time of initialization does not require its parent interface to complete the initialization, only when the actual use of the parent interface (such as the reference to the definition of the interface constants) will be initialized.

Passive references to classes

1) For a static field, only the class that defines the field is initialized, so referencing a static field defined in the parent class by a subclass initializes only its parent class, not the child class.


2) referencing a class through an array definition does not trigger initialization of this class. Supperclass[] s = new SUPPERCLASS[10];


3) Constants are stored in the constant pool of the calling class at compile time and are not inherently referenced directly to the class that defines the constants, so initialization of classes that define constants is not triggered. public static final String hello = "Hello";

Second, class loading process loading

Loading is a phase of the class loading process, and during the load phase, the virtual machine needs to complete the following three things:

1) obtain a binary byte stream that defines this class by using the fully qualified name of a class


2) Convert the static storage structure represented by this byte stream into the runtime data structure of the method area


3) Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area


"To get a binary byte stream that defines this class through the fully qualified name of a class" There is no indication of where or how the binary stream is to be obtained, not necessarily from the class file, and many Java technologies are built on this basis:

1) read from the Zip package (JAR, EAR, WAR)

2) getting (applets) from the network

3) Run-time compute generation (Dynamic Agent technology)

4) generated by other files (JSP file generation corresponding Class)

5) read from the database (relatively rare, some middleware server installed the program to the database to complete the program code distribution between the clusters)


When the load phase is complete, the binary byte stream outside the virtual machine is stored in the method area in the format required by the virtual machine. Then instantiate an object of the Java.lang.Class class in memory (not explicitly defined in the Java heap, for a hotspot virtual machine, although the class object is an object but is stored in the method area), This object will act as the external interface for the program to access these types of data in the method area.


The load phase and part of the connection phase, such as a partial bytecode file format Verification action, are interleaved, but the start time of the two phases remains in a fixed sequence.

Verify

Validation is the first step in the connection phase, which is designed to ensure that the information contained in the byte stream of a class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself.

1) file Format verification (main verify that the byte stream conforms to the specification of the class file format)

2) Metadata Validation (semantic analysis of the information described in bytecode to ensure that it conforms to the Java syntax requirements, such as final cannot be overridden, etc.)

3) Bytecode verification (the most complex phase, ensuring that the method of checking class does not make the event that endangers the security of the virtual machine at run time)

4) symbol Reference validation (the class, field, method in the symbol reference can be accessed by the current class, private, protected, etc.)

Get ready

The prep phase is a phase that formally allocates memory for class variables and sets the initial value of class variables, and the memory used by these variables is allocated in the method area. In this case, the memory allocation includes only class variables (static modified variables), not instance variables, and instance variables in the Java heap. The initial value here is the 0 value of the data type (int is 0). However, if the Constantvalue property exists in the field property sheet of the Class field, the value of this variable is initialized to the value specified by Constantvalue (static final int).

Analytical

The parsing phase is the process by which a virtual machine replaces a symbolic reference within a constant pool with a direct reference.

Symbol reference (Symbolic References): Describes the referenced target in a set of symbols that are independent of the memory layout implemented by the virtual machine, and that the referenced target is not necessarily loaded into memory. Symbolic references are clearly defined in the Java Virtual Machine specification's class file format.

Direct References: Related to the memory layout implemented by the virtual machine, either a pointer to the target directly, a relative offset, or a handle that can be indirectly anchored to the target. If there is a direct reference, the reference target must already exist in memory.

Initialization

The class initialization phase is the last step in the class loading process, before you actually start executing the Java program code (or bytecode) defined in the class. The initialization phase is the process of executing the class constructor <clinit> () method.

In-depth understanding of Java Virtual Machines-Learning notes-Virtual machine class loading mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.