What is Java Heap

When a Java program started Java Virtual Machine gets some memory from Operating System. JVM uses this memory for all its need and part of this memory is call java heap memory.

Heap in Java generally located at the bottom of address space and move upwards. Whenever we create the object using new operator or by any another means an object is allocated memory from Heap and When object dies or garbage collected, memory goes back to Heap space in Java.

So Java heap is a memory space which is used to store the Java Objects. The heap may be of a fixed size or may be expanded as required by the computation and may be contracted if a larger heap becomes unnecessary.


How to Increase Java Heap Size

The default size of Heap space in Java is 128MB on most of 32 bit Sun’s JVM but it highly varies from JVM to JVM. However, we can increase a size of java heap space based on your application need and I always recommend this to avoid using default JVM heap values, If your application is large and lots of objects created you can change the size of heap space by using JVM options -Xms and -Xmx.

Xms denotes starting size of Heap while -Xmx denotes the maximum size of Heap in Java. There is another parameter called -Xmn which denotes Size of a new generation of Java Heap Space. Only thing is you can not change the size of Heap in Java dynamically, you can only provide Java Heap Size parameter while starting JVM.


Java Heap & Garbage Collection

As we know objects are created inside heap memory and Garbage collection is a process which removes dead/used(Objects with no reference) objects from Java Heap space and returns memory back to Heap in Java.

For the sake of Garbage collection, Heap is divided into three main regions named as New Generation, Old Generation, and Perm space.

New Generation of Java Heap is part of Java Heap memory where a newly created object is stored, During the course of application many objects created and died but those remain to live they got moved to Old Generation by Java Garbage collector.

Perm space of Java Heap is where JVM stores Metadata about classes and methods, String pool, and Class level details.



OutOfMemory Error in Java 

When JVM starts, JVM heap space is equal to the initial size of Heap specified by -Xms parameter, as application progress more objects get created and heap space is expanded to accommodate new objects.

JVM also run garbage collector periodically to reclaim memory back from dead objects. JVM expands Heap in Java somewhere near to Maximum Heap Size specified by -Xmx and if there is no more memory left for creating the new object in java heap , JVM throws java.lang.OutOfMemoryError and your application die.


Spotlight on Java Heap

  • Java Heap Memory is part of memory allocated to JVM by Operating System.
  • Objects always create in the Java heap.
  • Java Heap space is divided into three generation for the sake of garbage collection called New Generation, Old Generation, and Perm Space. The permanent generation is garbage collected during full go in hotspot JVM.
  • You can increase the size of Java Heap space by using JVM command line option -Xms, -Xmx and -Xmn. don’t forget to add a word “m” or “g” after specifying the size to indicate Mega or Gigabyte. For example, you can set java heap size to 258MB by executing following command java -Xmx256m FirstProgram.
  • You can use command “jmap” to take Heap dump in Java and “jhat” to analyze that heap dump.
  • Java Heap space is different than Stack which is used to store method call hierarchy and local variables.
  • Java Garbage collector is responsible for reclaiming memory from a dead object and returning to Java Heap space.
  • Use Profiler and Heap dump Analyzer tool to understand Java Heap space and how much memory is allocated to each object.


Selenium Testing Video Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply