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

时间:2021-07-16 16:20:05

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

作为程序员,我认为这些看起来像“地址1a234552处的java.lang.Object”或类似于s in

Object s = "hello";

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

它是否正确?因此所有引用都是固定大小的?

4 个解决方案

#1


55  

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 will support "Compressed Oops" which are 32 bit references in a 64 bit JVM. (That doesn't mean that every reference 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现在或者很快就会支持“压缩的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 really matters. 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可以将该数据用于其他信息,例如其类型,这可能允许一些优化。 (有关详细信息,请参阅Tom的评论。)

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实例的引用形式,或类似的东西 - 我不太了解)以及标题中的其他必要的“东西”,然后再进入对象的用户数据。

#2


9  

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 Spec的一部分,但实际上它将是一个地址: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对象的原因。实际上,从其他答案中您可以看到,我们关注运行时类型,足以通过将部分类型信息引入参考来优化我们使用它们的方式。

#3


3  

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 Class instance that describes the type, as well as the data fields of the object.

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

#4


2  

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位。其他实现也可以选择使用不同的方案:引用可以是包含所有对象的指针数组的索引。

#1


55  

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 will support "Compressed Oops" which are 32 bit references in a 64 bit JVM. (That doesn't mean that every reference 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现在或者很快就会支持“压缩的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 really matters. 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可以将该数据用于其他信息,例如其类型,这可能允许一些优化。 (有关详细信息,请参阅Tom的评论。)

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实例的引用形式,或类似的东西 - 我不太了解)以及标题中的其他必要的“东西”,然后再进入对象的用户数据。

#2


9  

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 Spec的一部分,但实际上它将是一个地址: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对象的原因。实际上,从其他答案中您可以看到,我们关注运行时类型,足以通过将部分类型信息引入参考来优化我们使用它们的方式。

#3


3  

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 Class instance that describes the type, as well as the data fields of the object.

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

#4


2  

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位。其他实现也可以选择使用不同的方案:引用可以是包含所有对象的指针数组的索引。