Java BigDecimal 内存使用情况? [英] Java BigDecimal memory usage?

查看:78
本文介绍了Java BigDecimal 内存使用情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于估计 BigDecimal 消耗的内存量的指南?

Is there a guideline for estimating the amount of memory consumed by a BigDecimal?

寻找类似于这些指南的东西,用于估计String 内存使用情况.

Looking for something similar to these guidelines for estimating String memory usage.

推荐答案

如果您查看 BigDecimal 源代码中的字段,则有:

If you look at the fields in the source for BigDecimal there is:

BigDecimal:
  long intCompact +8 bytes
  int precision +4 bytes
  int scale +4 bytes
  String stringCache +?
  BigInteger intVal +?

BigInteger:
  int bitCount +4 bytes
  int bitLength +4 bytes
  int firstNonzeroIntNum +4 bytes
  int lowestSetBit +4 bytes
  int signum +4 bytes
  int[] mag +?

stringCache 的注释说

用于存储规范字符串表示(如果已计算).

Used to store the canonical string representation, if computed.

假设您不调用 .toString(),它将保持零字节.因此 BigDecimal 是 (8+4+4)=16 bytes + BigInteger.

Assuming you don't call .toString(), it will remain zero bytes. Hence BigDecimal is (8+4+4)=16 bytes + BigInteger.

BigInteger 本身就是 4+4+4+4+4=20 bytes + mag.

BigInteger itself is 4+4+4+4+4=20 bytes + mag.

20+16 总共有 36 个字节加上大小,这始终是表示完整整数所需的最小位数.对于数字 n 它将需要 log2(n) 位,可以转换为整数.您应该使用 about:

20+16 gives total of 36 bytes plus the magnitude, which is always the minimum number of bits necessary to represent the full integer. For a number n it will need log2(n) bits, which can be converted to ints. You should be using about:

36 + Ceiling(log2(n)/8.0) bytes

(请注意,这不包括任何其他对象描述符开销,如您的字符串示例链接所做的那样,但它应该为您提供一个很好的总体思路.)

(note this doesn't include any of the other object descriptor overhead as your example link for strings does, but it should give you a good general idea.)

这篇关于Java BigDecimal 内存使用情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆