How much memory does an object take up in Java? How many strings, dates etc can actually fit into,
say, a 256MB or 1GB Java heap?
In general, a Java object takes up a small number of bytes of housekeeping information, plus the bytes
for the actual fields on each instance, including references to other objects; objects may also
"waste" some space, because JVMs and/or the processor architecture will typically require object
data to be aligned to some number of bytes (so that e.g. every object occupies a multiple of 8 bytes).
Given the above observations, the table below gives typical memory usage in bytes for various common types of Java object
such as dates and strings:
Java object type | Memory usage | Observations |
Integer | 16 bytes | A boxed int (Integer) will take 16 bytes when object overhead is taken in to account.
(This will generally be the minimum amount of memory that any Java object will take up.) |
Long | 24 bytes | Theoretically, a long requires 4 more bytes compared to an int. But because object sizes will
typically be aligned to 8 bytes, a boxed Long will generally take up 24 bytes compared to 16 bytes for an Integer. |
Date | 24 bytes | |
java.sql.Date | 24 bytes | |
LocalDate | 24 bytes | |
LocalDateTime | 48 bytes | |
Calendar (=GregorianCalendar) | 560 bytes | Beware of storing large numbers of Calendar instances: they are not a compact format for storing dates/times. |
String (16 chars) | 56 bytes | Strings that only contain characters in the single-byte range 0-255 can be stored compactly as one byte per character. (But prior to Java 9, 2 bytes per character will be required.) |
String (16 chars, Unicode) | 104 bytes | Strings that contain characters outside the single-byte range will be stored in Unicode format, generally 2 bytes per character. |
StringBuilder (100 char capacity) | 144 bytes | StringBuilders will start off requiring 1 byte per character of capacity plus overhead, but will be "expanded" to require 2 bytes per character when the first out-of-range character is added. |
EnumSet | 32 bytes | Valid for typical enums with 64 possible values or less; note that you could technically use an 8-byte long to store this information! |
byte[] with 1024 elements | 1040 bytes | The additional 16 bytes are the overhead of storing object housekeeping information and the array length. |
boolean[] with 1024 elements | 1040 bytes | Note how wasteful boolean arrays are because they use 1 byte to store 1 bit of information! |
BitSet(1024) | 168 bytes | A BitSet is a much more memory compact way of storing an array of bits in Java. Note that for a small bit set, there is still a non-negligible overhead (168 bytes compared to the theoretical size of 128 bytes to store 1024 bits). |
HashMap of 100 ints mapped to ints | ~6.75 KB | The object overhead of boxed integers plus the 'bucket' objects internal to HashMap mean that almost ten times as much space is required over the theoretical amount of information stored (200 ints = 800 bytes). |
The articles in this section look at various aspects of memory usage in Java. Please note that some of this information is currently undergoing revision
and may be a little out of date: