Skip to content

JAVA Objects Memory Size Reference

September 1, 2013

Whether you are a well-grounded java programmer or a newcomer, It is essential to know memory consumption calculation in java. so in this article I am going to write about memory consumption of objects, Data types and collections, which are the most important in java.

Instances of an object on the Java heap take up memory for their actual fields and housekeeping information which consist of recording an object’s class, ID and status flags such as whether the object is currently reachable, currently synchronization-locked etc.

Each Object reference occupies 4 bytes if the Java heap is under 32GB and XX:+UseCompressedOops is turned on (it is turned on by default). Otherwise, Object references occupy 8 bytes. If the number of bytes which are required by an object for its header and fields is not multiple of 8, then you round up to the nearest multiple of 8 due to padding.

Padding or alignment: the JVM allocates the memory in multiples of 8 bytes. for example look at following class:

class X {
int a;
byte b;

JVM allocates 8 bytes from reference to the class definition + 1 byte (the b variable) + 4 bytes (the a variable)=13 bytes +3 bytes for padding to rounding up to 16 to be multiple of 8.
primitive data types
All primitive data types occupy following byte size:

  • byte, Boolean:1 byte.
  • short, char :2 bytes.
  • int, float :4bytes.
  • long, double :8bytes.

Numeric wrappers
Numeric wrappers occupy 12 bytes + size of the underlying type:

  • Byte, Boolean : 12 bytes +1 byte Data type +3 bytes alignment = 16 bytes.
  • Short, Character : 12 bytes +2 bytes Data type +2 bytes alignment =16 bytes.
  • Integer, Float : 12 bytes +4 bytes Data type =16 bytes.
  • Long, Double : 12 bytes +8 bytes Data type +4 bytes alignment = 24bytes.

HashMap, HashSet
HashMap is built on top of the array of “Map.Entry” objects. It contains a key, a value, hash of a key and an int and a pointer to the next entry. it means that an entry occupies 32 bytes (12 bytes header + 16 bytes data + 4 bytes padding). So, a HashMap with size = S has to spend 32*s bytes for entries storage. Besides, it will use 4 * c bytes for entries array, where c is the map capacity. memory consumption of a HashSet is identical to HashMap.

LinkedHashMap is not efficient and is famous for the most memory-hungry collection in JDK. It extends HashMap by using “LinkedHashMap.Entry” as an entry in the internal array of entries. It means that LinkedHashMap consumes 40 * SIZE + 4 * CAPACITY bytes.

TreeMap , TreeSet
a tree contains exactly some nodes. Each tree node contains: key, value, pointers to the left and right children, pointer to a parent and a boolean ‘colour’ flag. so It means that a node occupies:
12 bytes for header
20 bytes for 5 object fields : int key,int value,int left pointer,int right pointer,int parent pointer.
1 byte for flag
So, the total memory consumption of a TreeMap is 40 * SIZE bytes, which is approximately the same as the memory consumption of a HashMap.
so its memory consumption is identical: 40 * SIZE bytes.
each LinkedList node contains references to the previous and next elements as well as a reference to the data value. So 12 bytes header + 3*4 bytes for references, which is 6 times more than ArrayList in terms of per-node overhead.

it has an Object[] for storage plus an int field for tracking the list 4 bytes (but may be more if ArrayList capacity is seriously more than its size).

Collection Overview

JDK collection                Size

HashMap                           32 * SIZE + 4 * CAPACITY bytes

HashSet                             32 * SIZE + 4 * CAPACITY bytes

LinkedHashMap             40 * SIZE + 4 * CAPACITY bytes

TreeMap, TreeSet           40 * SIZE bytes

Note: This article is presented by Saeid Siavashi.  Know more about him in:




  1. Mahdi permalink

    Impatient for new great articles 😀

Trackbacks & Pingbacks

  1. JAVA Objects Memory Size Reference | IT World Web
  2. Building a Graph Engine — Key-Value Store | Shisoft Notes

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: