Author: threedayman

Source: Hang Seng LIGHT Cloud Community

The theoretical knowledge

When a type is loaded into vm memory and unloaded from memory, 2. Its life cycle must go through Loading, Verification, Preparation, Resolution, Initialization, Using and Unloading. The three parts of validation, preparation and parsing are collectively referred to as Linking.

The Java Virtual Machine Specification specifies that there are only six cases in which classes must be “initialized” immediately (and loading, validation, and preparation naturally need to begin before then).

  • When you encounter four bytecode instructions — new, getstatic, putstatic, or Invokestatic — you need to trigger initialization if the type has not already been initialized

When an object is instantiated using the new keyword.

Reads or sets a static field of a type (except static fields that are modified by final and have put the result into the constant pool at compile time).

When a static method of a type is called.

  • When a reflection call is made to a type using the java.lang.Reflect package’s methods, initialization needs to be triggered if the type has not already been initialized.
  • When initializing a class, if the parent class has not been initialized, the initialization of the parent class must be triggered first.
  • When the virtual machine starts, the user needs to specify a primary class (the one containing the main() method) to execute, and the virtual machine initializes this primary class first.
  • When using the new dynamic language support in JDK 7, If a Java lang. Invoke. The final analytical results for MethodHandle instance REF_getStatic, REF_putStatic, REF_invokeStatic, REF_newInvokeSpecial handle four kinds of methods, If the class corresponding to the method handle has not been initialized, it needs to be initialized first.
  • When an interface defines a new JDK 8 default method (an interface method decorated with the default keyword), if any of the interface’s implementation classes are initialized, the interface should be initialized before it.

Load (Loading)

  • Gets the binary byte stream that defines a class by its fully qualified name.
  • Transform the static storage structure represented by this byte stream into the runtime data structure of the method area.
  • Generate a java.lang.Class object in memory that represents the Class and acts as an access port for the Class’s various data in the method area.

validation

  • File format verification: Verifies that the byte stream complies with Class file format specifications and can be processed by the current version of the VIRTUAL machine.
  • Metadata verification: semantic analysis of the information described by bytecode to ensure that the information described conforms to the requirements of the Java Language Specification.
  • Bytecode validation verifies that program semantics are legitimate and logical.
  • Symbolic reference validation, which verifies that some external class, method, field, or other resources on which it depends are missing or denied access, occurs during the parsing phase of converting symbolic references to direct references.

To prepare

The phase that allocates memory for class variables and sets their initial values.

parsing

The process by which the Java virtual machine replaces symbolic references in a constant pool with direct references.

Initialize the

During the preparation phase, variables have already been assigned the initial zero value required by the system. During the initialization phase, class variables and other resources are initialized according to a subjective plan made by the programmer through the program code.

Instance to explain

When does the JVM load our class? From a practical point of view, this is when you use this class in your code. For example, let’s say you have a class (userService.class) with a “main()” method as the main entry. Once your JVM starts, it must load your class (UserService.cass) into memory and execute it from the entry code of the “main()” method.

public class UserService {
    public static void main(String[] args) {
    }
}
Copy the code

Then we add the following code

public class UserService { public static void main(String[] args) { CarManager carManager = new CarManager(); }}Copy the code

As you can see, the application needs to use CarManager, which triggers the JVM to load CarManager into memory for use.

validation

Simply put, this step is to verify that the contents of the “.class “file you load conform to the specified specification against the Java virtual machine specification. In case the class file is corrupted or modified so that the JVM cannot execute the bytecode.

To prepare

The preparation phase allocates memory for the class, allocates space for the class variable, and assigns an initial value.

public class UserService {
    private static int age;
}
Copy the code

The age variable is assigned an initial value of 0 during the preparation phase.

parsing

Replace a symbolic reference with a direct reference

Initialize the

Let’s take a look at the following code, when we passed the Configuration. The getConfiguration (sys. User. “age”) and access to the value assigned to the age?

public class UserService {
    private static int age = Configuration.getConfiguration("sys.user.age");
}
Copy the code

The answer is the initialization phase. We will be the Configuration getConfiguration (” sys. User. Age “) values for and assigned to the class variable age.

That’s all for this episode. I’m looking forward to learning about class loading and communicating in the comments section.

reference

In-depth Understanding of the Java Virtual Machine

Fire Captain -JVM column