
You can get a summary view of the deep size of an object instance using GraphLayout.parseInstance(obj).toFootprint(). Space losses: 2 bytes internal + 4 bytes external = 6 bytes total Instance size: 72 bytes (reported by Instrumentation API) It does include VM overhead for the object header, field alignment and padding. This is only the space consumed by a single instance of that class it does not include any other objects referenced by that class.

You can get the shallow size of an object instance using ClassLayout.parseClass(Foo.class).toPrintable() (optionally passing an instance to toPrintable). On Oracle JDK 1.8.0_40 running on 64-bit Windows (used for all following examples), this method returns Running 64-bit HotSpot VM.įield sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 Īrray element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 To get the sizes of primitives, references and array elements, use VMSupport.vmDetails(). This makes JOL much more accurate than other tools relying on heap dumps, specification assumptions, etc. These tools are using Unsafe, JVMTI, and Serviceability Agent (SA) heavily to decoder the actual object layout, footprint, and references.

JOL (Java Object Layout) is the tiny toolbox to analyze object layout schemes in JVMs.

You should use jol, a tool developed as part of the OpenJDK project.
