18

How many bytes of memory does a BigInteger object use in general ?

finnw
  • 47,861
  • 24
  • 143
  • 221
Nikunj Banka
  • 11,117
  • 16
  • 74
  • 112
  • 9
    How many bytes of memory does an array object use in general? ;-) –  Mar 08 '13 at 18:57
  • here ya go: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/math/BigInteger.java – jtahlborn Mar 08 '13 at 18:58
  • 2
    read this: http://stackoverflow.com/questions/9368764/calculate-size-of-object-in-java – DwB Mar 08 '13 at 18:59
  • At least two minimum-sized objects. I think the minimum object size on most platforms is 32 bytes. – Hot Licks Mar 08 '13 at 19:19

4 Answers4

17

BigInteger internally uses an int[] to represent the huge numbers you use. Thus it really depends on the size of the number you store in it. The int[] will grow if the current number doesn't fit in dynamically.

To get the number of bytes your BigInteger instance currently uses, you can make use of the Instrumentation interface, especially getObjectSize(Object).

import java.lang.instrument.Instrumentation;

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

To convince yourself, take a look at the source code, where it says:

/**
 * The magnitude of this BigInteger, in <i>big-endian</i> order: the
 * zeroth element of this array is the most-significant int of the
 * magnitude.  The magnitude must be "minimal" in that the most-significant
 * int ({@code mag[0]}) must be non-zero.  This is necessary to
 * ensure that there is exactly one representation for each BigInteger
 * value.  Note that this implies that the BigInteger zero has a
 * zero-length mag array.
 */
final int[] mag;
poitroae
  • 21,129
  • 10
  • 63
  • 81
9

Following this post:

BigInteger:
  int bitCount +4 bytes
  int bitLength +4 bytes
  int firstNonzeroIntNum +4 bytes
  int lowestSetBit +4 bytes
  int signum +4 bytes
  int[] mag +?

That's a total of 20 bytes + the integer array. An integer array of length N has size 4N + 24 (Array overhead + 4 bytes/integer).

In total this makes 4N + 44 bytes, depending on how big your number is. Don't forget the reference to an object also uses memory.

Edit: 16 additional bytes as object overhead, brings it to 4N + 60 bytes. Adding padding to this (each object uses a multiple of 8 bytes) we get an additional 4 bytes.

This results in 4N + 64 bytes.

Community
  • 1
  • 1
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
3

Using VisualVM retained-size on a JVM 64-bit, here are some concrete numbers :

  • BigInteger 1 digit = 70B (ex: new BigInteger("9"))
  • BigInteger 20 digits = 80B (ex: new BigInteger("12345678901234567890"))
  • BigInteger 100 digits = 112B

For comparison :

  • Long (wrapper class) = 24B (but Long is limited to 18-19 digits)
  • long (primitive) = 8B

So BigInteger is at least 3x heavier than a Long and 10x heavier than a long. So use BigInteger only when you really need it!

user1075613
  • 725
  • 9
  • 13
1

Java object size depends on its fields. These are BigInteger fields

final int signum;
final int[] mag;
private int bitCount;
private int bitLength;
private int lowestSetBit;
private int firstNonzeroIntNum;

we can calculate the size of a BigInteger instance as

8 + 4 + (12 + mag.length * 4) + 4 + 4 + 4 + 4  ~= 40 + mag.length * 4 

see http://www.javamex.com/tutorials/memory/object_memory_usage.shtml.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275