Overview of Memory structure

Memory structure diagram:

Memory structure detail diagram:

English:

English: Here is a personal collation of some information, the need of friends can be directly click to receive.

Java Basics

22 Java Architect Core books

Learning routes and materials from 0 to 1Java

1000+ questions from 2021

Class loaders and the loading process

Class loader subsystem

Illustration:

Description:

  • The classloader subsystem is responsible for loading class files from the file system or network. Class files have specific file identifiers at the beginning of the file.
  • The ClassLoader is only responsible for loading the class file, and it is up to ExecutionEngine to decide whether it can run.
  • The loaded class information is stored in a piece of memory called the method area. In addition to class information, the method area also stores runtime constant pool information,
  • It may also include string literals and numeric constants (this constant information is a memory map of the constant pool part of the Class file)

Personal Understanding:

The function of the ClassLoader

Illustration:

Description:

  • A class file exists on the local hard disk, and can be interpreted as a template drawn on paper by the designer, which is eventually loaded into the JVM to instantiate n identical instances from this file.
  • The class file is loaded into the JVM, called the DNA metadata template, and placed in the method area.
  • The.class file ->_JVM -> eventually becomes the metadata template, and this process requires a transporter (the class Loader) to act as a Courier.

Personal Understanding:

Class loading process

Illustration:

Description:

Get the binary byte stream 2 that defines a class by its fully qualified name. Transform the static storage structure represented by this byte stream into the runtime data structure 3 of the method area. Generate an in-memory java.lang.class object representing this class as an access point to the various data of this class in the method area. The purpose is to ensure that the byte stream of the class file contains information that meets the requirements of the current VIRTUAL machine, ensure the correctness of the loaded class, and do not harm the security of the VIRTUAL machine. It mainly includes four types of verification: file format verification, metadata verification, bytecode verification, symbol reference verification. - Ready: Allocates memory for the class variable and sets the default initial value of the class variable, that is, zero. Static with final is not included here, because final is allocated at compile time and initialized explicitly during preparation; There is no instance variable allocation initialization, class variables are allocated in the method area, and instance variables are allocated in the Java heap along with the object. - Parsing: The process of converting symbolic references in the constant pool into direct references; In fact, parsing operations are often performed by the JVM after initialization; A symbolic reference is a set of symbols that describe the referenced object. The literal form of symbolic references is explicitly defined in the Class file format of the Java Virtualizer Specification. A direct reference is a pointer directly to the target, a relative offset, or a handle that is indirectly located to the target. Parsing actions are for classes or interfaces, fields, class methods, interface methods, method types, and so on. CONSTANT Class Info, CONSTANT Fieldref Info, and CONSTANT Methodref Info in the CONSTANT pool. // Initialize;Copy the code

Class loader classification

Summary:

The JVM supports two types of classloaders, bootstrapclassloaders and user-defined classloaders. Conceptually, a custom classLoader is a classLoader Defined by a developer in a program. The Java Virtual Machine specification does not define it this way, but classifies all classloaders derived from the abstract ClassLoader as custom classloaders.

Illustration:

Description:

Bootstrap classLoader (Bootstrap classLoader) : 1 This class loading is implemented in C /C++ and is nested within the JVM. 2. It is used to load Java core libraries (JAVA_HOME/jre/lib/rt.jar, resources.jar, or sun.boot.class.path) to provide classes required by the JVM itself; 3. Does not inherit from java.lang.ClassLoader, no parent loader. 4. Load extension classes and application classloaders and designate them as their parent classloaders. 5. For security reasons, Bootstrap only loads Extension classloaders whose package names start with Java, Javax, sun, etc. Launciler$ExtClassLoader ($ExtClassLoader, $ExtClassLoader). Derived from the classLoader class 3. The parent classLoader is the boot classLoader 4. Load the class libraries from the directory specified by the java.ext.dirs system property, or from the JRE /lib/ext subdirectory (extension directory) of the JDK installation directory. If user-created jars are placed in this directory, they will also be automatically loaded by the extended class loader. Application class loader (System class loader, AppClassLoader) : 1. Java language written by sun.misc.Launcher$AppclagsLoader 2. The implementation is derived from the classLoader class 3. The parent classLoader is the extension classLoader 4. It is responsible for loading library 5 under the path specified by the environment variable classpath or the system property java.class.path. Class loading is the default class loader in a program, which is generally used to load classes in Java applications 6. Class loaders can be obtained by using the classLoader#getSystemclassLoader () method: In everyday Java application development, the loading of classes is almost carried out by the above three types of loaders together. If necessary, we can also customize the class loaders to customize the way the class is loaded. The procedure for implementing user-defined class loaders is as follows: 1. Developers can implement their own classloaders by inheriting the abstract java.lang.classLoader class to meet some special needs 2. Before JDK1.2, it was common to inherit the classLoader class and rewrite the loadClass () method to implement custom classloading classes. However, after JDK1.2, it is no longer recommended to override the loadClass () method. Instead, it is recommended that you write your custom classloading logic in the findClass () method 3. When writing a custom class loader, if you don't have too complicated requirements, you can inherit the URLClassLoader class directly. This way, you can avoid writing the findClass () method and the way to get the bytecode stream, making the custom class loader writing simpler.Copy the code

This section describes how to use ClassLoader

Summary:

The ClassLoader class is an abstract class that all subsequent classloaders inherit from (excluding the launcher ClassLoader).

How to obtain ClassLoader:

  • Clazz.getclassloader ()
  • Thread.currentthread ().getContextClassLoader ()
  • Three: get this – > system. This getsystemClassLoader ()
  • Four: get the caller’s this – > DriverManager. GetCallerclassLoader ()

Parent delegation mechanism

Summary:

The Java virtual machine loads class files on demand, which means that it loads its class files into memory to generate class objects when the class is needed. In addition, when loading a class file, the Java VIRTUAL machine adopts the parental delegation mode, that is, the request to the parent class processing, which is a kind of task delegation mode.

Illustration:

Description:

How it works: 1) If a classloader receives a classload request, it does not load the request itself. Instead, it delegates the request to the parent class loader. 2) If the parent class loader also has its parent class loader, then further delegate up, recursively, the request will eventually reach the top level of the start class loader; 3) If the parent class loader can complete the task, it returns successfully. If the parent class loader cannot complete the task, the child loader will try to load itself. This is the parent delegate mode. Advantages: 1. Avoid class reloading 2. Protect program security and prevent core API from being tampered with

other

Determine if two class objects are the same class:

  • The full class name of the class must be consistent, including the package name.
  • The ClassLoader (ClassLoader instance object) that loads this class must be consistent.

A reference to a classloader:

The JVM must know whether a type is loaded by the boot loader or by the user class loader. If a type is loaded by a user class loader, the JVM stores a reference to that class loader in the method area as part of the type information. When resolving a reference from one type to another, the JVM needs to ensure that the classloaders for both types are the same.

The last

See here feel the article is helpful to you might as well give small make up a thumbs-up, thank you for your support!