美文网首页
运行时查看Java对象占用内存

运行时查看Java对象占用内存

作者: _JiangXin | 来源:发表于2022-10-28 13:11 被阅读0次

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