I have read this article http://btoddb-java-sizing.blogspot.com/
Author explain how to calculate Objects size in Java. For example:
import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator;
public final class Perfomance {
static int number;
static int anotherNumber;
public static void main(String[] args) {
System.out.println(ObjectSizeCalculator.getObjectSize(new Integer(2)));
}
}
This code will show 16 because new object takes 12 bytes and inside Integer we have int(4 bytes) 12+4 = 16.
But consider this code:
System.out.println(ObjectSizeCalculator.getObjectSize(Integer.class));
Now its size 56 , moreover
System.out.println(ObjectSizeCalculator.getObjectSize(Object.class));
Its show 288. How does it work. How allocated memory depends on class ?