There are various articles out there on the interwebs that try to empirically estimate the overhead of a java.lang.Object
in particular JVM implementations. For example, I've seen the size overhead of a bare Object
estimated at 8 bytes in some JVMs.
What I would like to know is whether a typical JVM implementation of the extends
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)?
I imagine it is O(1) because although the size of some of the hidden fluffy stuff you need to be a Java Object
(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 Object
.
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?