Java中盒装基元的存储成本是多少? [英] What is the storage cost for a boxed primitive in Java?

查看:132
本文介绍了Java中盒装基元的存储成本是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大小(以字节为单位)是一个盒装基元,如 java.lang.Integer java.lang.Character 在Java中?

How large, in bytes, is a boxed primitive like java.lang.Integer or java.lang.Character in Java?

int 是4个字节,典型的指针也是4个字节(如果没有被压缩的话) JVM)。因此,整数(没有缓存)的成本是 4字节+4字节= 8字节?盒子对象中是否还有其他隐藏字段或者对象产生的额外开销(即,我不知道对象的一般成本是什么?)。

An int is 4 bytes, a typical pointer is also 4 byte (if not compressed by the JVM). Is the cost for an Integer (without caching) thus 4 bytes + 4 bytes = 8 bytes? Are there any more hidden fields within the box-object or additional overhead incurred regarding objects (i.e. is there a general cost for objects that I'm not aware of?).

我对缓存问题不感兴趣。我知道JVM会缓存一定范围内的整数。

I'm not interested in caching issues. I know that Integers within a certain range are cached by the JVM.

可以重新解释一个问题:在用于的内存量上乘以的最大因子是多少?盒装值与原始值?

One could rephrase the question: What is the maximum factor to be multiplied on the amount of memory used for boxed values versus primitive values?

编辑:我确实理解存在多个JVM实现。典型的32位HotSpot实现的典型成本是多少?

I do understand that multiple implementations of the JVM exist. What is the typical cost in a typical 32-bit HotSpot Implementation?

推荐答案

这是实现定义的,所以没有具体的答案。但是我应该能够为Hotspot回答它。

This is implementation defined, so there's no specific answer. But I should be able to answer it for Hotspot.

你需要知道的是:Hotspot总是在8字节边界上对齐对象。此外,每个对象有2个字开销。 [1]

What you need to know is: Hotspot always aligns objects on 8byte boundaries. Furthermore there are 2 words overhead for each and every object. [1]

如果我们把它们放在一起我们得到:

If we put this together we get:

32位VM:4byte整数+2个字对象header = 12bytes。这不是8的倍数,因此1个整数的成本是8:16byte的下一个倍数。

32bit VM: 4byte integer + 2 words object header = 12bytes. That's no multiple of 8 so as a result the cost for 1 integer is the next multiple of 8: 16byte.

64位VM:4byte整数+2个字= 20bytes。再次向上舍入:24字节大小。

64bit VM: 4byte integer + 2 words = 20bytes. Rounding up again: 24byte size.

引用的大小显然不会影响对象本身的大小,除非它引用了其他对象。简单的int包装器的情况。如果是这样的话,对于64位JVM,在现代JVM(8字节)上,对于32位和4byte,对于大小写为4byte< = 32gb,其中 CompressedOops

The size of a reference obviously does not play into the size of an object itself, except if it has references to other objects which isn't the case for a simple int wrapper. If it would, we'd have 4byte per reference for 32bit and 4byte for heaps <= 32gb with CompressedOops on modern JVMs (otherwise 8byte) for 64bit JVMs.

[1]感兴趣的人可以查看 share / vm / oops / oop.hpp

[1] Interested people can look at the code in share/vm/oops/oop.hpp

这篇关于Java中盒装基元的存储成本是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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