JAVA中类的引用大小

发布于 2024-08-11 03:44:16 字数 275 浏览 4 评论 0原文

Java中类的引用大小是多少?对于特定的 JVM 和操作系统,它是否恒定,与引用的类无关。


Class A
Class B
Class C

A a;
B b;
C c;

无论AB 的大小如何,abc 的大小是否相同C 类?

What is the size of reference of a class in Java? Is it constant for a particular JVM and OS irrespective of the class for which the reference is made.


Class A
Class B
Class C

A a;
B b;
C c;

Are the size of a, b and c the same irrespective of the size of A, B and C classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫七 2024-08-18 03:44:16

是的,所有引用都是指向对象的“指针”或间接指向对象的“指针的指针”。它们的实际内存大小是 JVM 的实现细节,但您可以确保不同类的大小相同。

Yes, all references are "pointers" to Objects or indirect "pointers to pointers" to Objects. Their actual in memory size is an implementation detail of the JVM but you can be sure that they are equal in size for different classes.

云巢 2024-08-18 03:44:16

我不认为这是明确指定的;它可能由 JVM 的实现来决定(就像 System.identityHashcode() 一样)。 JLS 的相关部分 当然没有好像没有提到具体的尺寸。

几乎可以肯定,引用实际上是指向内存地址的指针,但您不应该依赖它的任何特定行为。特别是,您根本不应该尝试像在 C 中那样操作它们。

我很好奇你问这个的目的是什么 - 仅仅是好奇还是你打算以某种方式利用参考尺寸(这几乎肯定是一个坏主意)?如果是后者,请告诉我们您打算做什么,因为可能有更好的方法来实现它。

I don't believe it's explicitly specified; it is likely left to the implementation of the JVM to decide (just like System.identityHashcode()). The relevant section of the JLS certainly doesn't seem to mention any specific size.

It is almost certainly the case that the refernce will actually be a pointer to a memory address, but you shouldn't rely on any specific behaviour of it. In particular, you shouldn't try to manipulate them at all like you would in C for example.

I'm curious as to what you are asking about this for - is it mere curiosity or you plan to make use of the reference size somehow (which would almost certainly be a bad idea)? If it's the latter, then let us know what you're planning to do as there's likely to be a better way to achieve it.

墨小墨 2024-08-18 03:44:16

引用基本上是一个内存地址(是的,我知道它比 Java 中的地址要多一点),它的大小与它指向的对象的大小无关。唯一的区别是,如果您使用 64 位 JVM,则引用需要更大才能容纳更大的内存地址。

The reference is basically a memory address (yes I know it is a little more than that in Java) and its size is unrelated to the size of the object it points at. The only variance is if you are using a 64 bit JVM, the references will need to be larger to accommodate the larger memory addresses.

忘东忘西忘不掉你 2024-08-18 03:44:16

就字节码而言,引用的行为与 intfloat 非常相似。 longdouble 占用两个堆栈槽。所以就好像引用是四个字节。然而,64 位系统经常对此进行修改,以便可以使用 64 位指针。

一些 JVM(我相信 BEA JRockit 一段时间了,最​​近添加到了 Sun 中)使用“压缩的 oops”,它是 32 位的,左移几个位置以允许在 64 位系统上访问数十 GB 的内存。除了减少内存消耗之外,减少引用大小还可以减少 CPU 内存带宽和缓存需求,从而在需要额外调整的情况下提高性能。

我相信 Azul 版本的 Hotspot 使用 64 位引用,其中 48 位用于地址,16 位类型信息。

As far as bytecode is concerned, a reference behaves much the same as an int or float. long and double take up two stack slots. So it's as if references are four bytes. However, 64-bit systems often munge this so that they can use 64-bit pointers.

Some JVMs (I believe BEA JRockit for some time and recently added to Sun's) use "compressed oops" which is 32-bits which gets shifted left a few places to enable access tens of GB of memory on 64-bit systems. As well as reducing memory consumption, reducing the reference size also reduces the CPU-memory bandwidth and cache requirements, improving performance despite the extra fiddling.

I believe Azul's version of Hotspot uses 64-bit references, with 48-bits for address and 16-bit type information.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文