What is java heap space in java?

Java Heap Space ( or Java Object Space):

Its a part of memory from the memory of operating system which java virtual machine or JVM use for the Java operations.
Java heap space is normally located at the bottom of the memory space and move upwords. Whenever object gets created using new operator some space in heap is generated for the new object.

Normally Java heap stores the objects which are instances of classes and primitives,references.

Three main regions of java heap space :

1. New generation.
2. Old or tenured generation.
3. Perm generation

New generation is the space or part of the java heap space where newly created object space is allocated.
Old or tenured generation where space is allocated to the objects which are not referred any more and survived the garbage collection.
Perm generation is the place where java stores the meta data information about the classes and methods, String pool and other class level details.

Changing the java heap size :

There are options available to change the allocated size to the Java Heap :
-Xms --- To set the initial java heap size
-Xmx --- To Set maximum java heap size
-Xss ---  To set thread stack size

Above size comes with units such as MB, KB, GB etc.

Getting the java heap size :

We can either use JConsole or programmatic way we can use following to get java heap size.

long heapSize = Runtime.getRuntime().totalMemory();

Many times we can see the java.lang.outofmemoryerror due to insufficient memory, we can avoid it by increasing the heap size.
Share on Google Plus

About Pranav

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment


  1. Hey there! I'm at work browsing your blog from my new iphone 3gs! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the outstanding work!