Preliminary understanding of JVM

Location of the JVM

 

JVM memory map (. java file compilation process)

 

 

 

Detailed architecture diagram:

The whole process of class loading:

 

 

 

 

Function of class loader:

 

1) Root class loader: it is used to load the core classes of Java. It is implemented in native code and does not inherit from Java lang.ClassLoader
(responsible for loading all classes in jre/lib/rt.jar in $JAVA_HOME, implemented by C + +, not subclasses of ClassLoader).
Since the boot class loader involves the local implementation details of the virtual machine, the developer cannot directly obtain the reference of the boot class loader, so it is not allowed to operate directly through the reference.

 

2) Extensions class loader: it is responsible for loading the extension directory of JRE, lib/ext or Java The class of the JAR package in the directory specified by the ext.dirs system property.
Implemented by the Java language, the parent class loader is null.


3) System class loader: it is called system (also known as application) class loader. It is responsible for loading the - classpath option from Java command and Java class. Path system properties
Or the CLASSPATH will replace the JAR package and CLASSPATH specified by the variable.
The program can obtain the system class loader through the static method getSystemClassLoader() of ClassLoader.
If not specified, all user-defined class loaders take this kind of loader as the parent loader.
Implemented by Java language, the parent class loader is ExtClassLoader.





Loading Class by Class loader roughly goes through the following 8 steps:

  1. Check whether this Class has been loaded, that is, whether there is this Class in the buffer. If so, go to step 8 directly, otherwise go to step 2.
  2. If there is no Parent loader, then either the Parent is the root loader or the Parent itself is the root loader. Skip to step 4. If the Parent loader exists, go to step 3.
  3. Request to use the parent class loader to load the target class. If the loading is successful, skip to step 8, otherwise follow step 5.
  4. Request to use the root class loader to load the target class. If the loading is successful, skip to step 8, otherwise skip to step 7.
  5. The current Class loader attempts to find the Class file. If it is found, execute step 6. If it is not found, execute step 7.
  6. Load Class from the file, and then skip to step 8.
  7. Throw ClassNotFountException exception.
  8. Return the corresponding Java Lang. class object.




JVM class loading mechanism:

1. Overall responsibility: the so-called overall responsibility means that when a Class loader is responsible for loading a Class, other classes that the Class depends on and references will also be loaded by the Class loader, unless it is shown that another Class loader is used for loading.

2. Parent delegation: the so-called parent delegation is to let the parent loader try to load the Class first, and only try to load the Class from its own Class path when the parent loader cannot load the Class.
Generally speaking, when a specific class loader receives a request to load a class, it first delegates the loading task to the parent loader and recurses in turn. If the parent loader can complete the class loading task, it will return successfully;
Only when the parent loader cannot complete this loading task can it load it by itself.

3. Caching mechanism: the caching mechanism will ensure that all loaded classes will be cached. When a Class needs to be used in the program, the Class loader will first search the Class from the cache,
Only when the Class object does not exist in the buffer, the system will read the binary data corresponding to the Class, convert it into a Class object and store it in the buffer.
This is why after modifying the Class, the JVM must be restarted before the changes made by the program will take effect.

4. Sandbox security mechanism: prevent malicious code from polluting java source code. For example, I defined a class named String and its package is Java Lang, because this class originally belongs to jdk,
If there is no sandbox security mechanism, this class will pollute all my strings. However, due to the sandbox security mechanism, I entrust the top-level bootstrap loader to find this class,
If not, delegate extsion. If not, go to aapclassloader. However, since String is the source code of jdk, it will be loaded in bootstrap. Find it first and use it first,
Therefore, the String in bootstrap is used, and none of the latter can be used, which ensures that it will not be polluted by malicious code

Description of native keyword:

As long as you bring the native keyword, it means that the scope of java operation cannot be reached. If you go back to call the C language library, you will enter the local method stack and call the local method interface (JNI). java has opened a special tag area (local method stack) in the memory area to register the native methods. When finally executing, the methods in the local method library are loaded through JNI. Native is a rare thing, which is not common

Code example

 1 package java.lang;
 2 
 3 /**
 4  * @Author five five
 5  * @Created on October 8, 2020
 6  * Parental delegation mechanism: Security
 7  * 1.APP-->EXC-->BOOt(Final execution)
 8  */
 9 public class String {
10     public String toString() {
11         return "hello";
12     }
13 
14     /**
15      * @param args
16      * error:Error: in class java main method not found in lang.string
17      */
18     public static void main(String[] args) {
19         System.out.println("123132123");
20     }
21 }
 1 package com.edu.jvm;
 2 
 3 /**
 4  * Class loader classification:
 5  *      1.Loader of virtual machine
 6  *      2.Start class (root) loader
 7  *      3.extensions class loader 
 8  *      4.Application Loader 
 9  * @Author five five
10  * @Created on October 8, 2020
11  */
12 public class Test02 {
13     public static void main(String[] args) {
14         Test02 test01 = new Test02();
15         Test02 test02 = new Test02();
16         Test02 test03 = new Test02();
17         Test02 test04 = new Test02();
18         System.out.println(test01.hashCode());
19         System.out.println(test02.hashCode());
20         System.out.println(test03.hashCode());
21         System.out.println(test04.hashCode());
22         System.out.println("=======================================");
23         System.out.println(test01.getClass().hashCode());
24         Class<? extends Test02> aClass = test02.getClass();
25         ClassLoader classLoader = aClass.getClassLoader();
26         System.out.println(classLoader);//AppClassLoader    /
27         System.out.println(classLoader.getParent());//ExtClassLoader    /jre/lib/ext
28         System.out.println(classLoader.getParent().getParent());//java Program cannot get    /rt.jar
29     }
30 }
 1 package com.edu.jvm;
 2 
 3 /**
 4  * @Author five five
 5  * @Created on October 8, 2020
 6  */
 7 public class Test03 {
 8     public static void main(String[] args) {
 9         new Thread(()->{
10             System.out.println("The thread started");
11         }).start();
12     }
13     //Just bring it native Keyword description java The scope of work cannot be reached,Back call C Language library
14     //It will enter the local method stack and call the local method interface(JNI)
15     //java A special marking area is opened in the memory area(Native Method Stack ),Used to register native method
16     //In the final execution, the methods in the local method library are loaded through JNI
17     //It's a rare thing
18     public native void sayHello();
19 }

 

Tags: Java jvm

Posted by p2grace on Thu, 12 May 2022 21:26:57 +0300