Dan*_*iel 9 java linux windows 64-bit
我想知道是否有一些方法让64位VM使用8byte对象头而不是12byte对象头,如果JVM的可用RAM仍然是4GB.
或者就像在Linux上那样,如果没有在Windows上?有人可以用这段代码测试吗?
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class ObjectSizes {
String s1;
String s2;
public static void main(String[] args) throws Exception {
Unsafe unsafe;
try {
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe)field.get(null);
} catch (Exception ex) {
throw new RuntimeException("Can't get Unsafe instance.", ex);
}
Field s1Field = ObjectSizes.class.getDeclaredField("s1");
Field s2Field = ObjectSizes.class.getDeclaredField("s2");
long s1OffSet = unsafe.objectFieldOffset(s1Field);
long s2OffSet = unsafe.objectFieldOffset(s2Field);
System.out.println("We are running "+System.getProperty("java.version"));
System.out.println("Object header size is "+s1OffSet+" bytes.");
System.out.println("Object reference size is "+(s2OffSet-s1OffSet)+" bytes.");
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*oni 16
它看起来不像在64位JVM上有一个8字节的对象头.标题由"标记字",指向对象类的指针,数组中的数组大小以及到达下一个8字节边界的填充组成.
,------------------+------------------+------------------ +---------------.
| mark word | klass pointer | array size (opt) | padding |
`------------------+------------------+-------------------+---------------'
Run Code Online (Sandbox Code Playgroud)
因此,64位系统上的对象标头可以占用8 + 4 = 12个字节,但不能少.