喵喵时光机
您可以使用jol来获取该类的布局。(但是要小心,你可能需要更深入地了解它背后的机制,不要盲目相信结果,并且要意识到它只是对当前使用的VM的估计(在我的情况下1.7.0_76 x64获胜:):我使用CLI版本我想正确的方法是将库包含在您的项目中,但无论如何,它似乎以这种方式工作:test>java -cp target\classes;jol-cli-0.3.1-full.jar org.openjdk.jol.Main internals test.CheckStoreRunning 64-bit HotSpot VM.Using compressed oop with 0-bit shift.Using compressed klass with 0-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]VM fails to invoke the default constructor, falling back to class-only introspection.test.CheckStore object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 12 (object header) N/A 12 1 boolean CheckStore.state N/A 13 3 (alignment/padding gap) N/A 16 4 String CheckStore.displayText N/A 20 4 String CheckStore.meaningfulText N/A 24 4 URL CheckStore.url N/A 28 4 (loss due to the next object alignment)Instance size: 32 bytes (estimated, the sample instance is not available)Space losses: 3 bytes internal + 4 bytes external = 7 bytes total以及自动压缩oops关闭:test>java -XX:-UseCompressedOops -cp target\classes;jol-cli-0.3.1-full.jar org.openjdk.jol.Main internals test.CheckStoreRunning 64-bit HotSpot VM.Objects are 8 bytes aligned.Field sizes by type: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]Array element sizes: 8, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]VM fails to invoke the default constructor, falling back to class-only introspection.test.CheckStore object internals: OFFSET SIZE TYPE DESCRIPTION VALUE 0 16 (object header) N/A 16 1 boolean CheckStore.state N/A 17 7 (alignment/padding gap) N/A 24 8 String CheckStore.displayText N/A 32 8 String CheckStore.meaningfulText N/A 40 8 URL CheckStore.url N/AInstance size: 48 bytes (estimated, the sample instance is not available)Space losses: 7 bytes internal + 0 bytes external = 7 bytes total如果您的字段为空,那些只是对象本身的布局,那么它不会指向更多对象,否则您还必须查看目标类型(URL和String)。(如果你有多个所有实例,则取决于你使用相同的多次或不同的实例)。无法在内存中跳过空字段,因为它需要在分配实例时调整其大小。所以这些字段都是预先构造的,它们只是不会在堆上的其他地方引用已分配的对象。注意:如果实现默认构造函数,则会获得更多详细信息,但在此特定情况下的大小调整将是相同的。如果你想知道字段的序列和填充来自哪里,你可以查看这篇文章 - (基本上它对齐8个字节的对象,按大小对字段进行排序,将相同类型组合在一起,引用最后。超类型的字段是第一个, 4字节对齐。)