java中的引用类型大小 [英] reference type size in java

查看:246
本文介绍了java中的引用类型大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么java中的引用类型占用8个字节?为什么不少于或多于8个字节?

Why does a reference type in java take 8 bytes? Why not less or more than 8 bytes?

推荐答案

实际上,它没有指定引用变量应该有多少字节,并且事实上,它并不是无处不在。

Actually, it is nowhere specified how much bytes a reference variable shall have, and in fact, it is not everywhere the same.

32位系统的常见虚拟机(即内存总线中具有32位地址的系统)通常使用32位(= 4个字节,与 int float 相同)作为对象引用的大小,而64-的虚拟机位系统通常使用64位(= 8字节)的本机地址。 (请注意,大多数64位系统也可以运行32位程序,所以即使在那里你也常常使用32位VM。)

Common virtual machines for 32-bit systems (i.e. systems with 32 bit adresses in the memory bus) usually use 32-bit (= 4 bytes, same as int and float) as the size for object references, while virtual machines for 64-bit systems often use the native address size 64 bits (= 8 bytes) for these. (Note that most 64 bit systems can run 32-bit programs, too, so often even there you'll be using a 32 bit VM.)

它只是一个简化实现的问题,如果你可以使用实际的内存地址作为你的引用而不是别的东西。

It is simply a matter of simplifying the implementation, if you can use an actual memory address for your references instead of something else.

因为这增加了所用内存的大小(我们经常实际上需要访问 更多的内存),来自Java 7,在64位HotSpot VM上能够在某些条件下使用32位引用,即当堆小于32 GB时(8· 2 32 字节)。要将它们转换为实际的内存地址,它们将乘以8(因为对象将在8字节边界上对齐),然后将其添加到基址(如果这不是零)。 (对于小于4 GB的堆,我们不需要乘法步骤。)

As this increases the size of memory used (and often we don't actually need to access that much memory), from Java 7 on the 64-bit HotSpot VM will be able to use 32-bit references under certain conditions, i.e. when the heap is smaller than 32 GB (8·232 bytes). To convert them to a actual memory address, they are multiplied by 8 (as objects will be aligned on 8-byte-boundaries) and then added to the base address (if this is not zero). (For heaps smaller than 4 GB, we don't need the multiplication step.)

其他虚拟机可能使用类似的技巧。

Other VMs might use similar tricks.

这篇关于java中的引用类型大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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