Java 中的对象引用有多大,它包含哪些信息? [英] How big is an object reference in Java and precisely what information does it contain?

查看:29
本文介绍了Java 中的对象引用有多大,它包含哪些信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

Object s = "hello";

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

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

推荐答案

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

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.)

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

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.)

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

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.

这篇关于Java 中的对象引用有多大,它包含哪些信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆