Loading like Android Growth Series

Summary: Android Xiaobai's Growth Path_Knowledge System Summary【Continuously updated...】

What is ClassLoader

ClassLoaderIt is the class loader. The virtual machine cannot directly load the class bytecode file. It needs to process the bytecode file through a series of processes to finally form an object ClassLoaderthat the virtual machine can directly read.Class

Class loading process

The class loading process can be simply summarized as:

  • Loading: Load bytecode files ClassLoaderfrom various sources into memory through differentClass
  • Verification: verify whether the loaded byte stream conforms to the specification of the virtual machine, and prevent non-standard byte streams generated by special processing from being loaded into memory
    • File format verification: Verify that the byte stream conforms to Classthe specification of the file format and can be processed by the current version of the virtual machine.
    • Metadata verification and bytecode verification: semantic analysis of bytecode and analysis of data flow to ensure correct code logic
    • Symbol reference verification: whether the class described in the form of a string exists, and whether the definition of the class conforms to the specification
  • Preparation: Allocate memory and assign initial values ​​to class variables (modified staticvariables). The default initial value here is not the initial value written in our code, but the initial value assigned by the java virtual machine according to the type, for example, the initial value of int is 0
  • Analysis: Replace the symbolic references in the constant pool with direct references, and the virtual machine replaces all symbolic references such as class names, method names, and field names with specific memory addresses or offsets
  • Initialization: Initialize class variables, mainly perform initialization of static variables, including assignment of static variables and execution of static initialization blocks

Parental Delegation Mechanism

  • For two classes, if and only if both classes come from the same Class file and are loaded by the same class loader, the virtual machine considers them to be the same class
  • Parent delegation mechanism:
    • Currently, ClassLoaderfirst query whether this class has been loaded from its own loading cache, and return this class directly if it has been loaded
    • If it has not been loaded at present ClassLoader, entrust the parent class loader to load, and the parent class loader also adopts the same strategy, first check its own cache, if not, continue to entrust the parent class loader to load, until the highest level class loader
    • When all parent class loaders are not loaded, the current class loader will load it and put it in its own cache
  • The parent class loader does not refer to the current ClassLoaderparent class, but refers to the class corresponding to the current field ClassLoader.parent
  • The role of the parent delegation mechanism:
    • Prevent repeated loading of the same Class: the loaded Class will be returned directly
    • Ensure that the Class of the core library is not tampered with: the Class of the core library is loaded by the high-level parent class loader. Even if we change a class of a core library and use the class loader to load it, it will be loaded by the parent class loader. If the parent class loader has already loaded this class, it will directly return the loaded one to avoid tampering with the core library
  • The method of destroying the parental delegation mechanism: customize the ClassLoader and rewrite loadClassthe method, and implement your own logic without following the parental delegation mechanism

ClassLoader in Java

insert image description here

  • BootstrapClassLoader: The top-level loading class, which mainly loads the core class library, usually jre/libthe class library in the JDK directory
  • ExtensionClassLoader: Its parent class loader is , it mainly loads the class library in the directory BootstrapClassLoaderunder the JDK directory , and then replaces it withjre/lib/extJAVA 9PlatformClassLoader
  • AppClassLoader: Application class loader, its parent class loader is responsible for loading all files and files under the ExtensionClassLoaderapplication directoryCLASSPATHjarClass

ClassLoader in Android

insert image description here

  • Since the Class file is no longer loaded in Android, but Dexthe file, there is no ExtensionClassLoader and AppClassLoader in Android
  • BootClassLoader: Created when the system starts, passed in when the App starts, used to load some Frameworkclasses required by the system level
  • DexClassLoaderAnd PathClassLoader: APK, DEX and JAR can be loaded from the directory. The difference between the two is that PathClassLoader does not set it optimizedDirectoryto Null, that is, it does not set the optimized storage path. In fact, the default path when optimizedDirectory is null is /data/dalvik-cachethe directory. PathClassLoader is a class used to load Android system classes and applications. And DexClassLoader can specify optimizedDirectory, but after it arrives Android 8.0之, this field is deprecated, which means that DexClassLoader specifying optimizedDirectory will not work

Class loading application scenarios

  • hot load
  • encrypted protection

Guess you like

Origin blog.csdn.net/Nbin_Newby/article/details/120888325