一个包含1个元素的数组占用的内存是否与该1个元素的内存相同?

huangapple go评论86阅读模式
英文:

Does an array containing 1 element take up same amount of memory as that 1 element?

问题

假设有一个 Cat 对象,每当创建一个新的 Cat 时,每次都会分配相同数量的内存给它。现在看看下面的代码:

Cat aSingleCat = new Cat(); // 给 'aSingleCat' 分配了 X 量的内存

Cat[] oneElmCatArray = new Cat[1];
oneElmCatArray[0] = new Cat(); // 给这个只有一个索引的 1 元素数组分配了相同数量的内存

那么这意味着什么呢?oneElmCatArray 占用的内存与 aSingleCat 一样吗,还是会占用 更多 的内存,因为某些幕后的 Java 数组操作?

英文:

Let's say there's a Cat object, and whenever a new Cat is created, the same amount of memory is allocated to it every time. Now look at the following code:

Cat aSingleCat = new Cat(); // X amount of memory is allocated to 'aSingleCat'

Cat[] oneElmCatArray = new Cat[1];
oneElmCatArray[0] = new Cat(); // Same amount of memory is allocated to the only index of this 1-element array

So what does this mean? Does oneElmCatArray take up the same amount of memory as aSingleCat, or does it take up more because of some behind-the-scenes Java Array shenanigans?

答案1

得分: 1

> oneElmCatArray 占用的内存与 aSingleCat 相同吗?还是由于某些 Java 数组操作在幕后的原因占用的内存更多?

答案是:这取决于情况。

首先,重要的是<sup>1</sup>你理解一个 Cat[] 实际上包含什么。

  • 不包含 Cat 对象。
  • 实际上它包含要么是指向 Cat 对象的引用,要么是 null 引用。

在每种情况下,引用占据 4 或 8 字节,具体取决于 JVM 平台和(在某些情况下)命令行选项。

那么,一个 Cat 占用的内存是否比 Cat[1] 多?答案实际上取决于:

  • 引用的大小(参见上文)
  • Cat 类的实例字段的数量和类型……这在你的问题中没有具体说明。

在进行实际计算时,您需要包括各自本地对象/数组头的大小(分别为 8 和 12 字节)。此外,还要考虑 JVM 堆节点大小是 8 字节的倍数。(但这些东西在理论上取决于实现。)

最后,正如 @Louis Wasserman 指出的,答案还取决于您的“会计”规则。当 Cat[] 包含对 Cat 的引用时,您是否将 Cat 视为数组的一部分?


<sup>1 - 如果您不理解这一点,您可能会对数组及其工作原理产生错误的心理模型。这可能导致您代码中的错误。</sup>

英文:

> Does oneElmCatArray take up the same amount of memory as aSingleCat, or does it take up more because of some behind-the-scenes Java array shenanigans?

The answer is: it depends.

First of all, it is important<sup>1</sup> that you understand what a Cat[] actually contains.

  • It does not contain Cat objects.
  • It actually contains either references to Cat objects, OR null references.

In each case, a reference will occupy either 4 or 8 bytes depending on the JVM platform and (in some cases) the command line options.

So, does a Cat occupy more memory than a Cat[1]? The answer will actually depend on:

  • The size of a reference (see above)
  • The number and type of the instance fields of the Cat class ... which is unspecified in your question.

When doing the actual calculations, you need to include the sizes of the respective native object / array headers (8 and 12 bytes respectively). Also, take account of the fact that JVM heap node sizes are a multiple of 8 bytes. (But these things are in theory implementation dependent.)

Finally, as @Louis Wasserman points out, the answer also depends on your "accounting" rules. When Cat[] contains a reference to a Cat, do you count the Cat as part of the array or not?


<sup>1 - If you don't understand this, you are going to end up with an incorrect mental model of arrays and how they work. That is likely to lead to bugs in your code.</sup>

答案2

得分: 0

非常有趣的问题!是的——在Java中,与数组相关联的数组开销大于24字节。

这是因为在Java中,数组只是对象。因此,头部、变量等都会占用空间。此外,对象在Java中需要填充为8字节。由于您的元素和数组都需要填充,所以它将占用更多的内存!

另外!对象填充:https://stackoverflow.com/questions/11926415/why-do-java-objects-have-to-be-a-multiple-of-8

编辑: 在SWE StackExchange找到了一个非常好的资源
为什么在Java中分配对象/数组时会有开销?

这个答案更深入地解释了我所写的内容。如果您有兴趣了解更多,一定要查看一下! 一个包含1个元素的数组占用的内存是否与该1个元素的内存相同?

英文:

Really interesting question! Yes - there is > 24 bytes of Array overhead associated with Arrays in Java.

This is because Arrays are simply objects in Java. So the header, variables etc. add to the space. Furthermore, the objects need to be padded to 8 bytes in Java. Since your element would need to be padded and the array, too, it will take more memory!

Aside! Object Padding: https://stackoverflow.com/questions/11926415/why-do-java-objects-have-to-be-a-multiple-of-8

EDIT: Found a really great resource @ SWE StackExchange
Why the overhead when allocating objects/arrays in Java?

The answer goes more in-depth on what I wrote. You should definitely check it out if you are interested in learning more. 一个包含1个元素的数组占用的内存是否与该1个元素的内存相同?

huangapple
  • 本文由 发表于 2020年8月23日 13:28:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63543712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定