For a grander grander sight of the JVM (D)

  You have a dream, you got to protect it. People can't do something by themselves; they wanna tell you you can not do it. You want something. Go get it!


This article will introduce more detailed VM class loading mechanism, the main knowledge comes from "in-depth understanding of the Java Virtual Machine."

Class file structure

Any Class file corresponds to only one class or interface definition information, in the Class file is saved information. Class file is a binary stream to a set of 8-bit bytes of base units, each data has a rigorous definition, are arranged in the order of the compact Class file.

The first four bytes of each Class file called "magic number", it is determined whether the file is a file that can be accepted by the virtual machine. Magic Class value file is: CAFEBABE, different files have different magic number used to distinguish, interested students can find relevant information about the review, such as: What is JPEG, text and other magic number Yes.

Class file contains: magic number, constant pool, access flag (class index, parent index, interface index set), field collection, the collection method table, set attribute table.

Class loading process

  From the class is loaded into memory to start a virtual machine, up to unload a memory, its entire life cycle, including: loading, validation, preparation, parsing, initialization, use, uninstall the seven stages.

Load stage is class load in the loading phase, the virtual machine needs to be done:

To obtain such a binary byte stream defined by the fully qualified name of a class;

Static byte stream cable storage structure into a data structure representative of the operation time zone;

Generating a Class object, as data access entry.

Validation is the first step in the connection phase, in order to ensure that the information byte stream Class file contained in line with current requirements of the virtual machine, the virtual machine will not jeopardize their own safety. Authentication verification operation need four stages:

File format validation that meets the Class File Format Specification;

Metadata validation that meets the Java language specification;

Bytecode verification, namely whether the virtual machine load security mechanism;

Symbolic references to verify that the information meets the class itself.

Preparation phase is the official allocate memory for the class variables and class variables set the initial phase value, the variable is worth noting that class instance variables are not included, but the basic data variables, instance variables allocated along with the object when instantiated.

Parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct reference, there are four references to the resolution process:

类接口解析
字段解析
类方法解析
接口方法解析复制代码

Initialization phase is the last step in the process of class loading, class constructor is executed during the method. Not the same as before with the steps that this stage of the procedure is the dominant and controlling, rather than dominate and control virtual machines.

Class loader

In the class loading process "to get the definition of such binary byte by fully qualified name of a class of flow" This action put an external Java virtual machine implementation, the program to determine how to obtain the required categories, code modules implement this action called "class loader."

In the previous article, I mentioned about class and class loader knowledge there are any questions, we completed the basic supplement for parents appointed mechanisms and the like, as well as knowledge of the mechanism of destruction parents delegated add some.

Delegate mechanism is important for parents to protect the stable operation of Java programs, but it's also very simple to implement ideas, focused on the implementation code ClassLoader class loaderClass (method). Logic: first check has been loaded before, if not loaded by calling the parent loader loaderClass then () method, if the parent loader is empty, the default boot class loader as the parent loader; if the parent class loader fails to start , throw a ClassNotFoundException, then call their findClass () method to load.

Destruction mechanism into the parent thread context delegate class loader, the load set by setContextClassLoaser java.lang.Thread class () method is not set if the thread is created it inherits from a parent thread; if a range referenced globally inside is not set, the default of this class is loaded application loader. Delegate parents about the damage mechanisms in place which, and why damage can find previous articles.


If there are omissions article, I will give in the comments area, but if readers find inappropriate This article also pointed out that the comments area please, joint research.

Special Note: This article excerpt text copyright belongs to the original author! ! !


Guess you like

Origin juejin.im/post/5dd87cbe5188257343661dc2
JVM
JVM