博客
关于我
JVM类加载器详解
阅读量:765 次
发布时间:2019-03-23

本文共 2985 字,大约阅读时间需要 9 分钟。

Java Class Loading Mechanism Explained

The Java class loading mechanism is a cornerstone of the Java Virtual Machine (JVM) that ensures applications run correctly by dynamically loading classes into memory. This process is managed by the ClassLoader class, which acts as a bridge between the external class files and the JVM's runtime environment.

The Class Loader Phases

The class loader operates through three main phases, each ensuring that classes are loaded, linked, and initialized correctly.

  • Loading Phase (Verification and Preparing)

    • Verification: The class loader reads the byte code from the file and checks it against the JVM specification to ensure it conforms to the expected structure. This prevents the loading of invalid or malformed class files.
    • Preparation: The class loader initializes the static variables and assigns default values to instance variables. This is done by the Class inappropriate class.
  • Linking Phase (Validation and Resolution)

    • Validation: The class loader cross-verifies the dynamic variables and ensures static fields are correctly resolved.
    • Resolution: The class loader converts symbolic references in the class file into direct references within the JVM. This is necessary for the JVM to efficiently access the classes and their members.
  • Initialization Phase

    • The class loader initializes the class by invoking its constructor. This process is managed by the JVM's clint method, which generates the initialization code dynamically based on the class structure.
  • Class Loader Types

    In the JVM, class loading is delegated to different class loaders. These include:

  • Bootstrap Class Loader (BootstrapClassLoader)

    • Written in C and responsible for loading core Java libraries (e.g., java.lang, javax, sun packages).
    • Operates in a isolated sandbox environment.
  • Extension Class Loader (ExtClassLoader)

    • Loads classes from the JAVA_HOME/jre/lib/ext directory. It allows third-party vendors to add their own classes by placing JAR files in this directory.
  • Application Class Loader (AppClassLoader)

    • Responsible for loading application classes from the classpath. It delegates loading requests to the Extension Class Loader unless a specific class is already loaded.
  • Double-Checked Locking Mechanism

    The class loader ensures that each class is loaded only once through the double-checked locking mechanism. This involves:

  • Checking if the class is already loaded.
  • If not, initiating the loading process.
  • Once the class is loaded, subsequent checks return the loaded class without re-loading.
  • This mechanism also supports the JVM's sandbox environment, preventing external classes from overriding core Java classes.

    Understanding the class loading mechanism is crucial for Java developers as it affects Everything from development to debugging, especially when dealing with classpath issues or performance optimizations.

    By mastery of the class loader architecture, you gain insight into how the JVM manages and enhances the dynamic nature of Java applications.

    转载地址:http://itozk.baihongyu.com/

    你可能感兴趣的文章
    NAT PAT故障排除实战指南:从原理到技巧的深度探索
    查看>>
    nat 网卡间数据包转发_你是不是从来没有了解过光纤网卡,它跟普通网卡有什么区别?...
    查看>>
    NAT-DDNS内网穿透技术,快解析DDNS的优势
    查看>>
    NAT-DDNS内网穿透技术,快解析DDNS的优势
    查看>>
    NAT-DDNS内网穿透技术,解决动态域名解析难题
    查看>>
    natapp搭建外网服务器
    查看>>
    NativePHP:使用PHP构建跨平台桌面应用的新框架
    查看>>
    nativescript(angular2)——ListView组件
    查看>>
    NativeWindow_01
    查看>>
    Native方式运行Fabric(非Docker方式)
    查看>>
    Nature | 电子学“超构器件”, 从零基础到精通,收藏这篇就够了!
    查看>>
    Nature和Science同时报道,新疆出土四千年前遗骸完成DNA测序,证实并非移民而是土著...
    查看>>
    Nature封面:只低一毫米,时间也会变慢!叶军团队首次在毫米尺度验证广义相对论...
    查看>>
    Nat、端口映射、内网穿透有什么区别?
    查看>>
    Nat、端口映射、内网穿透有什么区别?
    查看>>
    nat打洞原理和实现
    查看>>
    NAT技术
    查看>>
    NAT模式/路由模式/全路由模式 (转)
    查看>>
    NAT模式下虚拟机centOs和主机ping不通解决方法
    查看>>
    NAT的两种模式SNAT和DNAT,到底有啥区别?
    查看>>