I have this class :
public class Stack {
private class Node {
String item;
Node next;
}
// some other methods here
}
In my book, the author says that the size per stack Node is 40 bytes including:
16 bytes (object overhead)
8 bytes (inner class extra overhead)
8 bytes (references to string)
8 bytes (references to node)
----------------------------------------------
40 bytes per stack node
I understand that the last two things refer to the size of the references to the String and Node. But I don’t know what the object overhead and inner class extra overhead correspond to. Can you please explain?
Every object has a header which is typically 8-12 bytes long. Each object is also 8 byte aligned and a simple estimate is you say its about 16 bytes long.
As your inner class is not static, it has a reference to the outer class.
If this were an anonymous inner class you might have copies of any number of final variables (any used in the anonymous class code)
Most JVMs use 32-bit references so the size would be 4 bytes. Even 64-bit JVM with a heap up to 32 GB can use 32-bit references.