Thursday, March 22, 2012

java.lang.OutOfMemory in Java

There are 3 OutOfMemory error cases in java we can see frequently in production with default parameters.

1) Java.lang.OutOfMemoryError: Java heap space
2) Java.lang.OutOfMemoryError: PermGen space
3) java.lang.OutOfMemoryError: GC overhead limit exceeded

1) Java.lang.OutOfMemoryError: Java heap space : 

What is java heap space ?

while you try to create an object and there is not enough space in heap to allocate that object you will get this error. 

How to solve ? 

Increase the heap size memory. Use : -Xms512m  -Xmx1024m

a) In production server / linux by adding parameter in JAVA_OPTS 
b) for eclipse add in eclipse.ini file under vmargs
c) In standalone program pass in vm Arguments. 

Note : Xms represent heap size minimum. Xmx represent heap size maximum. Based on the ram you have you can allocate. 


Read more: http://javarevisited.blogspot.com/2011/09/javalangoutofmemoryerror-permgen-space.html#ixzz2KEV0BOiK

2) Java.lang.OutOfMemoryError: PermGen space

what is PermGen Space ? 

PermGen Space of heap is used to store classes and Meta data about classes in Java. When a class is loaded by a classloader it got stored in PermGen space, it gets unloaded only when the classloader which loaded this class got garbage collected. If any object retains reference of classloader than its not garbage collected and Perm Gen Space is not freed up. This causes memory leak in PermGen Space and eventually cause java.lang.OutOfMemoryError: PermGen space

How to Solve ?

Increase the permSize memory. Use : -XX:MaxPermSize=256m

By default JVM allocates 64M for perm space. You can increase it by passing argument. 

a) In production server / linux by adding parameter in JAVA_OPTS 
b) for eclipse add in eclipse.ini file under vmargs
c) In standalone program pass in vm Arguments. 

For tomcat in bin/catalina.sh add options in CATALINA_OPTS for setting the heap and perm space. You can also add below options.
-XX:+UseParallelGC -XX:+UseParallelOldGC -Xloggc:gc.log  -XX:HeapDumpPath=DumpPath -XX:+UseCompressedOops


Read more: http://javarevisited.blogspot.com/2012/01/tomcat-javalangoutofmemoryerror-permgen.html#ixzz2KEWjRaC1

3) java.lang.OutOfMemoryError: GC overhead limit exceeded

Mostly when you use HashMap objects to dump the data you may get this above error. There are 4 ways to avoid the same.

  1. Specify more memory, try something in between like -Xmx1024m first
  2. Work with smaller batches of HashMap objects to process at once if possible
  3. If you have a lot of duplicate strings, use String.intern() on them before putting them into the HashMap (String.intern will save lot of spaces on blank strings and duplicate strings).This will slow down your process. Having said memory utilization on the heap will be very less. Refer article : https://blog.codecentric.de/en/2012/03/save-memory-by-using-string-intern-in-java/ for more 
  4. Use the HashMap(int initialCapacity, float loadFactor) or HashMap(initialCapacity) constructor to tune for your case ex : new HashMap(100000).
You can Disable the error check altogether, via "-XX:-UseGCOverheadLimit". But, you will end up getting heap space OOM error. So, try to use the above methodologies to avoid or tweak your logic to avoid storing huge contents in hashMap objects.

Refer for more OOM : http://www.theserverside.com/news/thread.tss?thread_id=79323 

Eclipse Out Of Memory Error : 
If you use eclipse update memory configuration in JRE
Eclipse -> Window -> Preference -> Java-> Installed JREs -> select the running JRE -> Default VM arguments pass the arguments
"-Xms512m  -Xmx1024m -XX:MaxPermSize=256m"


4 types of Garbage Collectors : Refer : 
http://www.takipiblog.com/garbage-collectors-serial-vs-parallel-vs-cms-vs-the-g1-and-whats-new-in-java-8/
// Below script tag for SyntaxHighLighter