借助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

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐