Concept of Heap Size In Java (OUT OF MEMORY ERROR)

Have you ever came across the error like java.lang.OutOfMemoryError in Tomcat, while running an application???? Then this the r8 place you are wondering for.

You generally get along with this error, when you are trying to access extra memory for objects, than the actually defined fixed size. The size allocated when creating new objects is called as heap size.

HEAP SIZE:  When you are working with any application in java, a specific memory is allocated to Java virtual machine (JVM) by Operating System. JVM uses this memory for all its work and from this , a part of memory is known as JAVA HEAP MEMORY.

When any object is created by new operator or by some other way, it is allocated some memory space from heap. If the object creation requires more space than the heap size it throws the error java.lang.OutOfMemory. Heap resides at the bottom space of the memory allocated to JVM.

Java Heap space is generally divided into three regions or generation for sake of garbage collection

i) New Generation

ii) Old or tenured Generation

iii) Perm Space (permanent Generation).

As and when the object dies or is garbage collected , the free memory is joined to the heap memory. The default heap size limit in java is 128MB for a 32-bit, but it highly varies from JVM to JVM. But this is not it, you can also change the heap space if required when you need more objects or your application is quite large.

Changing the heap size

Generally there are two functions that deal with changing the heap size:

1) -Xms : This signifies the minimum(initial) heap size in java.

2) -Xmx : This signifies the maximum heap size in java.

To change the heap size , open CMD-> Change the directory to path where your java is installted.

Suppose your java is installed in directory C:\Program Files (x86)\Java\jdk1.7.0-64\bin

Below shown screen shot shows you the wayto change heap size:




Press enter and your heap size gets changed to minimum size of 256Mb and maximum size of 1024Mb.

Program to know heap size:

public class Heapsize {

public static void main(String [] args) {

int mb = 1024*1024;

//Getting the runtime reference from system

Runtime runtime = Runtime.getRuntime();

System.out.println ("##### Heap utilization statistics [MB] #####");

//Print used memory

System.out.println("Utilized Memory:" + (runtime.totalMemory() - runtime.freeMemory()) / mb);

//Print free memory

System.out.println ("Free Memory:" + runtime.freeMemory() / mb);

//Print total available memory

System.out.println ("Total Memory:" + runtime.totalMemory() / mb);

//Print Maximum available memory

System.out.println ("Maximum Memory:" + runtime.maxMemory() / mb);





Java also provides you the facility to change the Thread stack size by the function:

i)   -Xss<size>  set java thread stack size

Hope this helps you. If you face any problem in this blog do leave a message, always keen to help you… 🙂

Speak Your Mind

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: