// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
public class SizeOf {
    private final static int N = 1000000;
    public static long mem() {
        System.gc();
        Runtime rt = Runtime.getRuntime();
        return rt.totalMemory() - rt.freeMemory();
    }
    public static void main(String[] args) {
        Empty[] arrEmpty = new Empty[N];
        long mstart = mem();
        for(int i = 0; i < N; i++) {
            arrEmpty[i] = new Empty();
        }
        long mend = mem();
        long std = mend - mstart;
        arrEmpty = null;
        A[] arrA = new A[N];
        long m1 = mem();
        for(int i = 0; i < N; i++) {
            arrA[i] = new A();
        }
        long m2 = mem();
        System.out.println("sizeof A = " + (m2 - m1 - std)*1.0/N);
        arrA = null;
        B[] arrB = new B[N];
        long m3 = mem();
        for(int i = 0; i < N; i++) {
            arrB[i] = new B();
        }
        long m4 = mem();
        System.out.println("sizeof B = " + (m4 - m3 - std)*1.0/N);
        arrB = null;
    }
}

class Empty {
}

class A  {
    public int a;
    public int b;
}

class B {
    public double x;
    public double y;
}
