Java'Date'对象大小 [英] Java 'Date' object size

查看:113
本文介绍了Java'Date'对象大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何有效地确定我的内存中Date对象的大小?

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

最初我经历了这个链接,其中涉及日期对象的 9个字节

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

当我发现这个链接时,我试图找到它,它在内存中谈到日期对象的 32字节!!!!

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

内存中的日期对象大小

请帮助。

在这些行上思考的原因:
我正在加载数百万个对象的某一类进入内存,进行一些计算。该类中的一个变量是Date对象。我可以将值保存为一个很长的时间,但这将需要在代码中的小怪癖。我正在考虑将内存占用尽可能小的价值。为了做到这一点,我需要知道每种情况下的内存要求,以进行相同的调用。

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.

推荐答案

最简单的方式回答这个问题是看源代码 java.util.Date

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

它只有2非静态字段(Java 1.7.0_55):

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

private transient long fastTime;
private transient BaseCalendar.Date cdate;

long 内存大小为8字节和 cdate 是一个大小为4字节的对象引用。因此,总共 12个字节

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.

如果 cdate 将被实例化,它可以在内存中需要额外的字节,但是如果你也看这些构造函数,有时甚至不会被触动,而在其他的情况下,它将是 null 的构造函数,所以最终结果也是 12个字节

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.

这只是为了创建一个 Date 。如果您在 Date (例如 Date.toString())中调用方法,那么创建并将对象存储到不会被清除的 cdate 字段中。因此,如果您在 Date 上调用某些方法,则其内存使用量将增加。

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.

注意: / strong>对象引用在64位JVM上可能是64位长的,在这种情况下,内存使用量将为16字节。

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

注意#2:另请注意,这只是内存使用的 Date 对象本身。很有可能您将其参考存储在某个地方,例如在一个数组或列表或一些其他类中的字段,这将需要额外的4个字节(或64位JVM上的8个字节)。

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

这篇关于Java'Date'对象大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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