BigInteger 对象通常使用多少字节的内存?
最佳答案
BigInteger 在内部使用 int[]
来表示您使用的大数字。
因此它确实取决于您存储在其中的数字的大小。如果当前数字不适合动态,则 int[]
将增长。
要获取您的BigInteger
实例当前 使用的字节数,您可以使用Instrumentation
接口(interface),尤其是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);
}
}
为了说服自己,请查看 source code ,它说:
/**
* 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;
关于java - BigInteger 使用多少空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15301249/