java - BigInteger 使用多少空间?

标签 java biginteger

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/

相关文章:

java - BigInteger 会溢出吗?

java - Scala - 将大数或科学数转换为 Long

java - Seam 3 教程,Spring 3 + Seam 3 集成

java - 提高 Java 的 BigInteger 性能

elisp - emacs/elisp 中的 bignum

java - 如何使用 iText 读取 PDF 文件作为模板

java - BigInteger CompareTo 与 minus + signnum()

java - 如何使用 Java Reflection 将协议(protocol)字符串转换为 Spymemcached 协议(protocol)枚举?

java - 使用 100 - 1000 GB 内存测试 Java 库

java - 使用 Hibernate 加载 20K 产品,修改实体并更新到数据库