精华内容
下载资源
问答
  • JOL工具使用

    千次阅读 2020-03-13 09:59:04
    欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。 新的改变 我们对Markdown编辑器...

    JOL工具的使用

    今天介绍一个可以查看普通java对象的内部布局工具JOL(JAVA OBJECT LAYOUT),使用此工具可以查看new出来的一个java对象的内部布局,以及一个普通的java对象占用多少字节.

    1.Maven 导入jar

    JOLMaven依赖l

    2.使用方法

    在这里插入图片描述

    3.结果

    4.解释一下结果

    解释一下上图: 前面两行是java对象头的Mark Word共占8个字节,第三行是java对象头的Klass Pointer类指针,本来也是占8个字节,因JDK默认开启类指针压缩所以占4个字节,总共12个字节,又因12不能被8整除,所有最后又加了四个空字节.(java对象结构不清楚自行百度),所以java中new一个空对象总共占16个字节.

    展开全文
  • JOL全称为Java Object Layout,是用来分析JVM中对象布局的工具,它可以帮我们在运行时计算某个对象的大小。 引入JOL依赖,如下: <!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core --> <...

    JOL全称为Java Object Layout,是用来分析JVM中对象布局的工具,它可以帮我们在运行时计算某个对象的大小。

    引入JOL依赖,如下:

    <!-- https://mvnrepository.com/artifact/org.openjdk.jol/jol-core -->
    <dependency>
        <groupId>org.openjdk.jol</groupId>
        <artifactId>jol-core</artifactId>
        <version>0.12</version>
    </dependency>
    

    测试代码demo:

    public static void main(String[] args) {
        byte[] bytes = new byte[1000];
        System.out.println(ClassLayout.parseInstance(bytes).toPrintable());
    }
    

    运行结果如下:
    在这里插入图片描述

    可以很直观的看到java对象的大小是多少。

    展开全文
  • 本文是郑雨迪老师的“深入拆解 Java ...可以查阅java虚拟机中对象的内存分布,是OpenJDK的Code Tools项目中的工具,具体可以通过如下两套指令来实现: $ java -jar /path/to/jol-cli-0.9-full.jar internals j...

    本文是郑雨迪老师的“深入拆解 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
    

     

    展开全文
  • 1、maven 依赖 <dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> <version>...import org.openjdk.jol.info.ClassLayout.

    1、maven 依赖

    <dependency>
    	<groupId>org.openjdk.jol</groupId>
    	<artifactId>jol-core</artifactId>
    	<version>0.10</version>
    </dependency>

     

    2、测试类

    package constxiong;
    
    import org.openjdk.jol.info.ClassLayout;
    import org.openjdk.jol.info.GraphLayout;
    
    /**
     * 测试对象占用的内存信息
     */
    public class TestObjectSize {
    
        public static void main(String[] args) {
            System.out.println("对象内部信息");
            System.out.println(ClassLayout.parseInstance(new String("ConstXiong")).toPrintable());
            System.out.println(ClassLayout.parseInstance(new char[]{'C', 'o', 'n', 's', 't', 'X', 'i', 'o', 'n', 'g'}).toPrintable());
    
            System.out.println("对象整体信息");
            System.out.println(GraphLayout.parseInstance(new String("ConstXiong")).toPrintable());
            System.out.println(GraphLayout.parseInstance(new char[]{'C', 'o', 'n', 's', 't', 'X', 'i', 'o', 'n', 'g'}).toPrintable());
    
            System.out.println("对象占用总空间");
            System.out.println(GraphLayout.parseInstance(new String("ConstXiong")).totalSize());
            System.out.println(GraphLayout.parseInstance(new char[]{'C', 'o', 'n', 's', 't', 'X', 'i', 'o', 'n', 'g'}).totalSize());
        }
    }
    

     

    3、打印结果

    对象内部信息
    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 3e 17 (11010000 00010110 00111110 00010111) (389945040)
         12     4   char[] String.value                              [C, o, n, s, t, X, i, o, n, g]
         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
    
    [C 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)                           08 02 3e 17 (00001000 00000010 00111110 00010111) (389939720)
         12     4        (object header)                           0a 00 00 00 (00001010 00000000 00000000 00000000) (10)
         16    20   char [C.<elements>                             N/A
         36     4        (loss due to the next object alignment)
    Instance size: 40 bytes
    Space losses: 0 bytes internal + 4 bytes external = 4 bytes total
    
    
    对象整体信息
    java.lang.String@1a93a7cad object externals:
              ADDRESS       SIZE TYPE             PATH                           VALUE
             d6284f40         40 [C               .value                         [C, o, n, s, t, X, i, o, n, g]
             d6284f68   11751072 (something else) (somewhere else)               (something else)
             d6db9e08         24 java.lang.String                                (object)
    
    
    [C@3b764bced object externals:
              ADDRESS       SIZE TYPE PATH                           VALUE
             d6df9610         40 [C                                  [C, o, n, s, t, X, i, o, n, g]
    
    
    
    对象占用总空间
    64
    40

     

    内存分析:

    https://www.javanav.com/val/49e9ce5857744a39a88b1977d84086c7.html

     

     


    【Java学习资源】整理推荐

     

     


    【Java面试题与答案】整理推荐

     

    展开全文
  • jol使用

    2020-07-11 19:29:07
    使用方式 本人jar包管理工具是gradle compile group: 'org.openjdk.jol', name: 'jol-core', version: '0.9' 些测试案例 public static void main(String[] args) { Object o = new Object(); System.out....
  • Java openjdk 提供jol 工具,可以查看class的头信息 下载 jol 工具包 https://repo.maven.apache.org/maven2/org/openjdk/jol/jol-cli/ 选择一个版本,进去后下载 jol-cli-*.*-full.jar 一定要下载full 的jar ...
  • https://www.jianshu.com/p/6d62c3ee48d0
  • JOL全称为Java Object Layout,是分析JVM中对象布局的工具,该工具大量使用了Unsafe、JVMTI来解码布局情况,所以分析结果是比较精准的。通常分析java对象的大小需要人工按照Java基础数据类型大小及内容大小估算出...
  • https://www.cnblogs.com/LemonFive/p/11246086.html
  • Java Object Layout: Core 这是一个可以查看JVM创建对象所占用内存空间的jar <!-- ...org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> ..
  • 看过之前一个文章,https://mp.weixin.qq.com/s/XLCHuMiQ1nFWxLN9urx3aAnew一个对象占用了多少字节中提到了一个查看对象大小工具jol,那么就今天就拿来玩玩。 JOL是用来查看对象内存分布及对象内不同部分的占用字节...
  • OpenJDK,提供了JOL包,可以帮我们在运行时计算某个对象的大小,是非常好的工具 官网:http://openjdk.java.net/projects/code-tools/jol/ 定位:分析对象在JVM的大小和分布 依赖: <dependenc..
  • OpenJDK,提供了JOL包,可以帮我们在运行时计算某个对象的大小,是非常好的工具 官网:http://openjdk.java.net/projects/code-tools/jol/ 定位:分析对象在JVM的大小和分布 依赖: <dependency>.
  • 在前面的文章《一探究竟-如何从synchronized理解Java对象头中的锁》中,我们介绍并体验了JOL工具。虽然JOL很赞,但它的输出对我们不是很友好,如果不借助工具,我们很难直观理解其中的含义。 下面这段代码是对JOL...
  • 查看java对象内部构造
  • 压缩命令 在jvm参数中加入 -XX:+UseCompressedOops 验证 使用Oracle提供的实时内存布局工具jol进行查看实时内存,jol工具有两种使用方式,第一种是项目引入jar包,调用api打印。第二种是通过命令行查看。 项目调用...
  • 目录 安装Homebrew 下载JOL源码 安装Maven Eclipse中配置maven Eclipse中创建maven项目 ...最近想利用Eclipse查看Java对象的布局情况,于是在MAC上下载了...根据官网openjdk-jol使用手册,主要有如下几步: ...
  • 并发系列—JOL

    2020-03-23 19:03:32
    JOL:Java Object Layout,是一款工具,可以帮助我们查看对象的内存信息。 1、pom.xml <dependency> <groupId>org.openjdk.jol</groupId> <artifactId>jol-core</artifactId> &...
  • 文章目录Unsafe.CAS、jol查看偏移量AQS#compareAndSetState测试偏移量offsetjol测试输出 Unsafe.CAS、jol查看偏移量 AQS#compareAndSetState #AbstractQueuedSynchronizer protected final boolean ...
  • jol-cli-0.9-full.jar

    2018-08-14 14:29:05
    可以查阅java虚拟机中对象的内存分布,是OpenJDK的Code Tools项目中的工具,具体可以通过如下两套指令来实现: $ java -jar /path/to/jol-cli-0.9-full.jar internals java.util.HashMap $ java -jar /path/to/jol-...
  • jol-core-0.9.jar

    2020-04-14 10:37:15
    openjdk的一个工具,他可以很方便的让我看到一个对象的布局
  • CAS、JOL、锁

    2020-09-13 20:53:05
    这个是针对多CPU JOL = Java Object Layout 对象内存布局(工具) markword 8个字节 class pointer instance data padding markword:锁定某个对象,修改markword markword记录了哪些信息: 锁信息,gc信息和 ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 674
精华内容 269
关键字:

JOL工具使用