天下文章一大抄!!!!
现在由于资源的共享,很多技术平台的文章都很丰富,但是丰富中夹杂着多数的雷同,并且也没有什么依据,可能仅仅是自己的见解和别人的见解,没有真正的回归本质真理的实践。
那么想要最浅显的弄清楚JAVA的对象布局,其实很简单:
1.创建一个Maven项目
2.引入jol的jar包
<dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.9</version>
</dependency>
3.创建实体类
package com.zking.lock;
/**
* @description:
* @author: codinglife
* @time: 2021/3/22 16:29
*/
public class SynPojo {
private String s;
private boolean b;
private boolean c;
private int i;
private float f;
private byte by;
private double d;
private String name;
private Integer id;
private Byte aByte;
private Double aDouble;
private Float aFloat;
private SynPojos synPojos;
public float getF() {
return f;
}
public void setF(float f) {
this.f = f;
}
public byte getBy() {
return by;
}
public void setBy(byte by) {
this.by = by;
}
public double getD() {
return d;
}
public void setD(double d) {
this.d = d;
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public boolean isB() {
return b;
}
public void setB(boolean b) {
this.b = b;
}
public boolean isC() {
return c;
}
public void setC(boolean c) {
this.c = c;
}
public int getI() {
return i;
}
public void setI(int i) {
this.i = i;
}
public Float getaFloat() {
return aFloat;
}
public void setaFloat(Float aFloat) {
this.aFloat = aFloat;
}
public SynPojos getSynPojos() {
return synPojos;
}
public void setSynPojos(SynPojos synPojos) {
this.synPojos = synPojos;
}
public Double getaDouble() {
return aDouble;
}
public void setaDouble(Double aDouble) {
this.aDouble = aDouble;
}
public Byte getaByte() {
return aByte;
}
public void setaByte(Byte aByte) {
this.aByte = aByte;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.zking.lock;
/**
* @description:
* @author: codinglife
* @time: 2021/3/22 16:29
*/
public class SynPojos {
private String name;
private Integer id;
private Byte aByte;
private Double aDouble;
public Double getaDouble() {
return aDouble;
}
public void setaDouble(Double aDouble) {
this.aDouble = aDouble;
}
public Byte getaByte() {
return aByte;
}
public void setaByte(Byte aByte) {
this.aByte = aByte;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.运行
package com.zking.lock;
import org.openjdk.jol.info.ClassLayout;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @description:
* @author: codinglife
* @time: 2021/3/22 16:24
*/
public class Syn {
public static void main(String[] args) {
synchronized (Syn.class){
System.out.println(ClassLayout.parseClass(SynPojo.class).toPrintable());
}
}
}
4.结果
如图可见,
对象头为12Btye
实例数据根据数据类型来判断:
8大数据类型:
每种基础类型封装类型或其他实例对象各占4Byte,这里占51个byte
对齐数据为8的倍数,这里补1个byte
java对象布局:
1.对象头 (mark word+class Metadata Address)(12Byte) 固定大小
2.实例数据 (每种封装类型或对象4Byte,8大基础类型各有大小) 和数据类型有关
3.对齐 (满足8的倍数) 对象大小为8的倍数 向上补齐