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

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

问题描述

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

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对于32位JVM的位和64位JVM的64位,这是不可保证的 - 特别是HotSpot现在或很快支持压缩的糟糕,它们是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 <的实例的引用的形式在获得对象的用户数据之前,/ code>,或者类似的东西 - 我不太了解它们以及标题中的其他必要的东西。

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天全站免登陆