当一个对象有多个属性,需要计算整个对象的大小时,可以借助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>
测试代码
public void test() {
List<User> userList = new ArrayList<>();
User xh = new User();
xh.setName("xiaohong");
xh.setAge(2);
for (int i = 0; i < 1000; i++) {
userList.add(xh);
}
System.out.println("value is:" + RamUsageEstimator.humanSizeOf(userList));
}
@Data
class User {
private String name;
private int age;
}
执行结果展示
value is:4.9 KB
标签:xh,Java,对象,lucene,userList,内存,apache,org,User From: https://www.cnblogs.com/qingxuan0316/p/16830454.html