阵列占用多少空间? [英] How much space does an array occupy?

查看:107
本文介绍了阵列占用多少空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建10个整数和10个整数数组,那么总占用空间会有所不同吗?

If I create 10 integers and an integer array of 10, will there be any difference in total space occupied?

我必须创建一个包含数百万条记录的布尔数组,所以我想了解数组本身将占用多少空间.

I have to create a boolean array of millions of records, so I want to understand how much space will be taken by array itself.

推荐答案

整数数组表示为用于存储整数的内存块以及一个对象标头.对于32位JVM,对象标头通常需要3个32位字,但这取决于平台.(标头包含一些标志位,对类描述符的引用,原始锁信息的空间以及实际数组的长度.加上填充.)

An array of integers is represented as block of memory to hold the integers, and an object header. The object header typically takes 3 32bit words for a 32 bit JVM, but this is platform dependent. (The header contains some flag bits, a reference to a class descriptor, space for primitive lock information, and the length of the actual array. Plus padding.)

所以10个整数的数组可能需要 13 * 4 个字节.

So an array of 10 ints probably takes in the region of 13 * 4 bytes.

对于 Integer [] 而言,每个Integer对象都有一个2字的标题和一个包含实际值的1字的字段.并且您还需要添加填充,并添加1个单词(或在64位JVM上添加1到2个单词)作为参考.通常,数组的每个元素5个字或20个字节...除非某些Integer对象出现在数组的多个位置.

In the case on an Integer[], each Integer object has a 2 word header and a 1 word field containing the actual value. And you also need to add in padding, and 1 word (or 1 to 2 words on a 64-bit JVM) for the reference. That is typically 5 words or 20 bytes per element of the array ... unless some Integer objects appear in multiple places in the array.

注意:

  1. 在64位JVM上实际用于引用的单词数取决于是否使用了压缩的oops".
  2. 在某些JVM上,堆节点以16字节的倍数分配……这会增加空间使用量(例如,上面提到的填充).
  3. 如果您使用对象的身份哈希码,并且该对象在下一个垃圾回收中幸存下来,则其大小会至少膨胀4个字节以缓存哈希码值.
  4. 除了上面列举的可变性来源以外,这些数字都是特定于版本和供应商的.

这篇关于阵列占用多少空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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