java字节数计算,计算Java对象的字节大小

I am working on calculaitng the size [memory used] of a java object [hashmap] . It contains elements of different data types [at runtime] so [ no-of-elem * size-of-element] is not that good an approach. The code right now does it by series of

if (x)

do something

else if (primitives)

lookup size and calculate

However this process is a CPU hog and in-efficient.

I am thinking of following 2 approaches instead:

Serialize the object to a buffer and get the size.

Look into java.lang.instrument to get the size

I am looking for anyones experience with these approaches for performance , efficiency, scaling etc OR if you know any better way.

P.S:

This is a background utility that I am building so the size need no be super accurate though it should be about correct. So I am willing to trade accuracy for performance

I am not interested in the deep-size [the size of objects that are refered by this object will not be computed.]

I am looking for a performance comparisons and understanding how getObjectSize() works internally ..so that I do not messup something else to improve the performance

Thanks

解决方案

Use getObjectSize() method of the Instrumentation package.

Look here for implementation details:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值