Overview of memory structure

Memory structure diagram:

Detailed diagram of memory structure:

Chinese:

English:

personally organize some information here, friends in need can click to receive it directly.

Basic knowledge of Java

22 core Java architect books

Java learning routes and materials from 0 to 1

1000+ channels 2021 latest

Class loader and loading process

The role of the class loader subsystem

Illustration:

description:

  • The class loader subsystem is responsible for loading class files from the file system or the network. The class files have a specific file identifier at the beginning of the file.
  • ClassLoader is only responsible for the loading of class files. As for whether it can run, it is determined by ExecutionEngine.
  • The loaded class information is stored in a memory space called the method area. In addition to class information, runtime constant pool information is also stored in the method area.
  • May also include string literals and numeric constants (this part of the constant information is the memory map of the constant pool part of the Class file)

Personal understanding:

The role of ClassLoader

Illustration:

description:

  • The class file exists on the local hard disk, which can be understood as a template drawn on paper by the designer, and in the end this template must be executed
    Load it into the JVM to instantiate n identical instances based on this file.
  • The class file is loaded into the JVM, which is called a DNA metadata template, and is placed in the method area.
  • In the .class file ->_JVM -> eventually becomes a metadata template, this process requires a transportation tool (class Loader),
    Play the role of a courier.

Personal understanding:

Class loading process

Illustration:

description:

//加载:
1.通过一个类的全限定名获取定义此类的二进制字节流
2.将这个字节流所代表的静态存储结构转化为方法区的运行时数据结构
3.在内存中生成一个代表这个类的java.lang.class对象,作为方法区这个类的各种数据的访问入口
//链接:
- 验证:目的在于确保class文件的字节流中包含信息符合当前虚拟机要求,保证被加载类的正确性,
       不会危害虚拟机自身安全,主要包括四种验证,文件格式验证,元数据验证,字节码验证,
       符号引用验证。
- 准备:为类变量分配内存并且设置该类变量的默认初始值,即零值;这里不包含用final修饰的static,
       因为final在编译的时候就会分配了,准备阶段会显式初始化;这里不会为实例变量分配初始化,
       类变量会分配在方法区中,而实例变量是会随着对象一起分配到Java堆中。
- 解析:将常量池内的符号引用转换为直接引用的过程;事实上,解析操作往往会伴随着JVM在执行完初始化之
        后再执行;符号引用就是一组符号来描述所引用的目标。符号引用的字面量形式明确定义在《java虚
        拟机规范》的class文件格式中。直接引用就是直接指向目标的指针、相对偏移量或一个间接定位到目
        标的句柄;解析动作主要针对类或接口、字段、类方法、接口方法、方法类型等。对应常量池中的
        CONSTANT Class info、CONSTANT Fieldref info、cONSTANT Methodref info等。
//初始化;

Class loader classification

Overview:

JVM supports two types of class loaders, namely BootstrapClassLoader and User-Defined classLoader. Conceptually, custom class loader generally refers to the development of the program A class loader customized by personnel, but the Java virtual machine specification does not define it as such. Instead, all class loaders derived from the abstract class ClassLoader are classified as custom class loaders.

Illustration:

description:

//虚拟机自带的类加载器:
启动类加载器(引导类加载器,Bootstrap classLoader):
1.这个类加载使用c/C++语言实现的,嵌套在JVM内部。
2.它用来加载Java的核心库(JAVA_HOME/jre/lib/rt.jar、resources.jar或sun.boot.class.path
  路径下的内容),用于提供JVM自身需要的类;
3.并不继承自java.lang.ClassLoader,没有父加载器。
4.加载扩展类和应用程序类加载器,并指定为他们的父类加载器。
5.出于安全考虑,Bootstrap启动类加载器只加载包名为java、 javax、sun等开头的类

扩展类加载器(Extension ClassLoader):
1.扩展类加载器(Extension ClassLoader)
2.Java语言编写,由sun.misc.Launciler$ExtClassLoader实现。派生于classLoader类
3.父类加载器为启动类加载器
4.从java.ext.dirs系统属性所指定的目录中加载类库,或从JDK的安装目录的jre/lib/ext子目录(扩展
  目录)下加载类库。如果用户创建的JAR放在此目录下,也会自动由扩展类加载器加载。

应用程序类加载器(系统类加载器,AppClassLoader):
1.java语言编写,由sun.misc.Launcher$AppclagsLoader
2.实现派生于classLoader类
3.父类加载器为扩展类加载器
4.它负责加载环境变量classpath或系统属性java.class.path指定路径下的类库
5.该类加载是程序中默认的类加载器,一般来说,Java应用的类都是由它来完成加载
6.通过classLoader#getSystemclassLoader ()方法可以获取到该类加载器

//用户自定义类加载器:
在Java的日常应用程序开发中,类的加载几乎是由上述3种类加载器相互配合执行的,在必要时,我们还可以
自定义类加载器,来定制类的加载方式。
用户自定义类加载器实现步骤:
用户自定义类加载器实现步骤:
1.开发人员可以通过继承抽象类java.lang.classLoader类的方式,实现自己的类加载器,以满足一些
  特殊的需求
2.在JDK1.2之前,在自定义类加载器时,总会去继承classLoader类并重写loadclass ()方法,从而实现
  自定义的类加载类,但是在JDK1.2之后己不再建议用户去覆盖loadclass()方法,而是建议把自定义的类
  加载逻辑写在findclass ()方法中
3.在编写自定义类加载器时,如果没有太过于复杂的需求,可以直接继承URLClassLoader类,这样就可以
  避免自己去编写findclass ()方法及其获取字节码流的方式,使自定义类加载器编写更加简洁。

Instructions for use of ClassLoader

Overview:

ClassLoader class, it is an abstract class, all subsequent class loaders inherit from ClassLoader (not including the startup class loader)
Illustration:


Ways to obtain ClassLoader:

  • Method 1: Get the ClassLoader of the current class -> clazz.getClassLoader ()
  • Method 2: Get the ClassLoader of the current thread context -> Thread.currentThread ().getContextclassLoader ()
  • Method 3: Get the system's ClassLoader -> ClassLoader.getsystemClassLoader()
  • Method 4: Get the caller's ClassLoader -> DriverManager.getCallerclassLoader ()

Parental delegation mechanism

Overview:

The Java virtual machine uses an on-demand loading method for class files, which means that when the class needs to be used, its class file will be loaded into the memory to generate a class object. And when loading the class file of a certain class, the Java virtual machine adopts the parental delegation mode, that is, the request is handed over to the parent class for processing, which is a task delegation mode.

Illustration:

description:

working principle:
1) If a class loader receives a class loading request, it does not load it first, but delegates the request to the loader of the parent class to execute;
2) If the parent class loader still has its parent class loader, it will further delegate upwards, recursively, and the request will eventually reach the top-level startup class loader;
3) If the parent class loader can complete the class loading task, it returns successfully. If the parent class loader cannot complete the loading task, the child loader will try
Load it yourself, this is the parental delegation model.
Advantage:
1. Avoid repeated loading of classes
2. Protect the program security and prevent the core API from being tampered with at will

√自定义类: java.lang.String


other

Determine whether two class objects are the same class:

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

Reference to the class loader:

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 the user class loader, the JVM will save a reference to this class loader in the method area as part of the type information. When resolving references from one type to another, the JVM needs to ensure that the class loaders of these two types are the same.

At last

If you see this article and feel that the article is helpful to you, you may wish to give the editor a like, thank you for your support!


前程有光
936 声望618 粉丝