1

How do I calculate the memory occupied by a given BigDecimal value? (For example, 0.4247246522.)

For an integer 10, the binary equivalent is 1010, and hence it occupies 4 bits of memory.

How is it done in case of a BigDecimal object?

Cat
  • 66,919
  • 24
  • 133
  • 141
  • Perhaps [this question](http://stackoverflow.com/q/52353/1438733) can give you an idea? – Cat Feb 16 '13 at 16:52

1 Answers1

0

I am doing the following experiment:

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);
    }
}

With this test:

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));
            }
        }
    }
}

Results so far:

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

It is strange because it is stuck at 32.

This makes me believe I probably have a bug in my code...

user000001
  • 32,226
  • 12
  • 81
  • 108