【使用ObjectSizeCalculator计算对象内存大小】

使用ObjectSizeCalculator计算对象内存大小的总结

简单分析

如果想要简单的获取某对象的大小,不需要进行详细信息,可以直接使用ObjectSizeCalculator中的静态方法:getObjectSize。示例:

public static void main(String[] args)
{
    Map<Object,Object> map = new HashMap<>();
    map.put("AA",11);
    map.put("BB",11L);
    map.put("CC",11D);
    map.put("DD",11F);
    map.put("EE",'A');
    map.put("FF",new Date());
    map.put("GG",(byte)11);

    System.out.println("map size:"+ObjectSizeCalculator.getObjectSize(map));
}

结果是:

map size:824

详细分析

如果想要详细分析对象中占用内存的详细信息需要用到getClassHistogram方法。示例:

public static void main(String[] args)
{
    Map<Object, Object> map = new HashMap<>();
    map.put("AA", 11);
    map.put("BB", 11L);
    map.put("CC", 11D);
    map.put("DD", 11F);
    map.put("EE", 'A');
    map.put("FF", new Date());
    map.put("GG", (byte) 11);
    
    // 初始化时,可以使用自带的内存布局
    ObjectSizeCalculator calculator1 = new ObjectSizeCalculator(ObjectSizeCalculator.getEffectiveMemoryLayoutSpecification());
    System.out.println("Use mem:" + calculator1.calculateObjectSize(map));
    List<ClassHistogramElement> elementList = calculator1.getClassHistogram();
    // 按类占用的大小排序
    elementList.sort((o1, o2) -> (int) (o1.getBytes() - o2.getBytes()));
    for (ClassHistogramElement element : elementList)
    {
        System.out.println("element>>>>" + element.toString());
    }
}

结果是:

Use mem:824
element>>>>ClassHistogramElement[class=java.lang.Integer, instances=1, bytes=16]
element>>>>ClassHistogramElement[class=java.lang.Character, instances=1, bytes=16]
element>>>>ClassHistogramElement[class=java.lang.Byte, instances=1, bytes=16]
element>>>>ClassHistogramElement[class=java.lang.Float, instances=1, bytes=16]
element>>>>ClassHistogramElement[class=java.lang.Double, instances=1, bytes=24]
element>>>>ClassHistogramElement[class=java.util.Date, instances=1, bytes=24]
element>>>>ClassHistogramElement[class=java.lang.Long, instances=1, bytes=24]
element>>>>ClassHistogramElement[class=java.util.HashMap, instances=1, bytes=48]
element>>>>ClassHistogramElement[class=java.util.HashMap.Node[], instances=1, bytes=80]
element>>>>ClassHistogramElement[class=char[], instances=7, bytes=168]
element>>>>ClassHistogramElement[class=java.lang.String, instances=7, bytes=168]
element>>>>ClassHistogramElement[class=java.util.HashMap.Node, instances=7, bytes=224]

可见,ClassHistogramElement中会包含类路径、实例数、占用大小,还是比较详细的。

其中初始化ObjectSizeCalculator时,需要一个实现ObjectSizeCalculator.MemoryLayoutSpecification接口的参数。除了自定义外,可以使用自带的ObjectSizeCalculator.getEffectiveMemoryLayoutSpecification()

然后,我这里使用了递增的一个排序。其实ClassHistogramElement类中是自带三种排序的。但是都使用了绝对值,目前没想到它们的使用场景。下面是自带的比较器:

public static final Comparator<ClassHistogramElement> COMPARE_INSTANCES = new Comparator<ClassHistogramElement>() {
        public int compare(ClassHistogramElement o1, ClassHistogramElement o2) {
            return (int)Math.abs(o1.instances - o2.instances);
        }
    };
    public static final Comparator<ClassHistogramElement> COMPARE_BYTES = new Comparator<ClassHistogramElement>() {
        public int compare(ClassHistogramElement o1, ClassHistogramElement o2) {
            return (int)Math.abs(o1.bytes - o2.bytes);
        }
    };
    public static final Comparator<ClassHistogramElement> COMPARE_CLASSNAMES = new Comparator<ClassHistogramElement>() {
        public int compare(ClassHistogramElement o1, ClassHistogramElement o2) {
            return o1.clazz.getCanonicalName().compareTo(o2.clazz.getCanonicalName());
        }
    };
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

树上灵溪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值