adam bien's blog

How Big Is An Java Object? 📎

Smaller, than you may think. The class BigEntity:

public class BigEntity {

    private long id;
    private String name;
    private String secondName;
    private String description;
    private Date date;
    private int number;

    public BigEntity() {
        this.id = System.currentTimeMillis();
        this.name ="duke" + this.id;
        this.secondName = "java" + this.id;
        this.description = "desc" + this.id;
        this.date = new Date();
        this.number = 42;
    }
}


...was instantiated 50.000 times with:

public class HowBigIsAnObject {
   public final static List list = new ArrayList();
    public static void main(String[] args) throws InterruptedException {
        for(int i=0;i<50000;i++){
            list.add(new BigEntity());
        }
    }
}


The result is: 50,000 instances consume 3,200,000 bytes (~3MB). One instance takes 64 bytes. The size was measured with: visualvm.