Solutions for OutOfMemoryError and PermGen Space Error

In java while running application or running test cases will face below memory issues.
  • Java.lang.OutOfMemoryError: Java Heap space
  • java.lang.OutOfMemoryError:PermGen space

Solutions for Memory issues!!

  • Find the cause of Memory Leak , if there is any memory leak. Try to resolve using Memory Analyzer(Find details below screen shot)

    Usually everybody will do small mistakes like creating unnecessary variables and unused variables. Unnecessary variables we can find out by PMD kind of tools. But unused variables like JUNITS will create one variable after execution of test method execution still it is alive. Try to practice like make it variable as a null. Use @Before and @After methods to initialize variable and to make it null. In source classes using MemoryAnalizer we can find which class is creating memory issues and we can try to resolve that problem.

  • Increase size of PermGen Space/Heap Space by using JVM param.

    export JVM_ARGS="-Xmx1024m -XX:MaxPermSize=256m

  • Externally calling Garbage Collector or cleaning/Clearing JVM memory

    In eclipse: GoTo Preferences-> java->installedJRE->edit->paste below properties in Default VM Arguments text box. See how your application will work without memory issues.

      Djava.awt.headless=true -Dfile.encoding=UTF-8 -Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC -XX:+CMSClassUnloadingEnabled -verbose:GC

Eclipse Default VM Arguments text box

Tools to investigate and fix OutOfMemoryError in Java

Java.lang.OutOfMemoryError is a kind of error which needs lot of investigation to find out root cause of problem, which object is taking memory, how much memory it is taking or finding dreaded memory leak and you can't do this without having knowledge of available tools in java space. Here I am listing out some free tools which can be used to analyze heap and will help you to find culprit of OutOfMemoryError

1) Visualgc
Visualgc stands for Visual Garbage Collection Monitoring Tool and you can attach it to your instrumented hostspot JVM. Main strength of visualgc is that it displays all key data graphically including class loader, garbage collection and JVM compiler performance data.

The target JVM is identified by its virtual machine identifier also called as vmid. You can read more about visualgc and vmid options here.

2) Jmap
Jmap is a command line utility comes with JDK6 and allows you to take a memory dump of heap in a file. It's easy to use as shwon below:

jmap -dump:format=b,file=heapdump 6054

Here file specifies name of memory dump file which is "heapdump" and 6054 is PID of your Java progress. You can find the PID by using "ps -ef" or windows task manager or by using tool called "jps"(Java Virtual Machine Process Status Tool).

3) Jhat
Jhat was earlier known as hat (heap analyzer tool) but it is now part of JDK6. You can use jhat to analyze heap dump file created by using "jmap". Jhat is also a command line utility and you can rum it from cmd window as shown below:

jhat -J-Xmx256m heapdump

Here it will analyze memory-dump contained in file "heapdump". When you start jhat it will read this heap dump file and then start listening on http port, just point your browser into port where jhat is listening by default 7000 and then you can start analyzing objects present in heap dump.

4) Eclipse memory analyzer
Eclipse memory analyzer (MAT) is a tool from eclipse foundation to analyze java heap dump. It helps to find classloader leaks and memory leaks and helps to minimize memory can use MAT to analyze heap dump carrying millions of object and it also helps you to extract suspect of memory leak. See here for more information.

When an Object becomes Eligible for Garbage Collection

An Object becomes eligible for Garbage collection or GC if its not reachable from any live threads or any static references in other words you can say that an object becomes eligible for garbage collection if its all references are null. Cyclic dependencies are not counted as reference so if Object A has reference of object B and object B has reference of Object A and they don't have any other live reference then both Objects A and B will be eligible for Garbage collection.

Generally an object becomes eligible for garbage collection in Java on following cases:
  • All references of that object explicitly set to null e.g. object = null
  • Object is created inside a block and reference goes out scope once control exit that block.
  • Parent object set to null, if an object holds reference of another object and when you set container object's reference null, child or contained object automatically becomes eligible for garbage collection.
  • If an object has only live references via WeakHashMap it will be eligible for garbage collection.