Java 如何计算给定BigDecimal值占用的内存?

Java 如何计算给定BigDecimal值占用的内存?,java,bigdecimal,Java,Bigdecimal,如何计算给定BigDecimal值占用的内存?例如,0.4247246522 对于整数10,二进制等价物为1010,因此它占用4位内存 对于BigDecimal对象,它是如何实现的?我正在做以下实验: public class ObjectSizeFetcher { private static Instrumentation instrumentation; public static void premain(String args, Instrumentation in

如何计算给定BigDecimal值占用的内存?例如,0.4247246522

对于整数10,二进制等价物为1010,因此它占用4位内存


对于BigDecimal对象,它是如何实现的?

我正在做以下实验:

public class ObjectSizeFetcher {

    private static Instrumentation instrumentation;

    public static void premain(String args, Instrumentation inst) {
        instrumentation = inst;
    }

    public static long getObjectSize(Object o) {
        return instrumentation.getObjectSize(o);
    }
}
通过此测试:

public class TestDoubleSize {

    public static void main(String args[]) {

        String num = "0.";
        for (int i = 1; i < 500000; i++) {
            num += (int) (Math.random()*10) ;
            BigDecimal bg = new BigDecimal(num);

            if (i % 1000 == 0) {
                System.out.println(i + "\t" + bg.precision() + "\t" + ObjectSizeFetcher.getObjectSize(bg));
            }
        }
    }
}
这很奇怪,因为它卡在32

这让我相信我的代码中可能有一个bug…

也许可以给你一个想法?
Digits   Precision  Bytes 
1000    1000    32
2000    2000    32
3000    3000    32
4000    4000    32
5000    5000    32
6000    6000    32
7000    7000    32
8000    8000    32
9000    9000    32
10000   10000   32
11000   11000   32
12000   12000   32
13000   13000   32
14000   14000   32
15000   15000   32
16000   16000   32
17000   17000   32
18000   18000   32
19000   19000   32
20000   20000   32
21000   21000   32
22000   22000   32
23000   23000   32
24000   24000   32
25000   25000   32
26000   26000   32
27000   27000   32
28000   28000   32
29000   29000   32
30000   30000   32