Life cycle Learning for JVM types

Source: Internet
Author: User

A Java Virtual machine is loaded, connected, and initialized with a Java type so that the type can be used by a running Java program in which the load is to read the binary form of the Java type into the Java virtual machine; is to merge this type of data, which is read into the virtual machine's binary form, into the runtime state of the virtual machine.

The connection phase is divided into three sub-steps---- validation, preparation, and parsing .

The validate step ensures that the Java type data is well-formed and is suitable for use with Java virtual machines.

The prepare step is responsible for allocating the required memory for the type, such as allocating memory for its class variables.

The resolve step is responsible for converting the symbolic references in the constant pool to direct references.

The implementation of a virtual machine can delay parsing this step by parsing it when a running program actually uses a symbolic reference (converting the symbolic reference to a direct reference). When the validation, preparation, and (optional) Resolution steps are complete, the type is ready for initialization. The variable is assigned the appropriate initial value during initialization.

All Java Virtual machine implementations must be initialized at the first active use of each class or interface. The following six scenarios meet the requirements for active use.

(1) When a new instance of a class is created (or by executing the new instruction in bytecode, or by ambiguous creation, reflection, cloning, or deserialization)

(2) When a static method of a class is called (that is, when the invoke, static command is executed in the bytecode)

(3) when a static field of a class or interface is used, or when the field is assigned a value, except for the static field that is final decorated, it is initialized to a compile-time constant expression.

(4) When invoking some of the reflection methods in the Java API, such as methods in class classes or class methods in a Java.lang.reflect package.

(5) When initializing a subclass of a class (When a class initializes, it is required that its hyper-exhaustion has been initialized)

(6) when the virtual machine starts , it is called the class of the startup class (that is, the class that contains the main () method)

Loading

The load phase consists of three basic actions, to load a type, the Java Virtual machine must:

(1) a binary data stream representing the type is produced by the fully qualified name of the type.

(2) parse this binary data stream as a method to go inside the internal data structure.

(3) creates an instance of the Java.lang.Class class that represents the type.

Verify

When the type is loaded, it is ready to be connected. The first step in the connection process is to verify that the---confirms that the type conforms to the semantics of the Java language, and that it does not compromise the integrity of the virtual machine. The entire process of checking whether the type being loaded has any problems is verified.

Another very likely check at load time is to make sure that every class except for object has a superclass . The reason for checking at load time is that when a virtual machine loads a class, it must ensure that all the superclass of that class have been loaded.

In most virtual machine implementations, there is also a check that occurs after the formal validation phase, which is the validation of the symbol reference . As described in the previous chapters, the process of dynamic linking involves locating referenced classes, interfaces, fields, and methods by means of symbolic references saved in a constant pool, replacing symbolic references with direct references. When a virtual machine searches for a (type, field, or method) that is referenced by a symbol, it must first confirm that the element exists.

Candidate checks that need to be completed during the formal validation phase are listed below:

(1) Check that the final class cannot have subclasses.

(2) Check the final method cannot be overwritten.

(3) Ensure that there is no incompatible method declaration between the type and the superclass (for example, two methods have the same name, the parameters are the same in number, order, and type, but the return type is different) here the superclass needs to be initialized before the subclass is initialized.

(4) Check that all constant pool entrances are consistent with each other.

(5) Check the integrity of the bytecode.

(6) Check that all special strings in the constant pool conform to the format.

Get ready

During the preparation phase, the Java virtual machine allocates memory for the class variable and sets the default initial value. However, before the initialization phase is reached, the class variable is not initialized to the true initial value. The Java Virtual machine implementation may also allocate memory for some data structures in order to improve the performance of the running program. The ray of this data structure, such as a method table, contains pointers to each method in the class, including methods inherited from the superclass.

Analytical

The type passes through the first two stages of the connection---, and it can enter the parsing phase. Parsing is the process of looking for a symbolic reference to classes, interfaces, fields, and methods in a constant pool of types, replacing those symbolic references with direct references.

Initialization

A class consists of two steps:

1, if the exhaustion exists directly superclass, and the direct superclass has not been initialized, first initialize the direct super class.

2. If there is a class initialization method that is exhausted, execute this method.

When initializing a direct superclass of a class, this is the two steps involved. Therefore, the first initialized class is always object and is then actively used by all classes on the inheritance tree of the class. The superclass is always initialized before the child class.

The Java virtual machine must ensure that the initialization process is synchronized correctly. If multiple threads need to initialize a class, allowing only one thread to perform initialization, the other threads need to wait. When the active thread finishes the initialization process, it must notify the other waiting threads.

Unload type

The virtual machine creates and initializes the object so that the program can use the object, and optionally performs garbage collection after the object becomes no longer referenced. Similarly, the virtual machine mounts, joins, and initializes classes so that the program can use classes, which are optionally unloaded when the program does not reference them.

The garbage collector must find the accessible class instance from the accessible Mythread class object, through its type data in the method go.

Starting with the Mythread object, the garbage collector follows a pointer to the type data of Mythread, which finds:

A reference to the class instance of Mythread in the heap.

A pointer to the type data of the Mythread direct Hyper-interface cloneable.

A pointer to the type data of the Mythread direct superclass thread.

Starting with the Cloneable type data, the garbage collector finds:

A reference to a class instance that points to cloneable in the heap

Starting with the type data for thread, the garbage collector finds:

A reference to a class instance that points to thread in the heap.

A pointer to the type data of the thread's direct hyper-interface runnable.

A pointer to the type data of the thread's direct superclass object.

Starting with the runnable type data, the garbage collector finds:

A reference to a class instance of runnable in the heap.

Starting with the type data for object, the garbage collector finds:

A reference to a class instance that points to object in the heap.

Just one instance of the mythread that can be touched, the garbage collector can touch mythread and all of its super-type class instances.

Reference: "Deep Java Virtual machine"

Life cycle Learning for JVM types

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.