栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java中jol-core依赖分析对象内存模型

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java中jol-core依赖分析对象内存模型

1、引用jol-core依赖

jol-core依赖版本的不同,下面代码分析的结果样式是不同的,下面再展示不同版本的结果


            org.openjdk.jol
            jol-core
            0.16

2、代码分析
import org.openjdk.jol.info.ClassLayout;
import org.openjdk.jol.info.GraphLayout;
import org.openjdk.jol.vm.VM;

public class test {
    public static void main(String[] args) {
        String s = "-1111111111111111111111111011011011110110100001001010001011110111";
//        String s = "- 11111111【】    11111111【】    11111111【】   10110110【b6】    11110110【f6】   10000100【84】   10100010【a2】     11110111【f7】";
        String b1 = "11110111";
        System.out.println("s的长度是="+s.length());
        System.out.println("=============================");
        Person person = new Person();
//        int hashCode = person.hashCode();
        System.out.println(VM.current().details());
        System.out.println("============================");
//        String toHexString = Integer.toHexString(person.hashCode());   136432db
        System.out.println("10进制》》System.identityHashCode(person)="+System.identityHashCode(person));
        System.out.println("16进制》》System.identityHashCode(person)="+Integer.toHexString(System.identityHashCode(person)));
        System.out.println("hashcode转成16进制的string=" +Integer.toHexString(person.hashCode()));
        System.out.println("=============================================");
        System.out.println(GraphLayout.parseInstance(person).totalSize());
        System.out.println("手动触发一次gc");
        System.gc();
        System.out.println("===========================================");
        print(person);

    }

    static void print(Person p){
        System.out.println("-----------------------------------------");
        System.err.println(ClassLayout.parseInstance(p).toPrintable());
    }
    
    private static class Person {
        private Integer age;
        private String name;
        private String address;
    }
}
3、当version是0.16时,结果如下
s的长度是=65
=============================
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass 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]

============================
10进制》》System.identityHashCode(person)=325333723
16进制》》System.identityHashCode(person)=136432db
hashcode转成16进制的string=136432db
=============================================
24
手动触发一次gc
===========================================
-----------------------------------------
com.xxx.test$Person object internals:
OFF  SZ                TYPE DEscriptION               VALUE
  0   8                     (object header: mark)     0x000000136432db09 (hash: 0x136432db; age: 1)
  8   4                     (object header: class)    0xf800c143
 12   4   java.lang.Integer Person.age                null
 16   4    java.lang.String Person.name               null
 20   4    java.lang.String Person.address            null
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total


Process finished with exit code 0
4、当version是0.09时,结果如下
s的长度是=65
=============================
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass 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]

============================
10进制》》System.identityHashCode(person)=325333723
16进制》》System.identityHashCode(person)=136432db
hashcode转成16进制的string=136432db
=============================================
24
手动触发一次gc
===========================================
-----------------------------------------
com.xxx.test$Person object internals:
 OFFSET  SIZE                TYPE DEscriptION                               VALUE
      0     4                     (object header)                           09 db 32 64 (00001001 11011011 00110010 01100100) (1681054473)
      4     4                     (object header)                           13 00 00 00 (00010011 00000000 00000000 00000000) (19)
      8     4                     (object header)                           43 c1 00 f8 (01000011 11000001 00000000 11111000) (-134168253)
     12     4   java.lang.Integer Person.age                                null
     16     4    java.lang.String Person.name                               null
     20     4    java.lang.String Person.address                            null
Instance size: 24 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total


Process finished with exit code 0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/697271.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号