This article has been transferred from the Internet

This series of articles will be organized into my GitHub repository for the Java Interview Guide. Check out my repository for more highlights

https://github.com/h2pl/Java-Tutorial

If you like, please click Star

Articles will be synced to my personal blog:

www.how2playlife.com

WeChat public is this article number “Java technology river’s lake” of the deep understanding of the JVM virtual machine, one of the article, this part from the network, in order to put this article speaks with clarity theme, also integrates a lot I think good technology content, reference some good blog posts, if there are any copyright infringement, please contact the author.

This series of posts will tell you how from introductory to advanced, step by step to learn basic knowledge of the JVM, and fit for the JVM tuning of actual combat, the JVM is each Java engineer must study and understand the knowledge, you must grasp the implementation principles, to more fully understand the Java technology system, form their own knowledge framework.

In order to better summarize and test your learning results, this series of articles will also provide interview questions and reference answers for each point.

If you have any suggestions or questions about this series of articles, you can also contact the author on the public account “Java Technology Jianghu”. You are welcome to participate in the creation and revision of this series of blog posts.

I. Objectives:

1. What is class loading?

2. Class lifecycle?

3. What is a classloader?

4. What is the parent delegation mechanism?

Ii. Principle (class loading process and its final product) :

The JVM loads the class file bytecode files into memory, converts this static data into runtime data structures in the method area, and generates a Java.lang. class object representing this class in the heap (not necessarily in the heap, HotSpot is in the method area) as an access point to the method area class data.

Iii. Process (class life cycle) :

The JVM class loading mechanism is divided into five parts: loading, validation, preparation, parsing, and initialization. Let’s take a look at each of these five processes. The order of load, verify, prepare, initialize and unload is fixed, while parsing is not. To support dynamic binding, the parsing process can occur after the initialization phase.

Loading:

The loading process does three things:

  1. Gets the binary byte stream that defines the class by its fully qualified name
  2. Convert the static storage structure represented by this byte stream into the runtime data structure of the method area
  3. A java.lang.Class object representing this Class is generated in the heap as an entry point to these data structures in the method area.

This process is mostly done by the class loader.

Check:

This phase ensures that the byte stream in the Class file meets the requirements of the current VM and does not compromise VM security.

  1. File format validation: based on byte stream validation.
  2. Metadata validation: Validation based on the storage structure of the method area.
  3. Bytecode validation: Validation based on the storage structure of the method area.
  4. Symbol reference validation: Storage structure validation based on the method area.

Preparation:

Allocates memory for class variables and initializes them to default values. (this is the default value, which is assigned at initialization.) Allocates the memory space used by these variables in the method area. Such as:

public static int value = 123;
Copy the code

Now the initial value after the preparation phase is 0 instead of 123; The putStatic instruction that assigns value to 123 is stored in the class constructor method after the program is compiled. Special case:

public static final int value = 123;
Copy the code

The value of value after the preparation phase is 123.

Resolution:

Converts symbolic references in a type to direct references.

  • Symbolic references are independent of the layout implemented by the virtual machine, and the target of the reference does not have to have been loaded into memory. The memory layout of various virtual machine implementations can vary, but the symbolic references they accept must be consistent, because the literal form of symbolic references is explicitly defined in the Class file format of the Java Virtual Machine specification.
  • A direct reference can be a pointer to a target, a relative offset, or a handle that can be indirectly located to the target. If there is a direct reference, the target of the reference must already exist in memory

There are four main types:

  1. Class or interface resolution
  2. Field analytical
  3. Class method resolution
  4. Interface method parsing

Initialization:

The initialization phase is the process of executing the class constructor methods. Methods are a combination of assignment operations that the compiler automatically collects for class variables in a class and statements in a static statement block. The virtual machine guarantees that the methods of the parent class are executed before the method is executed. If there is no static variable assignment and no static statement block in a class, the compiler may not generate a () method for that class.

In Java, the initialization phase requires classes to be “initialized” immediately (loading, validating, and preparing, naturally, need to start before that) if and only if:

  1. Instantiating an object with the new keyword, accessing or setting a static field of a class (unless it is modified by final or has been put into the constant pool when the compiler optimizes), or calling a class method initializes that static field or the class in which the static method resides.
  2. When initializing a class, if its parent has not already been initialized, initialization of its parent must be triggered first.
  3. When a reflection call is made using the java.lang.Reflect package’s methods, if the class is not already initialized, it is initialized first.
  4. When the virtual machine starts, the user initializes the main class (containing main) to execute.
  5. JDK 1.7, if Java. Lang. Invoke the MethodHandle instances of the corresponding analytical result is REFGetStatic, REFPutStatic, REF_invokeStatic method handles, and if the class of this method is not initialized, it is initialized first.

Class loader:

Pass the class-loading phase of fetching the binary stream describing a class by its fully qualified name to a class-loader outside the virtual machine. The advantage of this is that we can implement our own class loader to load classes in other formats, as long as they are binary byte streams, which greatly increases the flexibility of the loader. Class loaders of the system are divided into three types:

  1. Start the class loader.
  2. Extend the class loader.
  3. Application class loader.

V. Parent delegation mechanism:

How the parent delegation mechanism works:

If a class loader receives a request from the class loader. It doesn’t try to load the class itself in the first place. Instead, delegate the request to the parent loader. This is true at every level of class loaders. Therefore, all load requests will eventually be passed to the Bootstrap class loader (the boot class loader). Only when the parent class loader reports that it cannot load the request (it did not find the desired class in its search scope). The child loader will try to load it itself.

Advantages of the parent delegate model: Java classes have a hierarchy of priorities along with their loaders.

For example, the java.lang.Object class is stored in rt.jart. This class must be loaded by any class loader. The parent delegates the Bootstrap class loader at the top of the model. Thus, the Object class is the same class in the various classloader environments of the program. Conversely, if you don’t use the parent delegate model. Each class loader is loaded by itself. If the user writes a class called “java.lang.Object”. Put it in the ClassPath of the application. There would be many different Object classes in the system, and the most basic behavior of the Java type system would not be guaranteed. Applications will also be chaotic.

Refer to the article

https://blog.csdn.net/android_hl/article/details/53228348