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

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

问题描述

是否有估算 BigDecimal 消耗的内存量的准则?

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

寻找与这些类似的内容估算字符串内存使用情况的准则

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的注释是用于存储规范字符串表示,如果计算的话。,所以假设如果不调用toString,我们将其保留为零字节,因此总共有(8 + 4 + 4)= 16字节+ BigIncger中的BigInteger和4 + 4 + 4 + 4 + 4 = 20字节+ mag BigInteger的。所以共有36个字节加上幅度。据我所知,幅度始终是表示完整整数所需的最小位数,因此对于数字n,它将需要log2(n)位,可以将其转换为整数。所以一般来说你应该使用about:

The comment for stringCache is "Used to store the canonical string representation, if computed.", so assuming that you don't call toString we will leave that as zero bytes, so in total there are (8+4+4)=16 bytes + BigInteger in BigDecimal and 4+4+4+4+4=20 bytes + mag for BigInteger. So a total of 36 bytes plus the magnitude. As far as I can tell magnitude is always the minimum number of bits necessary to represent the full integer, so for a number n it will need log2(n) bits, which can be converted to ints. So in general 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天全站免登陆