由于发布了原始问题,情况发生了一些变化。
jol工具(Aleksey Shipilev的“java 对象布局”)现在是 OpenJDK 的一部分,允许您检查类的实际内存布局和使用情况。http://openjdk.java.net/projects/code-tools/jol/
示例输出如下所示:
$ java -jar jol-cli/target/jol-internals.jar java.util.HashMap
  Running 64-bit HotSpot VM.
  Using compressed references with 3-bit shift.
  Objects are 8 bytes aligned.
  Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
  Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
  java.util.HashMap object internals:
   OFFSET  SIZE       TYPE DESCRIPTION                    VALUE
        0     4            (object header)                01 00 00 00 (00000001 00000000 00000000 00000000)
        4     4            (object header)                00 00 00 00 (00000000 00000000 00000000 00000000)
        8     4            (object header)                0f 0f 3e e0 (00001111 00001111 00111110 11100000)
       12     4        Set AbstractMap.keySet             null
       16     4 Collection AbstractMap.values             null
       20     4        int HashMap.size                   0
       24     4        int HashMap.threshold              16
       28     4      float HashMap.loadFactor             0.75
       32     4        int HashMap.modCount               0
       36     4        int HashMap.hashSeed               0
       40     4    Entry[] HashMap.table                  []
       44     4        Set HashMap.entrySet               null
  Instance size: 48 bytes (estimated, add this JAR via -javaagent: to get accurate result)
  Space losses: 0 bytes internal + 0 bytes external = 0 bytes total