Thursday, September 26, 2019

java.lang.OutOfMemoryError: PermGen space Cause and Solution

PermGen Space of the heap is used to store classes and Metadata 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 it's not garbage collected and Perm Gen Space is not freed up.

This causes a memory leak in PermGen Space and eventually cause java.lang.OutOfMemoryError: PermGen space. Another important point is that when you deploy your web application a new Classloader gets created and it loads the classes used by the web application. So if Classloader doesn't get garbage collected when your web application stops you will have a memory leak in tomcat.

Saying that a Java program can have memory leak is little surprising for many programers, because they think that Garbage Collector will reclaim memory, and that's one of the reason, why Java is popular programming language. Bug, GC has it's limitation, it can not reclaim the object, which is undesired but still refrenced by some object in Program. This memory, which should be free but still can not be reclaimed is called Memory leak in Java. If you see java.lang.OutOfMemoryError: Java heap space, even after increasing heap space, than it's good to analyze memory pattern for any possible memory leak. There are tools like Profiler (JProbe, Yourkit or Netbeans Profiler), JConsole, VisualVM, which you can use to see memory usage of Java application to confirm memory leak. There are even tools like Plumbr, which can help you to find memory leaks in Java. Though this would be as easy as increasing heap memory to fix java.lang.OutOfMemoryError: Java heap space.

No comments:

Post a Comment