/java-object-layout

Low-level Java object layout dumpers

Primary LanguageJava

Java Object Layout Dumpers
---------------------------------------------------------------------

These are the very tiny tools to analyze object layout schemes in JVMs.
These tools are using Unsafe heavily to deduce the *actual* object
layout and footprint. This makes the tools much more accurate than
others relying on heap dumps, specification assumptions, etc.

Prepare:
  $ mvn clean install

Dump the fields of some class:
  $ java -jar target/java-object-layout.jar java.util.HashMap
     Running 64-bit HotSpot VM.
     Using compressed references with 3-bit shift.
     Objects are 8 bytes aligned.

     java.util.HashMap
      offset  size       type description
           0    12            (object header + first field alignment)
          12     4        Set AbstractMap.keySet
          16     4 Collection AbstractMap.values
          20     4        int HashMap.size
          24     4        int HashMap.threshold
          28     4      float HashMap.loadFactor
          32     4        int HashMap.modCount
          36     4        int HashMap.hashSeed
          40     1    boolean HashMap.useAltHashing
          41     3            (alignment/padding gap)
          44     4    Entry[] HashMap.table
          48     4        Set HashMap.entrySet
          52     4            (loss due to the next object alignment)
          56                  (object boundary, size estimate)
      VM reports 56 bytes per instance

Traverse the reachability graph and count instances and their sizes
(this should be more convenient to use once we publish the Maven artifact):
  $ java -jar target/java-object-graph.jar java.util.HashMap
     Running 64-bit HotSpot VM.
     Using compressed references with 3-bit shift.
     Objects are 8 bytes aligned.

     java.util.HashMap instance:
      count  size   avg description
          1    80    80 [Ljava.util.HashMap$Entry;
          1    56    56 java.util.HashMap
          2   136       (total)