The whole process and memory diagram analysis of class loading in Java

Source: Internet
Author: User

class loading mechanism :

The JVM loads the class file into memory and verifies, parses, and initializes the data, eventually creating a Java type that the JVM can use directly.

(1) Loading

The class file bytecode content is loaded into memory, and the static data is converted into a run-time data structure in the method area, and a Java.lang.Class object representing the class is generated in the heap as the access entry for the method area class data.

(2) Link the process of merging the Java class's binary code into the running state of the JVM

2.1 Verification

Ensure that the loaded class information conforms to the JVM specification and there is no security issue.

2.2 Preparation

A phase that formally allocates memory for class variables (static variables) and sets the initial value of class variables, which are allocated in the method area.

2.3 Parsing

The symbolic reference in the virtual machine constant pool is replaced with a direct reference procedure. (such as String s = "AAA", the address that translates to S to "AAA")

(3) initialization

The initialization phase is the process of executing a class constructor method. The class constructor method is generated by the compiler's automatic collection of assignment actions for all class variables in the class and statements in static statement blocks (static blocks).

When initializing a class, if it is found that the parent class has not yet been initialized, the initialization of its parent class needs to be initialized first

Virtual opportunity to ensure that a class's constructor method is properly locked and synchronized in a multithreaded environment

When accessing a static domain of a Java class, only the class that actually declares the static variable is initialized.

The class loading process is divided into : Active references to classes and passive references to classes

The active reference to the class (the initialization of the class must occur)

--new an object of a class

--Invoke the static members of the class (except the final constant) and the static method

--a reflection call to a class using the Java.lang.reflect package method

--When initializing a class, if its parent class is not initialized, initialize his parent class first

--When you want to execute a program, you must first start the class where the Main method resides

Passive reference to class (no initialization of class will occur)

--When accessing a static variable, only the class of the static variable of the real life is initialized (referring to the static variable of the parent class through the subclass, without causing the subclass to initialize)

--defining class application through an array, does not trigger initialization of this class a[] A = new A[10];

--the reference constant (final type) does not trigger initialization of this class (constants are stored in the constant pool of the calling class during the compilation phase)

class loader hierarchy (tree-like structure)

Boot class loader (bootstrap class loader)

--he uses classes to load Java's core library (String, Integer, List ... The content under the Jre/lib/rt.jar path is implemented in C code and does not inherit from Java.lang.ClassLoader.

--load the extension class and the Application class loader. and specify their parent class loader.

Extension class loader (Extensions class loader)

--The Java Virtual Machine implementation will automatically provide an extended directory to load Java's extension libraries (content under the Jre/ext/*.jar path). The ClassLoader finds and loads the Java class in this directory.

Application class loader (Application class loader)

--he is based on the classpath of the Java application (classpath path), and generally, the Java application class is done by him to load the

Custom Class Loaders

--developers can implement their own classloader by inheriting the Java.lang.ClassLoader class to meet some special requirements.

The extension class loader, the application ClassLoader, and the custom ClassLoader are all implemented by Java and inherit the Java.lang.ClassLoader class.

Proxy mode for class loader: Parent delegation mechanism

--that is, when a particular class loader receives a request to load a class, it first delegates the load task to the parent ClassLoader, one at a time, until the highest grandfather's generation, if the parent class loader can complete the class load task, it returns successfully, as long as the parent class loader cannot complete the secondary load task.

--the parent mechanism is to guarantee the type safety of the Java Core Library, and there is no case that the user can define the Java.lang.Object class.

The parent delegation mechanism is one of the proxy patterns, not all classloader use the parent-delegate mechanism, the Tomcat server ClassLoader also uses the proxy mode, the difference is that he is first trying to load a class, if not found in the proxy to the parent class loader.

The whole process and memory diagram analysis of class loading in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.