Sunday, May 6, 2012

Prevent OutOfPermGenSpace Java Error

PermGen space error is thrown by the Java Application if the application loads more number of classes into the memory than limited by the JVM PermSpace limit. This happens in a bigger Java applications and typically observed if the same application is loaded into the memory several times. PermGen space error is not same as Heap Space error. This error can also occur if there are memory leaks by your application or memory leaks in class loaders themselves. PermGen space is the memory in JVM to keep the loaded classes.

Let's understand different types of memory allocated by JVM for Java applications.
  • den Space (heap): The pool from which memory is initially allocated for most objects.
  • Survivor Space (heap): The pool containing objects that have survived the garbage collection of the Eden space.
  • Tenured Generation (heap): The pool containing objects that have existed for some time in the survivor space.
  • Permanent Generation (non-heap): The pool containing all the reflective data of the virtual machine itself, such as class and method objects. With Java VMs that use class data sharing, this generation is divided into read-only and read-write areas.
  • Code Cache (non-heap): The HotSpot Java VM also includes a code cache, containing memory that is used for compilation and storage of native code.
If we look at the definitions above there two categories (1) Heap and the other (2) Non-Heap. Heap is run time 'Data Area' meaning that memory allocated by JVM for all class instances and arrays whereas Non-Heap is 'Method Area' meaning that the memory required for internal processing or optimization for the JVM.

Both types of memories Heap and Non heap may be of fixed size or variable size. It depends on implementation of the underlying JVM. The Non-heap space may or may not be garbage collected unlike the heap memory. Though there are ways to increase these types of memories but its unlikely that out of PermGenSpace error is resolved since its a shared memory. However increasing the global heap space size may prevent OutOfHeap error in a Java Application.

What can be done to solve OutOfPermGenSpace Error?

1. Try out increasing the size limit using  "-XX:PermSize" and  "-XX:MaxPermSize" options as JVM arguments. This is a workaround but not a solution.
2. Check if your application has any memory leaks using Java Profiling Tools.
3. Check if there are any memory leaks by the class loaders. Yes, there could be memory leaks by class loaders too.

References

PermGen is not part of Heap Space
http://docs.oracle.com/javase/6/docs/technotes/guides/management/jconsole.html

Issues with Perm Space growing are seen while we manipulates class loaders.
http://stackoverflow.com/questions/2051734/why-is-permgen-space-growing

Fix the memory leaks found
http://www.springsource.com/files/uploads/all/pdf_files/news_event/Inside_the_JVM.pdf



No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...