运行时查看Java对象占用内存
Maven依赖
<dependency>
<groupId>com.carrotsearch</groupId>
<artifactId>java-sizeof</artifactId>
<version>0.0.5</version>
</dependency>
用法
public static void main(String[] args) {
int length = 10000000;
List<Double> a = new ArrayList<>();
double[] b = new double[length];
for (int i = 0; i < length; i++) {
double v = new Random().nextDouble();
a.add(v);
b[i] = v;
}
System.out.println(RamUsageEstimator.sizeOf(a));
System.out.println(RamUsageEstimator.sizeOf(b));
}
# output:
# 295380640
# 80000016
本文标题:运行时查看Java对象占用内存
本文链接:https://www.haomeiwen.com/subject/dhxftdtx.html
网友评论