How many bytes of memory does a BigInteger object use in general ?
-
9How 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
-
2read 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 Answers
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;

- 21,129
- 10
- 63
- 81
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.

- 1
- 1

- 43,651
- 22
- 107
- 170
-
You also need to include two object headers -- at least 8 bytes each, likely 16. – Hot Licks Mar 08 '13 at 19:21
-
@HotLicks: You are correct, I've added both the header and the padding. Although, come to think of it: could you explain me how you get to two headers instead of just one? – Jeroen Vannevel Mar 08 '13 at 19:26
-
You get two headers because the array is necessarily a separate object (except in the "classic JVM" of IBM iSeries). – Hot Licks Mar 08 '13 at 20:08
-
Are you referring to the `int[]` array? I believe I already covered that with the 24 bytes allocated to it. – Jeroen Vannevel Mar 08 '13 at 21:45
-
OK -- It's not obvious you're accounting for it... you have to assume. – Hot Licks Mar 08 '13 at 22:00
-
-
That's true, nor have I accounted for the references to the other ints – Jeroen Vannevel Mar 08 '13 at 23:36
-
1You don't need references for the int fields. But you do need a reference for the array, since it's a separate object. – Hot Licks Mar 09 '13 at 03:46
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!

- 725
- 9
- 13
-
@BertKing as mentionned I've used VisualVM (but there are plenty of other tools) to take a heap dump – user1075613 Jul 16 '19 at 15:55
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.

- 133,369
- 30
- 199
- 275