In-depth understanding of the JVM class loader

---
title: [learning] in-depth understanding of .md JVM class loader
DATE: 2019-10-20 22:20:06
Tags: the JVM class loading
---

Java class loading, connectivity, initialization is performed during program run

## Java Virtual Machine with the program life cycle

1. Perform System.exit () method
2. The program ends normally
3. encounters an exception or error termination
4. As the virtual machine operating system or program error process

The above situation can end life cycle

## Java class loading mode

1. The local system is loaded directly
2. .class file downloaded via the network
3. Through the zip, jar, etc. loaded archive
4. .class files loaded from a proprietary database
5. java files or .class files obtained dynamically compiled source file

What class loader ##

- class loader system comes
- root class loader (ClassLoader on Bootstrap)
- extension class loader (the Extension ClassLoader)
- system class loader (the System ClassLoader)
- user-defined class loader
- achieved by custom ClassLoader class loader

Subclass of java.lang.ClassLoader to implement a custom class loader through ClassLoader, this process can be loaded ** define the class ** ** ** load timing and the loading process to do something.

## class loading process

** class load refers to the binary data read .class file class into memory. Put it in run-time data area of ​​the zone method, and then create a java.lang.Class object in memory (virtual machine specification does not specify where the class object, HotSpot virtual machine on which the method area) is used to package the class data structure in the process zone **

- Load

Find binary data and loads the class of

- Connection

- Verify

** ** ensure the correctness of the loaded class

- Prepare

Is ** ** static class variables allocated memory, and initializes the default values ​​as ** **

- Analysis

The class ** ** for doing symbolic reference is a direct reference ** **

- Initialization

Static variables like giving the correct value ** **

## use a Java program for the class

All java virtual machine implementation must be initialized before each class or interface is the first initiative to use Java programs ** **

-XX: + TraceClassLoading can view the class loader

- Active use is initialized (7)

1. Create an instance of the class
2. The class or interface access to a static variable or a static variable of the class assignment
static method call the class 3.
4. reflection (the Class.forName ( "com.test.Test"))
. 5 subclasses initialize a class
6. if the java virtual machine start, is indicated as the start classes based
analysis result REF_getStatic example 7. JDK1.7 provided, REF_putStatic, REF_invokeStatic handle corresponding class is not initialized initializing

- passive use initialization

> For static fields, only ** ** it will directly define the class of the field is initialized

~~~java
public class Mytest1 {
public static void main(String[] args) {
System.out.println(Mychirld1.str);
}
}

/ **
* For static fields, only directly defines the class of the field will be initialized
* /
class Myparent1 {
public static String str = "the Hello world!";

static {
System.out.println("current in Myparent1");
}
}

class Mychirld1 extends Myparent1{
public static String str2 = "hello world2 !";
static {
System.out.println("current in Mychirld1");
}
}


>>>>>>
current in Myparent1
hello world !
~~~

Use the main method in a subclass to call the parent class's static variables directly because only defines classes of the field will be initialized, initialize static code block print content of the parent class and subclass does not initialize

> When a class initialization, all initialized parent class requirements

This is the active use, it will initialize

~~~java
public class Mytest1 {
public static void main(String[] args) {
System.out.println(Mychirld1.str2);
}
}

/ **
* For static fields, only directly defines the class of the field will be initialized
* /
class Myparent1 {
public static String str = "the Hello world!";

static {
System.out.println("current in Myparent1");
}
}

class Mychirld1 extends Myparent1{
public static String str2 = "hello world2 !";
static {
System.out.println("current in Mychirld1");
}
}


>>>>>>
current in Myparent1
current in Mychirld1
hello world2 !
~~~

static method called main variables subclasses, subclasses active initialization, class load order the class will be initialized according to the parent, so the first print something static block of code in the parent class, subclass of the printing.

> The modified final variables

When a main method references are final modified static variables will automatically trigger a class of its initialization it?

~~~java
public class Mytest2 {
public static void main(String[] args) {
System.out.println(Myparent2.str);
}
}

class Myparent2 {
public static final String str = "hello world !";

static {
System.out.println("current in Myparent1");
}
}

>>>>>
hello world !
~~~

The answer is no,

Relationship because the final modified as a constant, will compile phase, will be placed on the constant pool where the constant method calls the class. Essentially, classes, and constants defined in the constants class has no direct reference to the call , we will delete Myparent2.class compile time, run the main method will print hello world!

 

> Array instance is created

~~~java
public class Mytest3 {
public static void main(String[] args) {
Myparent3[] myparent3s = new Myparent3[1];
}
}

class Myparent3 {
static {
System.out.println("current in parent3");
}

}
~~~

What this case are not output because the arrays for example, its type is generated dynamically at runtime by the JVM

> Interface Initialization

The default interface is a public static final field

*** When an interface is initialized, the parent is not required to complete the initialization interfaces are only really used when the parent interface (such as a reference to the time constant defined by the interface), will be initialized (but sub-interface initialization, parent interface will be loaded) ***

~~~java
public class Mytest5 {
public static void main(String[] args) {
System.out.println(Mychirld5.b);
}
}

interface Myparent5 {
public static final int a = 7;

}

interface Mychirld5 extends Myparent5 {
public static final int b = new Random().nextInt(2);
}


>>>>
by -XX: + TraceClassLoading view the parent whether the interface is initialized
~~~

 

> Mnemonic

`
/ **
* Mnemonic
*
* IDC shows a int, float or of type String constant value from the constant pool are pushed to the stack
* bipush shows a single-byte (-128 to 127) push constant values to the top of the stack
* sipush shows a constant value a short integer (-32768 to 32767) are pushed to the stack
* iconst_1 type int 1 shows a stack push (iconst_1 - iconst_5)
* /
`` `

## to sum up

The whole class loading process

Start ---> has class loader to load classes ---> Connection ----> initialize ---> --- using class> Uninstall

| ----> verification

| ----> prepare

| ----> parse

> Load

The binary type is the type of java java virtual machine reads the red

> Connection

verification:

** ensure the correctness of the loaded class

ready:

1. allocate memory for variables, set a default value has not been reached 2. initialization, class variables are not initialized to the initial value of true

Resolution:

The type of the constant pool, finds the symbol classes, interfaces, fields and methods of reference, references to these alternative processes is directly referenced

> Initialize

Assigned an initial value for the variable

> Class instantiation

1. allocates memory for the new object is assigned a default value of 2. 3. instance variables instance variables assigned to the correct initial value

Guess you like

Origin www.cnblogs.com/ankuo/p/11746360.html