When calculate memory size of an object array, following code gives "used 24 bytes" as expected, which, as far as I know, consists of:
4bytes(element pointer)+16bytes(object header)+4bytes(element space) = 24bytes
// with JVM argument -XX:-UseTLAB
public static void main(String[] args) {
long size = memoryUsed();
Object[] o = new Object[]{1};
//Object[] o = new Object[]{1L};
size = memoryUsed() - size;
System.out.printf("used %,d bytes%n", size);
//Output: used 24 bytes
}
public static long memoryUsed() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
But when element type changed to Long (1L), the result is confusing, most of the time it's "used 9,264 bytes", anyone can help enlighten me? what is the difference in memory allocation between this two element type?
// with JVM argument -XX:-UseTLAB
public static void main(String[] args) {
long size = memoryUsed();
//Object[] o = new Object[]{1};
Object[] o = new Object[]{1L};
size = memoryUsed() - size;
System.out.printf("used %,d bytes%n", size);
//Output: used 9,264 bytes
}