poi*_*oae 15
BigInteger在内部使用a int[]来表示您使用的巨大数字.因此,它实际上取决于您存储在其中的数字的大小.int[]如果当前数字不适合动态,则会增长.
要获取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);
}
}
Run Code Online (Sandbox Code Playgroud)
为了说服自己,请看一下源代码,它说:
/**
* 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;
Run Code Online (Sandbox Code Playgroud)
下面这个帖子:
BigInteger:
int bitCount +4 bytes
int bitLength +4 bytes
int firstNonzeroIntNum +4 bytes
int lowestSetBit +4 bytes
int signum +4 bytes
int[] mag +?
Run Code Online (Sandbox Code Playgroud)
这总共是20个字节+整数数组.长度为N的整数数组的大小为4N + 24(数组开销+ 4字节/整数).
总共这会产生4N + 44个字节,具体取决于您的数字有多大.不要忘记对象的引用也使用内存.
编辑:16个额外字节作为对象开销,使其达到4N + 60字节.为此添加填充(每个对象使用8个字节的倍数),我们得到额外的4个字节.
这导致4N + 64字节.