java date cdate fasttime,Java'Date'对象大小

How to effectively determine the size of a Date object in my memory?

Initially I went through this link which talks about 9 bytes for a date object..

I was trying to find it out when I found this link, where it talks about 32 bytes!!!! for a date object in memory.

Kindly help.

Reason for thinking on these lines:

I am loading millions of objects of a certain class into memory, for some computation. One of the variables in that class is a Date object. I can store the value as a long, but that would require minor quirks in the code. I am thinking on keeping the memory footprint to the least value possible. To do that, I need to know the exact memory requirements in each case to take a call on the same.

解决方案

Easiest way to answer this question is to look at the source code of java.util.Date.

It has only 2 non-static fields (Java 1.7.0_55):

private transient long fastTime;

private transient BaseCalendar.Date cdate;

long has a memory size of 8 bytes and cdate is an object reference which has a size of 4 bytes. So a total of 12 bytes.

If cdate would be instantiated, it could require additional bytes in the memory, but if you look at the constructors too, sometimes it won't even be touched, and in others it will be null-ed at the end of the constructor, so the final result is also 12 bytes.

This is just for creating a Date. If you call methods on the Date (for example Date.toString()), that will create and store an object into the cdate field which will not be cleared. So if you call certain methods on the Date, its memory usage will increase.

Note: Object references might be 64 bit long on 64-bit JVMs in which case memory usage would be 16 bytes.

Note #2: Also note that this is just the memory usage of the Date object itself. Most likely you will store its reference somewhere, e.g. in an array or list or a field in some other class which will require additional 4 bytes (or maybe 8 bytes on 64 bit JVMs).

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值