`

JVM学习笔记-动态连接和解析(Dynamic Linking and Resolution)

 
阅读更多

 

When you compile a Java program, you get a separate class file for each class or interface in your program. Although the individual class files may appear to be independent, they actually harbor symbolic connections to one another and to the class files of the Java API. When you run your program, the Java Virtual Machine loads your programís classes and interfaces and hooks them together in a process of dynamic linking. As your program runs, the Java Virtual Machine builds an internal web of interconnected classes and interfaces.

当编译Java程序的时候,会得到程序中每一个类或者接口的独立的class文件。虽然独立看上去毫无关联,但是他们之间通过接口(harbor)符号互相联系,或者与Java API的class文件相联系。当运行程序的时候,Java虚拟机装载程序的类和接口,并且在动态连接的过程中把它们互相勾连起来。在程序运行中,Java虚拟机内部组织了一张互相连接的类和接口的网。

 

A class file keeps all its symbolic references in one place, the constant pool. Each class file has a constant pool, and each class or interface loaded by the Java Virtual Machine has an internal version of its constant pool. The internal constant pool is an implementation-specific data structure that maps to the constant pool in the class file. Thus, after a type is initially loaded, all the symbolic references from the type reside in the typeís internal constant pool.

class把他们所有的引用符号放在一个地方——常量池。每一个class文件有一个常量池,每一个被Java虚拟机装载的类或者接口都有一份内部版本常量池,被称作运行时常量池。运行时常量池是特定与实现的数据结构,数据结构映射到class文件中的常量池。因此,当一个类型被首次装载的时候,所有来自于类型的符号引用都装载到了类型的运行时常量池。 

 

At some point during the running of a program, if a particular symbolic reference is to be used, it must be resolved. Resolution is the process of finding the entity identified by the symbolic reference and replacing the symbolic reference with a direct reference. Because all symbolic references reside in the constant pool, this process is often called constant pool resolution.

在程序运行的过程中,如果某个特定的符号引用将要被使用,它首先要被解析。解析过程就是首先根据符号引用查找到实体,再把符号引用替换成直接引用的过程。因为所有的符号引用都是保存在常量池中,所以这种解析叫做常量池解析。

 

As described in Chapter 6, "The Java Class File," the constant pool is organized as a sequence of items. Each item has a unique index, much like an array element. A symbolic reference is one kind of item that may appear in the constant pool. Java Virtual Machine instructions that use a symbolic reference specify the index in the constant pool where the symbolic reference resides. For example, the getstatic opcode, which pushes the value of a static field onto the stack, is followed in the bytecode stream by an index into the constant pool. The constant pool entry at the specified index, a CONSTANT_Fieldref_info entry, reveals the fully qualified name of the class in which the field resides, and the name and type of the field.

 

在第6章中描述过,常量池按照一系列项组织。每一项拥有一个唯一的索引,很像数组元素。符号引用是可以出现在常量池的一种项目。使用符号引用的Java虚拟机指令指定位于常量池中的符号引用的索引。比如,getstatic操作码(它把一个静态字段的值压入栈中)在字节码流中会跟有一个常量池索引。常量池中指定索引指向的入口,是一个Constant_Fieldref_info入口,它显示出这个字段所在类的全限定名以及字段的名字和类型。

 

 

Keep in mind that the Java Virtual Machine contains a separate internal constant pool for each class and interface it loads. When an instruction refers to the fifth item in the constant pool, it is referring to the fifth item in the constant pool for the current class, the class that defined the method the Java Virtual Machine is currently executing.

 

记住,java虚拟机为每一个装载的类和接口保存一份独立的常量池。当一条指令引用常量池的第5个元素的时候,它指向的是当前类的常量池中的第5个元素,即定义Java虚拟机当前正执行的方法的类。


 

Several instructions, from the same or different methods, may refer to the same constant pool entry, but each constant pool entry is resolved only once. After a symbolic reference has been resolved for one instruction, subsequent attempts to resolve it by other instructions take advantage of the hard work already done, and use the same direct reference resulting from the original resolution.

Linking involves not only the replacement of symbolic references with direct ones, it also involves checking for correctness and permission. As mentioned in Chapter 7, "The Lifetime of a Class," the checking of symbolic references for existence and access permission (one aspect of the full verification phase) is likely performed during resolution. For example, when a Java Virtual Machine resolves a getstatic instruction to a field of another class, the Java Virtual Machine checks to make sure that:

  • The other class exists.
  • This class has permission to access the other class.
  • The named field exists in the other class.
  • The field has the expected type (symbolic references to fields include the field type).
  • This class has permission to access the field.
  • That the field is really static--a class variable and not an instance variable.

If any of these checks fail, an error is thrown and resolution fails. Otherwise, the symbolic reference is replaced by the direct reference and resolution succeeds.

 

As described in Chapter 7, "The Lifetime of a Class," different implementations of the Java Virtual Machine are permitted to perform resolution at different times during the execution of a program. An implementation may choose to link everything up front by following all symbolic references from the initial class, then all symbolic references from subsequent classes, until every symbolic reference has been resolved. In this case, the application would be completely linked before its main() method was ever invoked. This approach is called early resolution. Alternatively, an implementation may choose to wait until the very last minute to resolve each symbolic reference. In this case, the Java Virtual Machine would resolve a symbolic reference only when it is first used by the running program. This approach is called late resolution. Other implementations could choose a resolution strategy in-between these two extremes.

 

Although a Java Virtual Machine implementation has some freedom in choosing when to resolve symbolic references, every Java Virtual Machine must give the outward impression that it uses late resolution. No matter when a particular Java Virtual Machine performs its linking, it will always throw any error that results from attempting to resolve a symbolic reference at the point in the execution of the program where the symbolic reference was actually used for the first time. In this way, it will always appear to the user as if the linking were late. If a Java Virtual Machine does early linking, and during early linking discovers that a class file is missing, it wonít report the class file missing by throwing the appropriate error until later in the program when something in that class file is actually used. If the class is never used by the program, the error will never be thrown.

分享到:
评论

相关推荐

    nginx-upstream-jvm-route-1.15

    【标题】"nginx-upstream-jvm-route-1.15" 涉及的核心知识点是Nginx的upstream模块与JVM路由的整合,特别针对Nginx 1.15版本。这个项目旨在解决在配置Nginx时遇到的特定错误提示“nginx: [emerg] invalid parameter ...

    基于jvm-sandbox-repeater重新开发的一款流量回放平台产品

    在jvm-sandbox-repeater基础上提供了更加丰富功能,同时便于线上部署和使用,更多对比参考。 使用场景 你是否遇到过以下的问题? 线上有个用户请求一直不成功,我想在测试环境Debug一下,能帮我复现一下吗? 压测...

    metrics-jvm-3.1.5-API文档-中文版.zip

    赠送jar包:metrics-jvm-3.1.5.jar; 赠送原API文档:metrics-jvm-3.1.5-javadoc.jar; 赠送源代码:metrics-jvm-3.1.5-sources.jar; 赠送Maven依赖信息文件:metrics-jvm-3.1.5.pom; 包含翻译后的API文档:...

    JVM调优总结 -Xms -Xmx -Xmn -Xss

    其中,-Xms、-Xmx、-Xmn、-Xss 是四个重要的参数,分别控制 JVM 的初始堆大小、最大堆大小、年轻代大小和每个线程的堆栈大小。 堆大小设置是 JVM 调优的重要一步。JVM 的最大堆大小受到三个方面的限制:操作系统的...

    jvm 调优笔记-jvm.zip

    《JVM调优笔记》 Java虚拟机(JVM)是Java编程语言的核心组成部分,它负责执行字节码,管理内存,以及优化程序性能。JVM调优是提高Java应用程序性能的关键步骤,涉及到多个方面,包括堆内存设置、垃圾收集器选择、...

    JVM性学习笔记-基本原理,内存模型,JVM参数

    JVM性学习笔记-基本原理,内存模型,JVM参数设置,类加载器原理,JDK自带工具

    metrics-jvm-3.1.5-API文档-中英对照版.zip

    赠送jar包:metrics-jvm-3.1.5.jar; 赠送原API文档:metrics-jvm-3.1.5-javadoc.jar; 赠送源代码:metrics-jvm-3.1.5-sources.jar; 赠送Maven依赖信息文件:metrics-jvm-3.1.5.pom; 包含翻译后的API文档:...

    深入JVM---JVM命令---invokespecial

    《深入JVM---JVM命令---invokespecial》 在Java虚拟机(JVM)的世界里,`invokespecial`指令扮演着至关重要的角色。它主要用于执行对象的构造方法(即`<init>`方法),以及调用私有方法和父类非静态方法。本文将...

    JVM学习笔记(一)

    本篇学习笔记主要介绍如何利用一系列工具来查看和监控JVM的各种运行时信息,包括但不限于JVM进程与参数查看、垃圾收集信息、JVM锁信息等内容。 #### 二、查看JVM进程及参数 ##### 1. JPS - **用途**:列出主机上...

    nginx-upstream-jvm-route-0.1.tar.gz

    "nginx-upstream-jvm-route-0.1.tar.gz"这个压缩包包含了实现这一功能的相关配置和脚本。在解压后的"nginx_upstream_jvm_route"目录中,我们可以找到Nginx的配置示例、Java应用程序的部署配置以及可能的脚本文件。...

    Android Studio 报错failed to create jvm error code -4的解决方法

    代码如下:failed to create jvm error code -4 这一般应是内存不够用所致,解决方法参考如下。 打开 Android Studio 安装目录下的bin目录,查找并打开文件 studio.exe.vmoptions,修改代码: 代码如下:-Xmx512m 为...

    mini-jvm in rust实现jvm,jvm-rs-main.zip

    项目名为"jvm-rs-main",通过这个项目,我们可以深入学习Rust语言与JVM的交互,以及如何构建一个简化版的JVM。 一、Rust语言的魅力 Rust是一种系统级编程语言,它强调安全、并发和速度。它的内存管理模型避免了空...

    JVM学习笔记2018-4-151

    本篇JVM学习笔记主要关注对象声明、相关内存分配方法以及虚拟内存的物理和虚拟寻址概念。 首先,我们来看对象声明。在Java中,对象是在堆上创建的。例如,`CHeapObj` 类展示了如何在C++中模拟Java对象在堆上的分配...

    nginx-upstream-jvm-route-1.12.0.tar.gz

    nginx_upstream_jvm_route 是一个 Nginx 的扩展模块,用来实现基于 Cookie 的 Session Sticky 的功能。 安装方法(进入Nginx源码目录): #patch -p0 < /path/to/this/directory/jvm_route.patch # ./configure -...

    JVM-MANAGEMENT-MIB,jvm mib库

    JVM,MIB,可通过SNMP协议监控JVM运行情况

    nginx-upstream-jvm-route 和 nginx 对应版本,亲测可用

    此资源有两个文件,含 nginx-upstream-jvm-route 和 nginx 对应版本,都是tar.gz文件。 安装方法网上很多就不写了,亲测可用。 不用担心版本不匹配造成安装失败,再浪费积分去到处下载尝试的烦恼。 此资源有两个文件...

    jvm调优学习-optimizeJVM.zip

    在Java开发领域,JVM(Java Virtual Machine)是运行Java应用程序的关键组件,它负责解析字节码并执行程序。JVM调优是一项重要的技能,它能够帮助提升应用性能、减少内存消耗以及改善整体系统稳定性。"jvm调优学习-...

    java之jvm学习笔记十一(访问控制器)-源码

    本文将深入探讨JVM中的访问控制器,主要基于“java之jvm学习笔记十一(访问控制器)-源码”这一主题,以及相关的源码分析。 首先,我们得了解Java的安全模型。Java安全模型基于一种称为安全管理器(SecurityManager)...

    Jvm调优练习-jvm-tuning.zip

    【标题】"Jvm调优练习-jvm-tuning.zip" 提供了一个实践 JVM(Java Virtual Machine)调优的机会,这是一项至关重要的技能,特别是在处理大型、高性能的Java应用程序时。JVM调优涉及到调整一系列参数,以优化应用程序...

    JVM学习笔记

    ### JVM学习笔记 #### JVM内存模型 (JMM) JVM内存模型主要分为以下几个部分: - **Java堆**:这是所有线程共享的一块区域,在虚拟机启动时创建。主要用于存放对象实例,几乎所有的对象实例都在这里分配内存。 - *...

Global site tag (gtag.js) - Google Analytics