cool hit counter ClassLoader's working mechanism_Intefrankly

ClassLoader's working mechanism

In this article, we focus on the working mechanism of the class loader

One: What is a class loader in the first place?

   Class loader is used to load the java classes into the java virtual machine. The java source program is compiled to form a bytecode file, which the class loader loads into memory and converts into an instance object of java.lang.Class.

   The JVM is based on stack operations: all operations go through stack-in and stack-out operations. The advantage of stack-based operations: combining run-time optimization efforts with an execution engine that performs compile-time optimizations serves the purpose of optimizing Java bytecode.

II: Role of the class loader.

  1. The compiledclass The bytecode file is loaded into theJVM in。 will review which class loader each class has loaded during the loading process? Which class to load? There is actually a parent-first hierarchy
  2. Reparse the bytecode file into the object format required by the JVM uniformly.

III: Classification of class loaders

  •   BootStrap ClassLoader: The loading process is performed by theJVM self-determined, Exactly how to load, Which class to load is the one withJVM self-controlled, He doesn't actually fit.JVM standardize, There is no hierarchical structure, No parent loading utensil, No subloader either。 He exists only as a loading tool
  • ExtClassLoader: Although he himself isJVM part of, But loading isn't thereJVM Self-loading, The specific target he serves is inSystem.getProperties("java.ext.dirs") directory
  • APPClassLoader : beExtClassLoader Subclasses of the class loader, The main load is located atclasspath Classes in the directory System.getProperties("java.class.path")

IV: Two ways the JVM loads bytecode files

  1. Implicit loading: the class loader is not called and the needed class is loaded into memory automatically e.g. implicit loading is triggered when the current class needs a reference to an external class
  2. Explicit loading: Loading done by calling this.getClass(), this.getClassLoader(), Class.forName(class), etc. is explicit loading

The two loading methods can be mixed, loading a custom class explicitly and triggering an implicit load if that class has references to other classes.

V: Analysis of common errors in class loading

  • ClassNotFoundException: When explicitly loading a bytecode file, the corresponding bytecode file is not found. The reason for this problem is that there may not be a corresponding bytecode file under the corresponding classpath, resulting in the occurrence of the exception; Solution: Check whether there is a corresponding bytecode file under the corresponding classpath by this.getClass().getClassLoader().getResources().toString(); Get the path of path
  • NotClassDefFoundError: class does not exist exception Generated by: new keyword, reference class, inheritance interface or class, method parameters with references, these can lead to the occurrence of such an exception. A class not present exception may occur when loading these classes implicitly.
  • ClassCastException: this error occurs when forcing a type conversion   

   Automatic checking is performed when the JVM performs type conversions

  1.    Common object: must be an instance object or subclass object of the target class; in the case of an interface, the object is a subclass object of that interface
  2. array object: the target class must be of type array or Object under the java.lang package , Clonable, Serializable under the package

   Methods for resolving type conversion exceptions: explicitly specify the object type; determine if it is the target object type by instanceof, and then perform the type conversion.

VI: The architecture of the JVM and how it works

  • JVM is an architecture that achieves the computing capabilities a real computer has by emulating a real computer. Computer-centric architecture of real computers
    •    Instruction set: the set of all commands in a machine language that a computer can recognize
    • Calculation unit: a functional module that recognizes and controls the execution of instructions
    • Registers: the core components of the CPU, used for temporary storage, instructions, addresses and data
    • Storage unit: a unit capable of storing computer operands and operational structures, e.g., memory and disk
    • Addressing methods: the range of addresses, the minimum and maximum address ranges, and the rules of operation for addresses
  •    JVM architecture.
    •    Execution engine: equivalent to the CPU, controlling instruction execution. Parse the bytecode file and get the parsed result.
    • pc register: a pc register is created when each thread is started. The register holds the address of the currently executing JVM instruction. The register that holds the address of the next instruction to be executed is: pc register. He always keeps the address of the next instruction to be executed. The address can be a local pointer or the address in the method relative to the method start instruction.
    • Local method stack: the area where native methods are stored
    • Heap: holds the created object instances. All class objects are created by new, and the creation of an object creates a reference to that object on the stack.
    • Method area: also called static area: holds method data, classes, static variables, static methods, constants and member methods
    • Runtime constants pool: holds constants for classes and interfaces, in addition to references to member methods and member variables. It is through these references that the JVM runtime finds the actual address

1、witty machine in learning Introductory tutorial on convolutional neural networks 1
2、Sogou input method and shortcut CtrlSpace conflict resolution under Linux
3、How to get involved in large open source projects from scratch
4、uSens LingSense Chen Jiajing gesture recognition technology has emerged in ARVR mobile gesture tracking is expected to usher in new growth in 2018Magnetics please speak
5、Lists from Googles Guava Profile

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送