64位操作系统中Java8 启动时支持的这两个启动参数有什么区别呢?
背景
这两个启动参数经常用在JVM性能调优,因为他们会影响内存中对象占用大小。
分析
java对象内存模型可以描述为:OOP-Klass
- OOP:Ordinary Object Point。这部分是所有此类对象的不同部分(对象属性指针,类属性指针,普通对象数组的每个元素指针)
- Klass:指向类元数据信息比如方法,属性等。这部分是所有此类对象相同部分
- 此外还包括Markword头,数组大小(如果有的话)
- padding(8字节对齐)
这里OOP对于参数-XX:+UseCompressedOops,Klass对应-XX:+UseCompressedClassPointers。注意Java8中增加了两块内存空间
- metaspace 代替 perm generation space
- compressed class space只有开启了UseCompressedClassPointers后
/**
* -XX:+UseCompressedOops: mark/8 + Klass/8 = 16
* -XX:-UseCompressedOops: mark/8 + Klass/4 + padding/4 = 16
*/
static class X1 {
}
/**
* -XX:+UseCompressedOops: mark/8 + Klass/4 + a/4 = 16
* -XX:-UseCompressedOops: mark/8 + Klass/8 + a/4 + padding/4 = 24
*/
static class X2 {
int a = 0;
}
/**
* -XX:+UseCompressedOops: mark/8 + Klass/4 + i/4 = 16
* -XX:-UseCompressedOops: mark/8 + Klass/8 + i/8 = 24
*/
static class X3 {
Integer i = new Integer(0);
}
/**
* -XX:+UseCompressedOops: mark/8 + Klass/4 + size/4 = 16
* -XX:-UseCompressedOops: mark/8 + Klass/8 + size/4 + padding/4 = 24
*/
static class X4 {
int[] a = new int[100];
}
/**
* -XX:+UseCompressedOops: mark/8 + Klass/4 + size/4 = 16
* -XX:-UseCompressedOops: mark/8 + Klass/8 + size/4 + padding/4 = 24
*/
static class X5 {
Integer[] a = new Integer[100];
}
/**
* -XX:+UseCompressedOops: mark/8 + Klass/4 + i/4 + l/8 + d/8 = 32
* -XX:-UseCompressedOops: mark/8 + Klass/8 + i/4 + l/8 + d/8 + padding/4= 40
*/
static class X6 {
int i = 0;
long l = 0;
double d = 0;
}