java 一个数组占用多少空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11466553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:13:29  来源:igfitidea点击:

How much space does an array occupy?

javaarrays

提问by banjara

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

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

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.

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

采纳答案by Stephen C

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.)

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

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

所以一个 10 个整数的数组可能会占用13 * 4字节区域。

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.

在 的情况下Integer[],每个 Integer 对象都有一个 2 字的标题和一个包含实际值的 1 字的字段。并且您还需要添加padding和1个字(或64位JVM上的1到2个字)以供参考。这通常是数组的每个元素 5 个字或 20 个字节……除非某些 Integer 对象出现在数组中的多个位置。



Notes:

笔记:

  1. The number of words actually used for a reference on a 64 bit JVM depends on whether "compressed oops" are used.
  2. On some JVMs, heap nodes are allocated in multiples of 16 bytes ... which inflates space usage (e.g. the padding mentioned above).
  3. If you take the identity hashcode of an object and it survives the next garbage collection, its size gets inflated by at least 4 bytes to cache the hashcode value.
  4. These numbers are all version and vendor specific, in addition to the sources of variability enumerated above.
  1. 在 64 位 JVM 上实际用于引用的字数取决于是否使用“压缩 oops”。
  2. 在某些 JVM 上,堆节点以 16 字节的倍数分配......这会增加空间使用量(例如上面提到的填充)。
  3. 如果您使用对象的标识哈希码并且它在下一次垃圾回收中幸存下来,则其大小会膨胀至少 4 个字节以缓存哈希码值。
  4. 除了上面列举的可变性来源之外,这些数字都是特定于版本和供应商的。

回答by Thilo

Some rough lower bounds calculations:

一些粗略的下界计算:

Each int takes up four bytes. = 40 bytes for ten

每个 int 占用四个字节。= 10 个 40 个字节

An int array takes up four bytes for each component plus four bytes to store the length plus another four bytes to store the reference to it. = 48 bytes (+ maybe some padding to align all objects at 8 byte boundaries)

一个 int 数组为每个组件占用四个字节加上四个字节来存储长度加上另外四个字节来存储对它的引用。= 48 字节(+ 可能有一些填充以在 8 字节边界对齐所有对象)

An Integer takes up at least 8 bytes, plus the another four bytes to store the reference to it. = at least 120 for ten

一个整数至少占用 8 个字节,再加上另外 4 个字节来存储对它的引用。= 至少 120 为 10

An Integer array takes up at least the 120 bytes for the ten Integers plus four bytes for the length, and then maybe some padding for alignment. Plus four bytes to store the reference to it. (@Marko reports that he even measured about 28 bytes per slot, so that would be 280 bytes for an array of ten).

一个整数数组至少占用 120 个字节的十个整数加上四个字节的长度,然后可能还有一些用于对齐的填充。加上四个字节来存储对它的引用。(@Marko 报告说,他甚至测量了每个插槽大约 28 个字节,因此对于 10 个数组来说,这将是 280 个字节)。

回答by Razvan

In java you have both Integer and int. Supposing you are referring to int , an array of ints is considered an object and objects have metadata so an array of 10 ints will occupy more than 10 int variables

在 Java 中,您同时拥有 Integer 和 int。假设您指的是 int ,则将 int 数组视为对象,并且对象具有元数据,因此 10 个 int 的数组将占用 10 个以上的 int 变量

回答by Thihara

In light of your comment it will not make much difference if you used an array. Array will use a negligible amount of memory for its functionality itself. All other memory will be used by the stored objects.

根据您的评论,如果您使用数组,则不会有太大区别。Array 将为其功能本身使用可忽略的内存量。存储的对象将使用所有其他内存。

EDIT: What you need to understand is that the difference between Boolean wrapper and boolean primitive type. Wrapper types will usually take up more space than the primitives. So for missions of records try to go with the primitives.

编辑:您需要了解的是布尔包装器和布尔基本类型之间的区别。包装器类型通常比基元占用更多空间。因此,对于记录任务,请尝试使用原语。

Another thing to keep in mind when dealing of missions of record as you said is Java Autoboxing. The performance hit can be significant if you unintentionally use this in a function that traverses the whole array.

正如您所说,在处理记录任务时要记住的另一件事是 Java 自动装箱。如果您在遍历整个数组的函数中无意中使用它,则性能损失可能会很大。

回答by Marko Topolnik

What you can do is measure:

你可以做的是测量

public static void main(String[] args) {
  final long startMem = measure();
  final boolean[] bs = new boolean[1000000];
  System.out.println(measure() - startMem);
  bs.hashCode();
}
private static long measure() {
  final Runtime rt = Runtime.getRuntime();
  rt.gc();
  try { Thread.sleep(20); } catch (InterruptedException e) {}
  rt.gc();
  return rt.totalMemory() - rt.freeMemory();
}

Of course, this goes with the standard disclaimer: gc()has no particular guarantees, so repeat several times to see if you are getting consistent results. On my machine the answer is one byte per boolean.

当然,这符合标准的免责声明:gc()没有特别的保证,所以重复几次,看看你是否得到一致的结果。在我的机器上,答案是每个boolean.

回答by Ikabot

It needn't reflect poorly on the teacher / interviewer.

它不必对老师/面试官产生不良影响。

How much you care about the size and alignment of variables in memory depends on how performant you need your code to be. It matters a lot if your software processes transactions (EFT / stock market) for example.

您对内存中变量的大小和对齐的关心程度取决于您需要代码的性能。例如,如果您的软件处理交易(电子转帐/股票市场),这很重要。

The size, alignment, and packing of your variables in memory can influence CPU cache hits/misses, which can influence the performance of your code by up to a factor of 100.

内存中变量的大小、对齐和打包会影响 CPU 缓存命中/未命中,这可能会影响代码的性能高达 100 倍。

It's not a bad thing to know what's happening at a low level, as long as you use performance boosting tricks responsibly.

只要您负责任地使用性能提升技巧,了解低级别发生的事情并不是一件坏事。

For example, I came to this thread because I needed to know the answer to exactly this question, so that I can size my arrays of primitives to fill an integer multiple of CPU cache lines because I need the code that is performing calculations over those arrays of primitives to execute quickly because I have a finite window in which I need my calculations to be ready for the consumer of the result.

例如,我来到这个线程是因为我需要确切地知道这个问题的答案,以便我可以调整基元数组的大小以填充 CPU 缓存行的整数倍,因为我需要在这些数组上执行计算的代码的原语快速执行,因为我有一个有限的窗口,我需要我的计算为结果的使用者做好准备。

回答by Ramazan Polat

I mean if i create 10 integers and integer array of 10, will there be any difference in total space occupied.

我的意思是如果我创建 10 个整数和 10 个整数数组,那么占用的总空间会有什么不同。

(integer array of 10) = (10 integers) + 1 integer

The last "+1 integer" is for index of array ( arrays can hold 2,147,483,647 amount of data, which is an integer). That means when you declare an array, say:

最后一个“+1 整数”用于数组的索引(数组可以容纳 2,147,483,647 个数据量,这是一个整数)。这意味着当你声明一个数组时,说:

int[] nums = new int[10];

you actually reserve 11 int space from memory. 10 for array elements and +1 for array itself.

您实际上从内存中保留了 11 个 int 空间。10 表示数组元素,+1 表示数组本身。

回答by マルちゃん だよ

If you use an array you have 11 Objects, 10 integers and the array, plus Arrays have other metadata inside. So using an array will take more memory space.

如果你使用一个数组,你有 11 个对象、10 个整数和数组,再加上数组里面有其他元数据。所以使用数组会占用更多的内存空间。

Now for real. This kind of question actually comes up in job interviews and exams, and that shows you what kind of interviewer or teacher you have... with so many layers of abstraction working down there in the VM and in the OS itself, what is the point on thinking on this stuff? Micro-optimizing memory...!

现在是真的。这种问题实际上出现在求职面试和考试中,这表明你有什么样的面试官或老师......在虚拟机和操作系统本身中有这么多抽象层在工作,重点是什么在思考这个东西?微优化内存...!

回答by Stefano

In terms of RAM space, there is no real difference

在RAM空间方面,没有真正的区别