Java 中的对象引用有多大,它包含哪些信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/981073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:50:16  来源:igfitidea点击:

How big is an object reference in Java and precisely what information does it contain?

java

提问by

As a programmer I think of these as looking like "the java.lang.Object at address 1a234552" or similar for something like the sin

作为一个程序员,我认为这些是“在地址1a234552的java.lang.Object继承”或类似看起来像的东西,如s

Object s = "hello";

Is this correct? Therefore are all references a fixed size?

这样对吗?因此,所有引用都是固定大小吗?

回答by Georgy Bolyuba

It is not a part of JLS or JVM Spec, but in practice it will be an address: 32 bit on 32 bit CPU, 64 at 64.

它不是 JLS 或 JVM 规范的一部分,但实际上它将是一个地址:32 位 CPU 上的 32 位,64 位上的 64。

pqism: Okay got you, because after compilation we no longer care about the declared type?

pqism:好的,明白了,因为编译后我们不再关心声明的类型?

We do care. That is why Class objects are there. In fact, from the other answers you can see that we care about types in runtime enough to optimize the way we work with them by putting part of type information into reference.

我们很在意。这就是 Class 对象存在的原因。事实上,从其他答案中你可以看到我们在运行时足够关心类型,通过将部分类型信息引入引用来优化我们使用它们的方式。

回答by Jon Skeet

While on many VMs the size of a reference is the native pointer size (i.e. 32 bits for a 32 bit JVM and 64 bits for a 64 bit JVM) this isn't guaranteed - and in particular HotSpot either does now or soon willsupport "Compressed Oops"which are 32 bit references in a 64 bit JVM. (That doesn't mean that everyreference is compressed - read the linked article for more information, and there are plenty of blog posts about it too.)

虽然在许多 VM 上,引用的大小是本机指针大小(即 32 位 JVM 为 32 位,64 位 JVM 为 64 位),但这并不能保证 - 特别是 HotSpot 现在或很快支持“ Compressed Oops”,它是 64 位 JVM 中的 32 位引用。(这并不意味着每个参考资料都被压缩了 - 阅读链接的文章了解更多信息,还有很多关于它的博客文章。)

In response to another comment, note that the reference itself is typically just a way of addressing the object itself. Whether it's a direct memory pointer or not, its goal is to get to the data for the object. That's basically all that reallymatters. If there's some "spare" bits (e.g. it's a 64-bit reference and you don't need all of that width just to represent the object's location) then the VM can use that data for other information such as its type, which may allow some optimisations. (See Tom's comment for more details.)

作为对另一条评论的回应,请注意引用本身通常只是寻址对象本身的一种方式。不管是不是直接内存指针,它的目标都是获取对象的数据。这基本上就是真正重要的一切。如果有一些“备用”位(例如,它是一个 64 位引用并且您不需要所有宽度来表示对象的位置),那么 VM 可以将该数据用于其他信息,例如其类型,这可能允许一些优化。(有关更多详细信息,请参阅汤姆的评论。)

The object itself contains type information (probably in the form of a reference to the instance of Class, or something similar - I don't know in enough detail) as well as other necessary "stuff" in the header, before you get to the user data for the object.

Class在您到达用户之前,对象本身包含类型信息(可能以对 的实例的引用的形式,或类似的东西 - 我不知道足够详细)以及标题中的其他必要“内容”对象的数据。

回答by Rutger Nijlunsing

Most people tend to see a reference to an object as a C-language-like-memory-pointer. While this is not technically correct, most implementations do implement it as a pointer. In case of compressed object pointers for example, the JVM stores only bits 3 to 34 of the 64-bit pointer on a 64 bit platform. Other implementations could also choose to use a different scheme: the reference could be an index into an pointer array containing all objects.

大多数人倾向于将对象的引用视为类似 C 语言的内存指针。虽然这在技术上不正确,但大多数实现确实将其实现为指针。例如,在压缩对象指针的情况下,JVM 在 64 位平台上仅存储 64 位指针的第 3 到 34 位。其他实现也可以选择使用不同的方案:引用可以是包含所有对象的指针数组的索引。

回答by erickson

The size of an object reference depends on the JVM and machine architecture. Generally, on a 32-bit machine it is 32 bits and on a 64-bit machine it is 64 bits. However, I think that the OpenJDK 7 JVM will have support for "compressed pointers" that will save some room on 64-bit machines.

对象引用的大小取决于 JVM 和机器架构。通常,在 32 位机器上它是 32 位,而在 64 位机器上它是 64 位。但是,我认为 OpenJDK 7 JVM 将支持“压缩指针”,这将在 64 位机器上节省一些空间。

The information about the object's type is stored in the object itself; that is, if you follow the 32-bit or 64-bit pointer (or, more likely, handle) to the object, you would find another pointer to a Classinstance that describes the type, as well as the data fields of the object.

关于对象类型的信息存储在对象本身中;也就是说,如果您遵循指向对象的 32 位或 64 位指针(或更可能是句柄),您会发现另一个指向Class描述该类型的实例以及该对象的数据字段的指针。