java.util.Date 对象使用多少字节的内存?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4681960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How many bytes of memory does a java.util.Date object use?
提问by Michael McGowan
I need to store a large amount of dates (potentially large enough that the amount of heap space used is a concern so please, no lectures on premature optimization), and I'm wondering if it makes sense to use some sort of primitive representation instead of java.util.Date (or some other existing Date class). I know I could do some profiling to try it out, but does anyone know off-hand exactly how many bytes of memory a single Date object uses?
我需要存储大量日期(可能足够大以至于使用的堆空间量是一个问题,所以请不要讲授过早优化的课程),我想知道使用某种原始表示代替是否有意义java.util.Date (或其他一些现有的 Date 类)。我知道我可以做一些分析来尝试一下,但是有没有人知道单个 Date 对象使用多少字节的内存?
采纳答案by Dunes
My gut reaction was that the memory overhead for Date would be very small. Examining the source code it seems that the class only contains one instance field (a long called milliseconds). Which means the size of a date object is the size of a long plus the size of an instance of Object -- that is, very small.
我的直觉反应是 Date 的内存开销会非常小。检查源代码似乎该类只包含一个实例字段(称为毫秒的长)。这意味着日期对象的大小是 long 的大小加上 Object 实例的大小——也就是说,非常小。
I then found this codethat creates thousands of objects to determine the size of the object. It says that the size of java.util.Date
is 32 bytes. Compare that with just storing a the date as a long (which is what it does internally) -- a long is 8 bytes, so you have to pay four fold for the convenience of having a date object.
然后我找到了这段代码,它创建了数千个对象来确定对象的大小。它说的大小java.util.Date
是 32 个字节。与仅将日期存储为 long(这是它在内部所做的)相比,long 是 8 个字节,因此您必须为拥有日期对象的便利支付四倍的费用。
However, the overhead of creating of objects isn't very high. So if you're really that worried about space then store the dates as longs and create a Date object as and when needed.
但是,创建对象的开销并不是很高。因此,如果您真的很担心空间,那么将日期存储为 long 并在需要时创建一个 Date 对象。
回答by rapadura
Use the primitive long ?
使用原始 long ?
It is not an object, so less space, and dates can be expressed as a long value. Then convert back and forth between Date and long when you want to store the dates and use less memory.
它不是对象,因此空间较少,并且日期可以表示为 long 值。当您想要存储日期并使用较少内存时,然后在 Date 和 long 之间来回转换。
回答by milan
using java's instrumentationframework, getObjectSizesays it's 24B.
使用 java 的检测框架,getObjectSize说它是 24B。
回答by icza
As answered heretoo:
正如这里所回答的:
Easiest way to answer this question is to look at the source code of java.util.Date
.
回答这个问题最简单的方法是查看java.util.Date
.
It has only 2 non-static fields (Java 1.7.0_55):
它只有 2 个非静态字段 (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.
long
具有 8 个字节的内存大小,并且cdate
是一个大小为 4 个字节的对象引用。所以一共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.
如果cdate
要实例化,它可能需要内存中的额外字节,但是如果您也查看构造函数,有时甚至不会触及它,而在其他情况下,它将null
在构造函数的末尾进行-ed,因此最终结果也是12 个字节。
This is just for creating a Date
. If you call methods on the Date
(for example Date.toString()
), that willcreate 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.
这只是为了创建一个Date
. 如果您在Date
(例如Date.toString()
)上调用方法,这将创建一个对象并将其存储到cdate
不会被清除的字段中。所以如果你在 上调用某些方法Date
,它的内存使用量会增加。
Note:Object references might be 64 bit long on 64-bit JVMs in which case memory usage would be 16 bytes.
注意:对象引用在 64 位 JVM 上可能是 64 位长,在这种情况下,内存使用量为 16 字节。
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).
注意#2:还要注意,这只是Date
对象本身的内存使用情况。很可能您会将其引用存储在某处,例如在数组或列表中或其他一些类中的字段中,这将需要额外的 4 个字节(或者在 64 位 JVM 上可能需要 8 个字节)。
回答by Brian
If it's literally date, rather than date & timestamp, you could even use an int:
如果它实际上是日期,而不是日期和时间戳,您甚至可以使用 int:
20110113
20110113
回答by fmucar
java.util.Date object can be represented by a long value and a long value is 8 bytes -2^63 to (2^63)-1
java.util.Date对象可以用long值表示,long值为8个字节-2^63到(2^63)-1
回答by Sam
I tried a manual calculation based on the rules here: http://www.javamex.com/tutorials/memory/object_memory_usage.shtmland checking the source code of the Date object in Java 7 the memory usage.
我尝试根据以下规则进行手动计算:http: //www.javamex.com/tutorials/memory/object_memory_usage.shtml并检查 Java 7 中 Date 对象的源代码内存使用情况。
Object overhead: 8 bytes => 8 bytes
+ 1 long fastTime: 8 bytes => 16 bytes
+ 1 reference cdate: 4 bytes => 20 bytes
Rounded up to nearest multiple of 8 => 24 bytes
Maybe I'm missing something in the calculation or the tools that were used in other answers that gave a result of 32 were including the references to the dates themselves in the calculation?
也许我在计算中遗漏了一些东西,或者在其他答案中使用的给出 32 结果的工具在计算中包含了对日期本身的引用?