I wouldn't worry about it, you would need millions of Objects for this to really make a difference on a server.
But if you really want to know, you can look at the implementations to see the fields of each object, and the fields of each parent object etc and the fields of each referenced object). You can add up the number bytes used by each field + the size of an object reference (4 or 8 bytes depending on JVM) plus padding described in the Java spec.
Or you can use 3rd party libraries such as java.sizeOf which will do this for you.
But taking a quick look at the code of the classes you asked about, here are the fields:
Date:
private transient long fastTime;
/*
* If cdate is null, then fastTime indicates the time in millis.
* If cdate.isNormalized() is true, then fastTime and cdate are in
* synch. Otherwise, fastTime is ignored, and cdate indicates the
* time.
*/
private transient BaseCalendar.Date cdate;
long
+ reference to other class which has its own fields etc. However, after lots of code reading, cdate
is almost alwasy null
so we only have to count the size of the object reference (to null) which I think won't bloat up the size of the object beyond the padding of 24 bytes when using a 32-bit or if on 64 bit, using compressed Ops which tries to use 4 byte references instead of 8 when possible. (I think this is default on Java 8) so we can ignore it.
LocalDate:
/**
* The year.
*/
private final int year;
/**
* The month-of-year.
*/
private final short month;
/**
* The day-of-month.
*/
private final short day;
So int+short+short
takes the same size as long
but you have to account for the memory of each LocalDate
reference you keep around.
Datetime:
private long iMillis;
private Chronology iChronology;
Which is also long
+ object ref to other object that takes up memory.
Using SizeOf
When I created an instance of each object and used java.sizeOf on them on my 64bit Java 8 JVM I got the following memory sizes (in bytes):
javaDate = 24
localDate = 24
dateTime = 16928
As you can see jodatime takes up a lot of memory with all of its supporting classes. Much of it will be reused when you have other instances though.
Therefore, I would go with keep using your long
s if they aren't wrapped in a class. But if you are wrapping your long
in a class, or even using Long
, LocalDate
seems best since it's the same amount of memory plus all the supporting classes for converting/ timezones etc. I would not use Date
since that class is mutable and should be considered deprecated.