int vs Integer – Java Application Memory Usage

 

Before you read further, Please answer the following question:

A. int a = 100;

B. Integer b = new Integer(100);

There are two java statements written above. What is the ratio of memory used by b to memory used by a in a 32 bit machine?

The four options are:

  1. 1
  2. 1.5
  3. 2
  4. 4

Pick your answer and comment in the comment box below.

We will come back to the answer later.

What do we mean when we say 32 bit machine?

Machine,Server,Computer(All are essentially same in this context) have CPU register. These register have a specified size. 32 bit machines have CPU register of the size of 32 bits. CPU register stores memory address. CPU register is used by processor to access data in RAM.

32 bit machine can access 2^32 memory location or in other words, memory locations from 00000000000000000000000000000000 to 11111111111111111111111111111 which amounts to 4294967296 individual memory location. Assuming each memory location has 1 byte of space, 32 bit machine can access 4 GB of memory.

Now, when we run our java application on a 32 bit machine, OS process assign us 4 GB of space. Out of the 4 GB space, around half GB is used by OS itself and some memory is used by JVM.  This leaves us around 3 GB of space to play with.

java -Xmx1g myprogram

Remember the command? We are running a java class called myprogram with java heap memory size as 1 GB. This java heap memory is used to store the objects of class. Native Heap Memory is used for storing OS related tasks like thread creation. We can get OutOfMemoryError if we run out of either of Native Heap Memory or Java Heap Memory. The picture below might help in understanding.

Untitled Diagram (2).png

What happens when we write Integer b = new Integer(100)?

Coming back to question at the beginning of the post. This object doesn’t just store 100; instead it stores various related information. They are:

  • Class pointer: In our case of ajava.lang.Integer object, for example, this is a pointer to the java.lang.Integer class
  • Flags: Flags storing state of the object
  • Lock: To support synchronization
  • int value 100

Each one of class pointer, flags, lock and int value takes 32 bits which makes the b object take 128 bits. Memory used by b object is roughly 4 times of what it takes a to store.

How about int[] data = new int[1]?

Here, we are going to make an int array of size 1. The memory taken by object data is:

  • Class pointer: In our case of array of int fields, for example, this is a pointer to the int[]
  • Flags: Flags storing state of the object
  • Lock: To support synchronization
  • Size of the array
  • int value 100

Each one of class pointer, flags, lock, size and int value takes 32 bits which makes the data object take 160 bits. Memory used by data object is roughly 5 times of what it takes a to store. Single entry array is more expensive in terms of memory to the Integer object too.

Okay fine! Tell me about the memory usage of String greet = new String(“abcdefgh”);

String creates two object. The first one stores the usual suspects along with a count, offset, value fields. Value field stores the address of second memory location which actually stores the string abcdefgh.

Screen Shot 2018-01-07 at 2.29.00 AM

 

 

Screen Shot 2018-01-07 at 2.32.14 AM.png

 

 

 

 

 

 

 

 

 

 

The first object has memory size of 224 bits (32 * 7) and the second object has size of 256 bits. So, total of 224+256=480 bits are used to store 128 bits (size of char array) of data. That means memory overload of 480/128 = 3.75

And now, little bit about collections!

HashMap<String, String> myMap = new Hashmap<>();

What happens when we write this?

myMap is created with a default capacity of 16 key-value pair. myMap memory size is 128 bytes even when it’s empty. For every entry in myMap, 36 extra bytes are required. What happens when we reach to the default capacity of 16 key value pairs? Hashmap expands by doubling the size; so even if you need 17 key value pair, hashmap will allocate 32 empty key value pairs.

Screen Shot 2018-01-07 at 3.05.52 AM


Reference:

https://www.ibm.com/developerworks/library/j-codetoheap/index.html

http://www.vogella.com/tutorials/EclipseMemoryAnalyzer/article.html

If you liked this article and would like one such blog to land in your inbox every week, consider subscribing to our newsletter: https://skillcaptain.substack.com

One thought on “int vs Integer – Java Application Memory Usage

Add yours

Leave a Reply

Blog at WordPress.com.

Up ↑