Memory usage of objects in Java

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

Memory usage of some typical Java objects

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 typeMemory usageObservations
Integer16 bytesA 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.)
Long24 bytesTheoretically, 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.
Date24 bytes 
java.sql.Date24 bytes 
LocalDate24 bytes 
LocalDateTime48 bytes 
Calendar (=GregorianCalendar)560 bytesBeware of storing large numbers of Calendar instances: they are not a compact format for storing dates/times.
String (16 chars)56 bytesStrings 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 bytesStrings that contain characters outside the single-byte range will be stored in Unicode format, generally 2 bytes per character.
StringBuilder (100 char capacity)144 bytesStringBuilders 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.
EnumSet32 bytesValid 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 elements1040 bytesThe additional 16 bytes are the overhead of storing object housekeeping information and the array length.
boolean[] with 1024 elements1040 bytesNote how wasteful boolean arrays are because they use 1 byte to store 1 bit of information!
BitSet(1024)168 bytesA 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 KBThe 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).

Java memory usage in more detail

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:

If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.

Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.