java bigdecimal 存储,Java BigDecimal内存使用情况?

本文提供了估算BigDecimal内存占用的详细指南,包括字段解析和内存计算。重点介绍了BigDecimal对象结构(如long、int、BigInteger等)及其内存消耗,并给出一般性的内存计算公式:36+Ceiling(log2(n)/8.0)字节。
摘要由CSDN通过智能技术生成

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.

解决方案

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 +?

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.)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值