当前位置:网站首页>JVM easy start-01

JVM easy start-01

2022-06-23 10:25:00 cfcoolya

One . Study JVM The benefits of

  • Be able to understand why Java The earliest is called interpretative language
  • In order to better solve online troubleshooting problems in the future
  • Can be adjusted by JVM Relevant parameters are improved Java Application performance
  • It's clear that Java How the program is executed

Two .JVM Architecture diagram

3、 ... and . Class loader ClassLoader

3.1 Class loading 、 Connect 、 initialization

First, let's explore a class loaded into JVM A basic structure of :

class Enter class loader , Load initialization as class template , Instantiate as instances of classes .

Understand from code :

class Test{ 
  public static int a = 1; 
}// What is given in our program is  public static int a = 1;
 // However, the steps in the loading process are as follows : 

1.  Loading phase   The compiled file is  .class file , Then load... Through the class , Load into JVM【 Find and load the binary data of the class 】

2.  Connection phase  
   First step ( verification ): Make sure Class Class files are OK  【 Make sure the class being loaded is correct 】
   The second step ( Get ready ): Initialize to  a=0.( Because of you int The initial value of the type is 0) 【 Allocate memory for static variables of a class , And initialize it to the default value 】
   The third step ( analysis ): Convert reference to direct reference 【 Convert symbolic references in a class to direct references 】

3.  Initialization phase :  Through this parsing stage , hold 1 Assign to variable a

3.2 Classification of class loaders

  1. BootstrapClassLoader( Root loader )sun.boot.class.path Load the package of the system , Package line jdk Classes in the core library
  2. ExtensionClassLoader( Extend the classloader )java.ext.dirs Load extension jar The class in the package
  3. AppClassLoader( Application class loader )java.class.path Load the classes you write , Compiled classes
  4. Java.long.ClassLoader Subclasses of , The user can customize the loading method of the class

3.3 Parent delegate mechanism

How the parental delegation mechanism works :

Let the parent class load layer by layer , The topmost parent class cannot be loaded down , By analogy .

  1. Class loader received a class load request
  2. Delegate this request all the way up , Until the root class loader ( Start the loader )
  3. The class loader checks to see if it can load , Load as soon as you can , Otherwise, notify the child loader to load
  4. Repeat step 3
public class ClassLoaderDemo {
     public static void main(String[] args) {
          System.out.println("ClassLodarDemo's ClassLoader is " +ClassLoaderDemo.class.getClassLoader());
          System.out.println("The Parent of ClassLodarDemo's ClassLoader is " +
          ClassLoaderDemo.class.getClassLoader().getParent());
          System.out.println("The GrandParent of ClassLodarDemo's ClassLoader is " +
          ClassLoaderDemo.class.getClassLoader().getParent().getParent());
   }
}

ClassLodarDemo's ClassLoader is [email protected]
The Parent of ClassLodarDemo's ClassLoader is [email protected]
The GrandParent of ClassLodarDemo's ClassLoader is null

benefits :

  1. To ensure the Java The documentation of the program runs , Avoid duplicate loading of classes

  2. To ensure the Java At the heart of API Not tampered with

Learning comes from :B standing - Madness theory  

原网站

版权声明
本文为[cfcoolya]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231006570620.html