您可以使用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.CheckStore
Running 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
並具有自動壓縮哎呀關閉相同:
test>java -XX:-UseCompressedOops -cp target\classes;jol-cli-0.3.1-full.jar org.openjdk.jol.Main internals test.CheckStore
Running 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/A
Instance size: 48 bytes (estimated, the sample instance is not available)
Space losses: 7 bytes internal + 0 bytes external = 7 bytes total
這些只是對象本身的佈局如果您的字段爲空,則它不會指向更多的對象,否則您必須查看目標類型(URL
和String
)。 (如果你有多個實例的話,這取決於你使用相同的多次還是不同的次數)。空字段不能在內存中跳過,因爲它需要實例在分配時調整大小。所以這些字段都是預先構建的,它們只是不會在堆上的其他地方引用分配的對象。
注意:如果你實現了一個默認的構造函數,你會得到更多的細節,但在這個特定情況下的尺寸是相同的。如果你想知道字段序列和填充是從哪裏來的,你可以檢查this article - (基本上它將8個字節的對象對齊,按大小對字段進行排序,將相同類型組合在一起,引用最後一個字段。字節對齊。)
請你也請告訴我爲什麼需要差異。 64位和32位系統上的空間? – 2010-03-12 05:28:28
@Yatendra:指針必須足夠大才能跨越整個地址空間。因此,在32位機器上,你需要32位。在64位機器上,你需要64位。 – dsimcha 2010-03-12 05:29:57
不一定......在HotSpot中有一些有趣的工作可以避免這種情況:http://wikis.sun.com/display/HotSpotInternals/CompressedOops – 2010-03-12 06:28:58