首页 > 解决方案 > Java中类的大小

问题描述

我已经阅读了这篇文章http://btoddb-java-sizing.blogspot.com/
作者解释了如何在 Java 中计算对象大小。例如:

import jdk.nashorn.internal.ir.debug.ObjectSizeCalculator;

public final class Perfomance {

    static int number;

    static int anotherNumber;

    public static void main(String[] args) {
        System.out.println(ObjectSizeCalculator.getObjectSize(new Integer(2)));
    }
}

此代码将显示 16,因为新对象占用 12 个字节,而在 Integer 内部我们有 int(4 bytes)12+4 = 16.

但是考虑一下这段代码:

System.out.println(ObjectSizeCalculator.getObjectSize(Integer.class));

现在它的尺寸为 56 ,而且

 System.out.println(ObjectSizeCalculator.getObjectSize(Object.class));

它的节目288。它是如何工作的。分配的内存如何取决于类?

标签: javamemory

解决方案


推荐阅读