Java 中包含一个布尔值的数组比独立变量小吗?

发布于 2024-12-28 13:06:30 字数 289 浏览 1 评论 0原文

我对 SO 的搜索失败了,所以如果这是重复的,请重定向我。

有了这个,我的问题是:我从经验和浏览中了解到,如果将 Java boolean 声明为一个,它就会存储为 32 位 int独立值,但如果您在数组中声明它,则为 8 位字节。我的问题如下:哪个内存效率更高?数组的元数据是否使其在内存中比替代方案更大?

boolean oneVariable = false, oneArray[] = {false};

My searches on SO have failed me, so if this is a duplicate, please redirect me.

With that out of the way, my question: I learned, from experience and browsing SO that a Java boolean is stored as a 32-bit int if you declare it as a standalone value, but as an 8-bit byte if you declare it within an array. My question, here, is as follows: Which is more memory efficient? Does the meta data of the array make it bigger in memory than the alternative?

boolean oneVariable = false, oneArray[] = {false};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

三五鸿雁 2025-01-04 13:06:30

数组是一个实际的对象,会带来内存损失(我相信是 12 字节),因此原始布尔值较小。

The Array is an actual Object that comes with a memory penalty (I believe 12 bytes) So the primitive boolean is smaller.

停滞 2025-01-04 13:06:30

数组的“元数据”包括:

  • 8 字节(32 位 JVM)或 16 字节(64 位 JVM)用于对象头
  • 4 字节(32 位)用于数组长度

为数组添加 1 个必要字节布尔数据,并且至少有 13 个字节(32 位)或 21 个字节(64 位)。

然而,对象以 8 字节的倍数分配内存,因此即使您只需要 12 或 20 字节的开销 + 1 字节的布尔值,您最终也会为数组对象分别使用 16 或 24 字节的内存。

除了对象本身占用的 16/24 字节之外,您还需要 4 字节(32 位)或 8 字节(64 位)作为对象的内存地址,总共分别需要 20 或 32 字节的内存,将布尔值存储在数组中。

独立变量的大小取决于 JVM。 Java 没有指定存储的大小,事实上 Oracle 说

此数据类型表示一位信息,但其“大小”并未精确定义。

较旧的 JVM 使用 32 位堆栈单元,用于保存局部变量、方法参数和表达式值,因此用作变量的单个布尔值将消耗 4 个字节;使得数组的成本至少是单个布尔值的 5 倍。例如,如果布尔值是一个类变量,则这个答案可能会有所不同,在这种情况下,它只是添加到现有开销中的单个字节。在较新的 JVM 中,单个布尔值仅使用 1 个字节,但根据其上下文和对齐内存地址所需的 8 字节填充,仍可能消耗最多 8 个字节的堆空间。它仍然比布尔数组小。

The "meta data" of the array includes:

  • 8 bytes (32-bit JVM) or 16 bytes (64-bit JVM) for object header
  • 4 bytes (32 bits) for the length of the array

Add on the 1 necessary byte for the boolean data and you have 13 bytes (32 bit) or 21 bytes (64 bit) at a minimum.

However, objects are allocated memory in 8-byte multiples, so even though you only need 12 or 20 bytes of overhead + 1 byte for the boolean, you'll end up using 16 or 24 bytes of memory, respectively, for your array object.

In addition to the 16/24 bytes the object itself will take up, you'll need 4 bytes (32 bit) or 8 bytes (64 bit) for the memory address of the object, totaling 20 or 32 bytes of memory, respectively, to store your boolean in an array.

The size of a standalone variable is JVM dependent. Java does not specify the size of storage, and in fact Oracle says

This data type represents one bit of information, but its "size" isn't something that's precisely defined.

Older JVMs use a 32-bit stack cell, used to hold local variables, method arguments, and expression values so a single boolean used as a variable would consume 4 bytes; making the array at least 5 times as expensive as for a single boolean. This answer may be different if, for example, the boolean is a class variable in which case it would just be a single byte added to the existing overhead. In newer JVMs a single boolean would only use 1 byte, but depending on its context and the 8-byte padding necessary to align memory addresses, could still consume up to 8 bytes of heap space. It would still be smaller than the boolean array.

地狱即天堂 2025-01-04 13:06:30

正如 user949300 提到的,所有对象都带有惩罚,使它们比基元更大。不过,对于单个布尔值来说,内存并不重要。如果要存储大量布尔值,请考虑使用 位集。我相信在幕后它每个布尔值使用大约 1 位(加上一些开销)。

As user949300 mentioned, all objects carry a penalty that make them larger than primitives. For only a single boolean though, memory doesn't really matter. If you are storing a large number of booleans, consider using a BitSet. I believe under the hood it uses approximately 1 bit per boolean (plus some overhead).

凉宸 2025-01-04 13:06:30

这篇 Java 专家文章是了解内存的一个很好的来源用法。

This Java specialist article is a good source for understanding the memory usage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文