title: What's the size cost of Java inheritance?
body: <p>There are various articles out there on the interwebs that try to empirically estimate the overhead of a <code>java.lang.Object</code> in particular JVM implementations. For example, I've seen the size overhead of a bare <code>Object</code> <a href="http://www.javamex.com/tutorials/memory/object_memory_usage.shtml" rel="noreferrer">estimated at 8 bytes</a> in some JVMs.</p>
  
  <p>What I would like to know is whether a typical JVM implementation of the <code>extends</code> relationship introduces incremental size overhead at every level of the class hierarchy. In other words, suppose you have a class hierarchy with N levels of subclasses. Is the overhead of the in-memory representation of a class instance O(1) or O(N)?</p>
  
  <p>I imagine it is O(1) because although the size of some of the hidden fluffy stuff you need to be a Java <code>Object</code> (vtable, chain of classes) will grow as the inheritance hierarchy grows, they grow per-class, not per-instance, and the JVM implementation can store constant-size pointers to these entities in a constant-size header attached to every <code>Object</code>.</p>
  
  <p>So in theory, the overhead directly attached to the in-memory representation of any Java object should be O(1) for inheritance depth N. Does anyone know if it's true in practice?</p>