Exception in thread java.lang.OutOfMemoryError: Java heap space



Contents of page >


1) Exception in thread thread_name java.lang.OutOfMemoryError: Java heap space in java

OutOfMemoryError : Java heap space - is thrown whenever there is insufficient space to allocate an object in the Java heap.

Is Exception in thread threadName - java.lang.OutOfMemoryError - Java heap space
Indicates memory leak?
No, this OutOfMemoryError does not necessarily means that it is memory leak.

>How to set or change permgen size in tomcat server, eclipse?


2) How to solve Exception in thread thread_name java.lang.OutOfMemoryError - Java heap space in java?
You may need to increase the heap size using -Xms and -Xmx jvm parameters as a solution to this issue.

2.1) What is -Xms JVM parameter in java?
-Xms : Xms is minimum heap size which is allocated at initialization of JVM in java.

Examples of using -Xms VM (JVM) option in java >

Example1 of using -Xms VM (JVM) option in java >
java -Xms512m MyJavaProgram
It will set the minimum heap size of JVM to 512 megabytes.

Example2 of using -Xms VM (JVM) option in java >
java -Xms1g MyJavaProgram
It will set the minimum heap size of JVM to 1 gigabyte.


2.2) What is -Xmx JVM parameter in java?
-Xmx : Xmx is the maximum heap size that JVM can use.

Examples of using -Xmx VM option in java >

Example1 of using -Xmx VM (JVM) option in java >
java -Xmx512m MyJavaProgram
It will set the maximum heap size of JVM to 512 megabytes.

Example2 of using -Xmx VM (JVM) option in java >
java -Xmx1g MyJavaProgram
It will set the maximum heap size of JVM to 1 gigabyte.

Read more in detail - What are -Xms and -Xmx JVM parameters in java, and differences between them with examples



3) OutOfMemoryError may also be thrown in java when >

OutOfMemoryError may also be thrown when an excessive amount of time is being by jvm in performing garbage collection and very little memory is being freed.

A long lived application might be unintentionally holding references to objects and this prevents the objects from being garbage collected. Holding of objects for a long time is also a kind of memory leak in java.


Also one of the most important source of OutOfMemoryError ( Java heap space)  could be the excessive use of finalizers in the application.

What happens with the excessive use of finalizers in the application?
If a class has a finalize method, then space for object is not reclaimed at garbage collection time. Instead the objects are queued for finalization after garbage collection, finalization occurs at some later time.

4) How excessive use of finalizers could cause OutOfMemoryError in java?
finalizers are executed by a daemon threads. As we discussed above that finalization occurs at some later time. Holding finalizer daemon threads for long time could fill the Java heap and cause OutOfMemoryError.

5) Example/Program in java to generate OutOfMemoryError: Java heap space >
Before executing program pass this vm parameter in eclipse.
-Xmx5m   (Here maximum heap memory set to just 5 megabytes, so that we could easily produce OutOfMemoryError: Java heap space)

package outofmemory;
import java.util.ArrayList;
import java.util.List;
/**
*
* Write a program which could throw
* java.lang.OutOfMemoryError : Java Heap Space
*
*/
public class OutOfMemoryErrorJavaHeapSpace {
   static List<String> list=new ArrayList();
   public static void main(String[] args) {
          while(true){
                 list.add(new String("a"));   //Line 16
          }
   }
}


OUTPUT of program >
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
   at java.util.Arrays.copyOf(Unknown Source)
   at java.util.Arrays.copyOf(Unknown Source)
   at java.util.ArrayList.grow(Unknown Source)
   at java.util.ArrayList.ensureExplicitCapacity(Unknown Source)
   at java.util.ArrayList.ensureCapacityInternal(Unknown Source)
   at java.util.ArrayList.add(Unknown Source)
   at outofmemory.OutOfMemoryErrorJavaHeapSpace.main(OutOfMemoryErrorJavaHeapSpace.java:16)

The solution is simply to increase the Xmx parameter to -Xmx512m as we discussed above.


6) Summary -
So in this tutorial we learned that OutOfMemoryError : Java heap space - is thrown whenever there is insufficient space to allocate an object in the Java heap. We also learned how to produce and solve this issue this error by program.

solve Exception in thread thread_name java.lang.OutOfMemoryError - Java heap space?
Increase the heap size using -Xms and -Xmx jvm parameters.
-Xms : Xms is minimum heap size which is allocated at initialization of JVM in java.
-Xmx : Xmx is the maximum heap size that JVM can use.

OutOfMemoryError may also be thrown when > an excessive amount of time is being by jvm in performing garbage collection and very little memory is being freed.

Excessive use of finalizers could cause OutOfMemoryError.



Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Error in exception handling in java

How to write java program to pass VM/JVM parameters through CMD

eEdit
Must read for you :