JVM class loader depth analysis and analysis of important characteristics

1, a flowchart class loading

Loaded from disk to complete the process of destruction.

 

2, Scheme 2 class loading

1. Load: is the binary form of type java read java virtual machine

2, connected to: verification, preparation, resolution.

Connection that has been read into memory to run binary data type and merge into a virtual machine environment in

  verification:

    Check the structure of the class file

    Semantic checks

    Bytecode verification

    Binary compatibility verification

  Preparation: to allocate memory for the class, set the default value. But before reaching the initialization, class variables are not initialized to the initial value of the real.

  Analysis: parsing process is to find the symbol classes, interfaces, fields and methods of the type cited in the constant pool, replacing these symbolic references to processes directly referenced.

3, initialization: assigns an initial value to the true class variable

4, class instantiation: 

  Allocate memory for the new object

  Assign default values ​​for instance variables

  An instance variable assigned the correct initial value

  java compiler to compile each of its class generating at least one instance of the method, the java class file in the instance initialization method is called "<init>". The method of construction for each class in the source code, java compiler will generate a <init> method.

5, garbage collection and object finalization

 

3, the class loading

The final product is a class Class object loaded in memory of

Class objects encapsulate data structure in the region class methods, and data structure provides access to the area method of Java programmers

 

4, class loader

There are two types of class loader

1) Java Virtual Machine that comes with loader

  Root class loader (on Bootstrap)

  Extension class loader (the Extension)

  System (Application) class loader (System)

2) user-defined class loader

  Java.lang.ClassLoader subclass

  Users can customize the type of loading

 

5, the class loader is not required when a class is the "first active use" wait until it is loaded.

JVM规范允许类加载器在预料某个类将要被使用时就预先加载它,如果在预先加载过程中遇到了.class 文件缺失或存在错误,类加载器必须在程序主动使用该类是才报告错误(LinkageError错误

如果这个类一直没有被程序主动适应,那么类加载器就不会报告错误

 

6、类的准备、类的初始化

在准备阶段,Java虚拟机为类的静态变量分配内存,并设置默认初始值。对于一下Sample类,在准备阶段,将为int类型的静态变量a分配4个字节的内存空间,并且赋予默认值为0,为long类型的静态变量b分配8个字节的内存空间,并且赋值0.

 

在初始化阶段,Java虚拟机执行类的初始化语句,为类的静态变量赋予初始值。在程序中,静态变量的初始化有两种途径: (1)在静态变量声明处进行初始化 (2)在静态代码块中进行初始化。例如下面Sample类的代码中,静态变量a和b都被显示初始化,而静态变量c没有被显示初始化,它将保持默认值0

 

在初始阶段,静态变量的声明语句,以及静态代码块都被看作类的初始化语句。Java虚拟机会按照初始化语句在类文件中的先后顺序来依次执行它们。例如下面的Sample2类被初始化后,它的静态变量a的取值为4.

public class Sample2 {
    static  int a = 1;
    static {
        a = 2;
    }
    static {
        a = 4;
    }

    public static void main(String[] args) {
        System.out.println("a=" + a); //打印a=4
    }
}

  

7、类的初始化步骤

1) 假如这个类没有被加载和连接,那就先进行加载和连接

2)假如类存在直接父类,并且这个父类还没有被初始化,那就先初始化直接父类

3)加入类中存在初始化语句,那就依次执行这些初始化语句。

 

8、当Java虚拟机初始化一个类时,要求它的所有父类都已经初始化,但是这条规则不适于接口

1) 当初始化一个类时,并不会先初始化它所实现的类的接口。

2) 在初始化一个接口时,并不会先初始化它的父接口

因此,一个父接口并不会因为它的子接口或者实现类的初始化而初始化。只有当程序首次使用特定接口的镜头变量时,才会导致该接口的初始化。

 

9、类的初始化时机

只有当程序访问的静态变量或者静态方法确实在当前类或当前接口中定义时,才可以认为是对类或接口的主动使用。

调用ClassLoader类的loadClass方法加载的一个类,并不会对类的主动使用,不会导致类的初始化。

Guess you like

Origin www.cnblogs.com/linlf03/p/10990649.html