[英]Size of a 'long' in memory - Java
I am still trying to figure out why long
and double
in Java consuming 12 bytes
instead of 8 bytes
in memory我仍在试图弄清楚为什么 Java 中的
long
和double
在内存中消耗12 bytes
而不是8 bytes
Sure there is wrong assumptions somewhere;肯定在某处有错误的假设; please guide me..
请指导我..
I have analyzed it using two ways,我用两种方法分析它,
Following is my logic/assumption/source info,以下是我的逻辑/假设/来源信息,
12 bytes = [CLASS INFO OF 4 BYTES] + [FLAGS INFO OF 4 BYTES] + [LOCK INFO OF 4 BYTES]
12 bytes = [CLASS INFO OF 4 BYTES] + [FLAGS INFO OF 4 BYTES] + [LOCK INFO OF 4 BYTES]
元数据12 bytes = [CLASS INFO OF 4 BYTES] + [FLAGS INFO OF 4 BYTES] + [LOCK INFO OF 4 BYTES]
Total Memory - Meta Data
Total Memory - Meta Data
12 bytes => 24 bytes (Total Memory) - 12 bytes (Meta Data)
12 bytes => 24 bytes (Total Memory) - 12 bytes (Meta Data)
[ ANSWER ] Difference of 4 bytes is owing to Padding Applied by JVM. [ ANSWER ] 4个字节的差异是由于JVM应用的填充。 Thanks Andy Turner...
谢谢安迪·特纳...
Quoting this answer :引用这个答案:
In a modern 64-bit JDK, an object has a 12-byte header, padded to a multiple of 8 bytes
在现代 64 位 JDK 中,对象有一个 12 字节的标头,填充为 8 字节的倍数
The extra 4 bytes is padding to get to a multiple of 8.额外的 4 个字节被填充以获得 8 的倍数。
you can check the size of double:你可以检查double的大小:
double numDouble=2;
long size=(long)(numDouble*Double.SIZE) / Byte.SIZE;
System.out.println(size);
output: 16输出:16
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.