查看java对象所占内存大小
查看java对象所占内存大小
·
借助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));
更多推荐
已为社区贡献1条内容
所有评论(0)