借助org.apache.lucene工具类

当一个对象有多个属性,需要计算整个对象的大小时,可以借助org.apache.lucene工具类

Maven依赖

		<dependency>
            <groupId>org.apache.lucene</groupId>
            <artifactId>lucene-core</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
        </dependency>

测试代码:

		Map<String, String> map = new HashMap<>();
        System.out.println("map init value is " + RamUsageEstimator.sizeOf(map));
        for (int i = 0; i < 100; i++) {
            RandomStringUtils.randomAlphanumeric(100);
            map.put(RandomStringUtils.randomAlphanumeric(10), RandomStringUtils.randomAlphanumeric(10));
        }
        System.out.println("map size 100, value is " + RamUsageEstimator.sizeOf(map));
        System.out.println("map size 100, value is " + RamUsageEstimator.humanSizeOf(map));
Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐