Jvm loading mechanism and the like

jvm

Java class loading mechanism

1. What is the loaded class

Class loading refers to the class .class binary data file is read into memory, the method area into which the runtime data area, and then create a stack in the region java.lang.Class objects, with class data structure encapsulated in the method area. Class loading end product is located in the heap Class object that encapsulates the data structure in class method area, and provides an interface access method of the data structure area of the java programmer.

Class loader does not need to wait until a class is the first time actively used in loading it if, jvm class loader standardized operation when a class is expected to be used will be pre-loaded on it, if encountered during pre-loaded in .class file is missing or there is an error, the class loader to report an error in the program for the first time take the initiative to use the class, if the class has not been actively using the program, the class loader will not report an error.

2. Class lifecycle

Loading the class includes loading, verification, preparation, parsing initialization five stages, in five stages, the loading, verification, preparation, the initialization sequence is determined, the resolution phase is not necessarily, it is in some cases next start after the initialization phase, which is to support dynamic binding java language. Also these stages is sequentially beginning, rather than the complete sequence, because these stages are often carried out mixing interdigitated, or activate another is usually called during the execution phase of a stage.

load
  • To obtain defined by a fully qualified class name of the class binary byte stream
  • Converting the byte stream storage structure represents static run-time data structure in the method area
  • Generating a Java heap representatives of this class java.lang.class objects, a method of accessing a data entry area

Developers can either use the class loader provided by the system to finish loading, you can also define your own class loader to finish loading.

verification

The first step is to verify the connection phase, the aim is to ensure that the information byte stream Class file contains meet the requirements of the current virtual machine and the virtual machine will not jeopardize their own safety.

  • File format validation
  • Metadata validation
  • Bytecode verification
  • Verification of symbolic references
ready

Allocate memory for the formal class variables and class variable initial value setting phase, and these are allocated memory area in the process

  • This memory allocation time contains only class variables ( static ), and does not include the instance variables, instance variables in the heap assigned as a java objects when an object is instantiated
  • Typically the initial value set here is the data type of the default value of zero, instead of the value displayed in the java code is assigned.
  • If the field attribute table class field in ConstantValue property, that is, at the same time be final and static modification, then the preparation stage variable value is initialized.
Resolve

Parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct references. Analysis operation mainly for class or interface, field, class methods, the interface method, the method type, method handle, and the point of call qualifier

Wait.

  • Symbol references: a description of the target set of symbols, may be any literal
  • Direct reference: direct pointer to the object, an indirect or offset relative to the positioning target handle
initialization

Initialization, given the correct initial value for the class of static variables, JVM classes responsible for initializing the main class variables are initialized.

  • Declare a class variable is specified initial value
  • Using static code block is assigned an initial value for class variables

JVM initialization step

  • If the class has not been loaded, the program is loaded and connected to the class
  • If the direct parent class has not been initialized, first initializes its direct parent
  • If there is class initialization statement, the system in order to perform initialization statement.
Lead to class initialization
  • Create an instance of the class, which is the new method
  • Access static variables of a class or interface, or assignment of the static variables
  • Static method call class
  • 反射(Class.forName(“com.qxy.test”)
  • Subclass of initiating a class, parent class will not initialize
  • It is indicated when the java virtual machine is started classes start classes to run a master class directly java.exe command
End of life situations
  • Execute System.exit () method
  • Normal program execution
  • Encountered an exception or an error in the implementation process is terminated
  • Because the operating system error caused the process to terminate the java virtual machine
3. The class loader
Several class loader
  • Boot class loader (Bootstrap Classloader) is responsible for loading on JDK \ jre \ lib or substituted -Xbootclasspath parameter specified path, and can be recognized by the virtual machine library. Start class loader can not be directly referenced java program
  • Extension class loader (Extension ClassLoader): The loader is responsible for loading JDK \ jre \ lib \ ext or java.ext.dirs all libraries, developers can specify system variables directly using the extension class loader
  • Application class loader (Application classLoader): responsible for loading the user class path specified class, if your application does not live their custom class loader, in general, is that the program in the default class loader.
Jvm class loading mechanism
  • Overall responsibility: when a class loader is responsible for loading a class, his class and other class depends also cited by the class loader is responsible for loading, unless it appears from the use of another class loader to load
  • Parent delegate: let the parent class loader tries to load the class, only when the parent class loader can not load the class will try to load your own class from the class path
  • Caching: caching mechanism will ensure that all of the Class will be loaded through the cache, when the program requires the use of a class, the class loader will look for the class start buffer, only the buffer does not exist, the system will read corresponding to such binary data, and converts it to Class object into the buffer.
4. Class loading

There are three ways

  • When the command line to start the application initialization load jvm
  • By ** class.forName () ** dynamic loading
  • By ClassLoader.loadClass () dynamic loading
Difference class.forName () and ClassLoader.loadClass () of
  • Class.forName(): The .class file is loaded into the class beyond the jvm, it will be explained based on the executed blocks static class;
  • ClassLoader.loadClass(): Only do one thing, it is to load the .class file into the jvm not performed in static content, only the will to perform static newInstance block.
  • Class.forName(name,initialize,loader)Function can also be parameterized control whether static load block. And only call the newInstance () method uses call the constructor, create an object class.
5. Delegate parents mechanism
  • 1, when AppClassLoaderthe time to load a class, it first does not own to try to load this class, but the class loader delegates to the parent class loader ExtClassLoaderto complete.
  • 2, when ExtClassLoaderwhen loading a class, it first does not own to try to load this class, but the class loader delegates to BootStrapClassLoadergo to completion.
  • 3, if BootStrapClassLoaderfails to load (for example $JAVA_HOME/jre/libin not find the class), will be used ExtClassLoaderto try to load;
  • 4, if ExtClassLoaderalso fails to load, will be used AppClassLoaderto load, if AppClassLoaderalso fails to load, it will report the anomaly ClassNotFoundException.
effect
  • Multiple copies of the same bytecode prevent
  • Java program to ensure the safe and stable operation
6. custom class loader

Rewriting the findClass () method

Rewrite the loadClass () will destroy the parents delegate mechanism

Published 18 original articles · won praise 2 · Views 371

Guess you like

Origin blog.csdn.net/weixin_40907792/article/details/105193826