java BigInteger 使用了多少空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15301249/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How much space does BigInteger use?
提问by Nikunj Banka
How many bytes of memory does a BigInteger object use in general ?
BigInteger 对象一般使用多少字节的内存?
采纳答案by poitroae
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.
BigInteger 内部使用 anint[]
来表示您使用的巨大数字。因此,它实际上取决于您存储在其中的数字的大小。int[]
如果当前数字不适合动态,则将增长。
To get the number of bytes your BigInteger
instance currentlyuses, you can make use of the Instrumentation
interface, especially getObjectSize(Object)
.
要获取您的BigInteger
实例当前使用的字节数,您可以使用该Instrumentation
接口,尤其是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;
回答by Jeroen Vannevel
Following thispost:
按照这个帖子:
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).
总共 20 个字节 + 整数数组。长度为 N 的整数数组的大小为 4N + 24(数组开销 + 4 个字节/整数)。
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.
总共有 4N + 44 个字节,具体取决于您的数字有多大。不要忘记对对象的引用也使用内存。
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.
编辑:16 个额外字节作为对象开销,使其达到 4N + 60 个字节。向其添加填充(每个对象使用 8 字节的倍数),我们会得到额外的 4 个字节。
This results in 4N + 64bytes.
这导致4N + 64个字节。
回答by user1075613
Using VisualVM retained-size on a JVM 64-bit, here are some concrete numbers :
在 64 位 JVM 上使用 VisualVM 保留大小,以下是一些具体数字:
- BigInteger 1digit = 70B(ex: new BigInteger("9"))
- BigInteger 20digits = 80B(ex: new BigInteger("12345678901234567890"))
- BigInteger 100digits = 112B
- BigInteger 1位 = 70B(例如:new BigInteger("9"))
- BigInteger 20位 = 80B(例如:new BigInteger("12345678901234567890"))
- BigInteger 100位 = 112B
For comparison :
比较:
- Long(wrapper class) = 24B(but Long is limited to 18-19 digits)
- long(primitive) = 8B
- Long(包装类)= 24B(但Long限制为18-19位)
- 长(原始)= 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!
所以BigInteger
至少比 aLong
重3 倍,比 a重 10 倍long
。所以只有在你真正需要它的时候才使用 BigInteger!
回答by Evgeniy Dorofeev
Java object size depends on its fields. These are BigInteger fields
Java 对象大小取决于其字段。这些是 BigInteger 字段
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
我们可以计算 BigInteger 实例的大小为
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.
参见http://www.javamex.com/tutorials/memory/object_memory_usage.shtml。
回答by Amandeep Jiddewar
It can represent integer of any size. Allocated memory to your application is Limit.
它可以表示任意大小的整数。分配给您的应用程序的内存为限制。