The virtual machine loads the data of the description class from the class file into memory, verifies the data, transforms the analysis and initialization, and finally forms the Java type that can be used directly by the virtual section, which is the class loading mechanism of the virtual machine.
The life cycle of classes from being loaded into virtual machine memory to unloading out of memory includes the following: initialization, using-
5 Types of initialization scenarios:
1. When instantiating an object using the New keyword, read or set the static field of a class, except when the static method of the class is called by the final decoration, which is placed in a constant pool by the compiled result. 2. When you use the Java.lang.reflect package method to make a reflection call to a class. (Class.forName ()). 3. If the parent class is not initialized when the subclass is initialized. 4. The class where the main method resides when the virtual machine starts. 5. When using JDK1.7 Dynamic language support, the Java.lang.invoke.MethodHandle instance resolves the result as a ref_getstatic,ref_putstatic,ref_invokestatic method handle, and the corresponding class is not initialized.
Load loading is the first stage of class loading, and the virtual machines complete the following three processes:
1. Get the binary byte stream that defines this class by the fully qualified name of the class. 2. Convert the storage structure of the byte stream into the run-time structure of the method area. 3. Generate a Class object in memory that represents the category as a access entry for various data in the method area.
The purpose of verification is to ensure that the class file byte stream information conforms to the requirements of the virtual machine.
Prepare to assign an initial value to the static modified variable, for example, type int defaults to 0,boolean by default to False.
Resolving a virtual machine replaces a symbolic reference within a constant pool with a direct reference.
Initialization initialization is the last phase of class loading, and the class constructor < Init> () method is executed, noting that the method here is not a construction method. The method will explicitly call the parent class constructor and then assign a value to the class variable and the static statement block in the Java statement order.
Method invocation
Java is an object-oriented language that is polymorphic. So how does a virtual machine know which method to call at runtime?
Static dispatch is a typical static dispatch that determines which method to call, rather than the virtual machine, at compile time. Dynamic dispatch is the time at which a virtual machine runs to decide which method to call, and a method override is a typical dynamic dispatch.
The implementation of a dynamic dispatch: When a method of an object is called, the reference to the object is pushed to the operand stack, and then the bytecode directive invokevirtual to look for the actual type of the reference. If the corresponding method is found in the actual type, and the access permission is sufficient, the method reference is returned directly, otherwise the parent class will be looked up according to the inheritance relationship. In fact, if the subclass does not override the parent class method, the reference to the subclass method directly points to the parent class method.
Classes loaded by the class loader that comes with the Java virtual machine are never unloaded during the lifetime of the virtual machine.
As described earlier, the Java Virtual machine comes with a class loader that includes the root ClassLoader , the extension ClassLoader , and the system ClassLoader .
The Java Virtual machine itself always references these class loaders, which always refer to the class objects of the classes they load, so these class objects are always accessible .
Classes loaded by the user-defined ClassLoader can be unloaded.
The specific examples are:
The loader1 variable and the obj variable indirectly apply the class object representing the sample class, while the objclass variable references it directly.
If the program runs, the left three reference variables are set to NULL, at this time the sample object ends the life cycle, the Myclassloader object end life cycle, the class object representing the sample class also end the life cycle, sample class in the method area of the binary data be uninstalled .
When needed again, it checks to see if the class object is present, if it is used directly, no longer reloads, and if the sample class does not exist, it will be reloaded. The heap area of the Java Virtual machine generates a new class instance representing the sample class (which can be hashed to see if it is the same instance).
Loading and unloading of Java plane question JVM byte code