public class C3 { //default 8 public byte b = 2 ; //1 public short s = 2 ; //2 public char c = '2' ; //2 public int i = 2 ; //4 public float f = 2.0f ; //4 public double d = 2.0 ; //8 public long l = 2l ; //8 public String str = "1" ; //4 //total 41 }
//上面忘了测试boolean,测试工具为“visualvm”
public class C4 extends C3 { //C3类型部分 大小 41 public int i = 23 ; //4 public Object obj ; //4 public Object obj2 = null ; //4 public Object obj3 = new Object(); //4 public Object obj4 = new C3(); //4 public C3 c3 = new C3(); //4 //总占用 65 //这里的占用内存多少,并没有计算对象类型变量实例(如new Object()、new C3())所占内存 //如:在这个类的实例中生成了两个“C3”类型对象共用去82byte内存(也没计算内部对象所占内存) }