使用JOL工具查看java中类的字段分布情况

这篇博客介绍了如何利用JOL工具查看Java对象,如HashMap,在内存中的分布情况。通过执行特定的JOL命令,可以获取对象的内部结构和估计的内存占用。

本文是郑雨迪老师的“深入拆解 Java 虚拟机”第10节 | Java对象的内存布局 的课后作业

下载jol-cli-0.9-full.jar

JOL简介:
可以查阅java虚拟机中对象的内存分布,是OpenJDK的Code Tools项目中的工具,具体可以通过如下两套指令来实现:

$ java -jar /path/to/jol-cli-0.9-full.jar internals java.util.HashMap
$ java -jar /path/to/jol-cli-0.9-full.jar estimates java.util.HashMap

D:\CQQ>java -cp jol-cli-0.9-full.jar org.openjdk.jol.Main internals java.lang.String
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. Unable to attach even with module exceptions: [org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed.]
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# 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]

Instantiated the sample instance via default constructor.

java.lang.String object internals:
 OFFSET  SIZE     TYPE DESCRIPTION                               VALUE
      0     4          (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4          (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4          (object header)                           d0 16 c8 16 (11010000 00010110 11001000 00010110) (382211792)
     12     4   char[] String.value                              []
     16     4      int String.hash                               0
     20     4          (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total


D:\CQQ>java -cp jol-cli-0.9-full.jar org.openjdk.jol.Main estimates java.lang.String
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. Unable to attach even with module exceptions: [org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed.]
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# 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]

***** 32-bit VM: **********************************************************
java.lang.String object internals:
 OFFSET  SIZE     TYPE DESCRIPTION                               VALUE
      0     8          (object header)                           N/A
      8     4   char[] String.value                              N/A
     12     4      int String.hash                               N/A
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

***** 64-bit VM: **********************************************************
java.lang.String object internals:
 OFFSET  SIZE     TYPE DESCRIPTION                               VALUE
      0    16          (object header)                           N/A
     16     8   char[] String.value                              N/A
     24     4      int String.hash                               N/A
     28     4          (loss due to the next object alignment)
Instance size: 32 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

***** 64-bit VM, compressed references enabled: ***************************
java.lang.String object internals:
 OFFSET  SIZE     TYPE DESCRIPTION                               VALUE
      0    12          (object header)                           N/A
     12     4   char[] String.value                              N/A
     16     4      int String.hash                               N/A
     20     4          (loss due to the next object alignment)
Instance size: 24 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

***** 64-bit VM, compressed references enabled, 16-byte align: ************
java.lang.String object internals:
 OFFSET  SIZE     TYPE DESCRIPTION                               VALUE
      0    12          (object header)                           N/A
     12     4   char[] String.value                              N/A
     16     4      int String.hash                               N/A
     20    12          (loss due to the next object alignment)
Instance size: 32 bytes
Space losses: 0 bytes internal + 12 bytes external = 12 bytes total

下面是看hashmap


D:\CQQ>java -cp jol-cli-0.9-full.jar org.openjdk.jol.Main internals java.util.HashMap
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. Unable to attach even with module exceptions: [org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed.]
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# 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]

Instantiated the sample instance via default constructor.

java.util.HashMap object internals:
 OFFSET  SIZE                       TYPE DESCRIPTION                               VALUE
      0     4                            (object header)                           01 00 00 00 (00000001 00000000 00000000 00000000) (1)
      4     4                            (object header)                           00 00 00 00 (00000000 00000000 00000000 00000000) (0)
      8     4                            (object header)                           18 bd c8 16 (00011000 10111101 11001000 00010110) (382254360)
     12     4              java.util.Set AbstractMap.keySet                        null
     16     4       java.util.Collection AbstractMap.values                        null
     20     4                        int HashMap.size                              0
     24     4                        int HashMap.modCount                          0
     28     4                        int HashMap.threshold                         0
     32     4                      float HashMap.loadFactor                        0.75
     36     4   java.util.HashMap.Node[] HashMap.table                             null
     40     4              java.util.Set HashMap.entrySet                          null
     44     4                            (loss due to the next object alignment)
Instance size: 48 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total


D:\CQQ>java -cp jol-cli-0.9-full.jar org.openjdk.jol.Main estimates java.util.HashMap
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. Unable to attach even with module exceptions: [org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed., org.openjdk.jol.vm.sa.SASupportException: Sense failed.]
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# 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]

***** 32-bit VM: **********************************************************
java.util.HashMap object internals:
 OFFSET  SIZE                       TYPE DESCRIPTION                               VALUE
      0     8                            (object header)                           N/A
      8     4              java.util.Set AbstractMap.keySet                        N/A
     12     4       java.util.Collection AbstractMap.values                        N/A
     16     4                        int HashMap.size                              N/A
     20     4                        int HashMap.modCount                          N/A
     24     4                        int HashMap.threshold                         N/A
     28     4                      float HashMap.loadFactor                        N/A
     32     4   java.util.HashMap.Node[] HashMap.table                             N/A
     36     4              java.util.Set HashMap.entrySet                          N/A
Instance size: 40 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

***** 64-bit VM: **********************************************************
java.util.HashMap object internals:
 OFFSET  SIZE                       TYPE DESCRIPTION                               VALUE
      0    16                            (object header)                           N/A
     16     8              java.util.Set AbstractMap.keySet                        N/A
     24     8       java.util.Collection AbstractMap.values                        N/A
     32     4                        int HashMap.size                              N/A
     36     4                        int HashMap.modCount                          N/A
     40     4                        int HashMap.threshold                         N/A
     44     4                      float HashMap.loadFactor                        N/A
     48     8   java.util.HashMap.Node[] HashMap.table                             N/A
     56     8              java.util.Set HashMap.entrySet                          N/A
Instance size: 64 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

***** 64-bit VM, compressed references enabled: ***************************
java.util.HashMap object internals:
 OFFSET  SIZE                       TYPE DESCRIPTION                               VALUE
      0    12                            (object header)                           N/A
     12     4              java.util.Set AbstractMap.keySet                        N/A
     16     4       java.util.Collection AbstractMap.values                        N/A
     20     4                        int HashMap.size                              N/A
     24     4                        int HashMap.modCount                          N/A
     28     4                        int HashMap.threshold                         N/A
     32     4                      float HashMap.loadFactor                        N/A
     36     4   java.util.HashMap.Node[] HashMap.table                             N/A
     40     4              java.util.Set HashMap.entrySet                          N/A
     44     4                            (loss due to the next object alignment)
Instance size: 48 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

***** 64-bit VM, compressed references enabled, 16-byte align: ************
java.util.HashMap object internals:
 OFFSET  SIZE                       TYPE DESCRIPTION                               VALUE
      0    12                            (object header)                           N/A
     12     4              java.util.Set AbstractMap.keySet                        N/A
     16     4       java.util.Collection AbstractMap.values                        N/A
     20     4                        int HashMap.size                              N/A
     24     4                        int HashMap.modCount                          N/A
     28     4                        int HashMap.threshold                         N/A
     32     4                      float HashMap.loadFactor                        N/A
     36     4   java.util.HashMap.Node[] HashMap.table                             N/A
     40     4              java.util.Set HashMap.entrySet                          N/A
     44     4                            (loss due to the next object alignment)
Instance size: 48 bytes
Space losses: 0 bytes internal + 4 bytes external = 4 bytes total

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值