preface

From graduation to now more than two years, because of the old club has always been a week and overtime to the size of the state, combined with the own procrastination, there has been no good to their mastery of the knowledge of a system arrangement, suddenly one day, my mind with the idea of writing the technical articles, thinking about from work before the don’t care but and can’t bypass the JVM start the interview, Hence the article.

1. Parent delegation mechanism

1. What is parental delegation

When an application loads a class, the classloader receives a request to load the class, it doesn’t load the class itself, it checks that it’s loaded, and if it’s not loaded, it delegates to its parent class to load the class, because classloaders at every level do that. Finally, the Bootstrap ClassLoader is sent to the top layer of the Bootstrap ClassLoader to start the ClassLoader. When the parent loader finds itself unable to load, it pushes down to the child loader for loading. This is the so-called parent delegate mechanism.

2. Why parents delegate

  • For safety reasons
  • The parent class is loaded, not reloaded

Class loaders

  • Bootstrap ClassLoader

This is the big Boss of the loader, which is responsible for any class’s loading behavior. Jar, resources. Jar, charsets. Jar, etc. Of course, the path of these JAR packages can be specified, and the -xbootCLASspath parameter can do the specified operation.

This loader is written in C++ as the JVM starts.

  • Extention ClassLoader

The extension class loader is used to load JAR packages and.class files in the lib/ext directory. Again, this directory can be specified by the system variable java.ext.dirs.

This loader is a Java class that inherits from URLClassLoader.

  • App ClassLoader

This is the default loader for Java classes we write, sometimes called System ClassLoader. This is used to load all other JAR packages and.class files in the classpath, and our code will try to use this class loader first.

  • Custom ClassLoader

Custom loader, support some personalized extension features.

4, on the source code

Check out the ClassLoader#loadClass method to see how it works. As described above, the class is first checked to see if it has been loaded. If it has not been loaded, it is handed to the parent loader for loading. If it has not been loaded, it is loaded itself.

Class loading mechanism

1. There are seven stages of class loading

The life cycle is divided into seven phases, from when the class is loaded into virtual machine memory to when it is unloaded. They are load, verify, prepare, parse, initialize, use, and uninstall

  • loading

    The main function of loading is to load external.class files into the Java method area. The loading phase is mainly to find and load the binary data of the class, such as from jar packages or war packages.

  • validation

    In the validation phase, the main purpose is to verify that the loaded bytecode conforms to the JVM specification, otherwise the JVM will not execute randomly. In the verification stage, four aspects of verification will be completed, respectively: file format verification, metadata verification, bytecode verification and symbol reference verification.

  • To prepare

    In the preparation phase, memory is allocated for class variables and initial values are set. The memory used by these variables is allocated in the method area.

    Memory allocation includes only class variables (static variables), and does not include instance variables. Instance variables will be allocated along with the object when it is instantiated. The initial value is usually a zero value of the data type

  • parsing

    In the parse phase, we convert symbolic references to direct references. We convert some variables, such as temp, into physical addresses. Otherwise, the JVM will not know what temp is

  • Initialize the

    This is the core stage, the initialization of the class. If it is found that the parent of the class is not initialized, it will first pause and then initialize the parent class. It is also a set of process of class loading until the parent class is loaded, and then initialize the subclass

Wechat public account: next door shuhua attention can learn more about Java related knowledge. Questions or suggestions, please leave a message on the public account; There is no road in vain, as long as you walk carefully, every step counts.