You must have often come across something OutOfMemoryError: PermGen Space error.
Let's first get in to a little detail of what actually is permanent generation heap space and why Permgen Space error comes and how to handle this.
What is:-
There are two memory regions in the JVM: the heap and the stack. Local variables and methods reside on the stack, everything else on the heap.
The heap memory is structured again into regions, called generations.
The Java virtual machine is organized into three generations: a young generation, an old generation, and a permanent generation.
Most objects are initially allocated in the young generation. The old generation contains objects that have survived some number of young generation collections, as well as some large objects that may be allocated directly in the old generation. The longer an object lives, the higher the chance it will be promoted to an older generation.
Young generations are more garbage collected than older generations.
However, there is also some separate heap space called permanent generation. Since it is a separate region, it is not considered part of the Java Heap space. Objects in this space are relatively permanent. Class definitions are stored here, as are static instances.
For the HotSpot Java VM, the memory pools for serial garbage collection are the following.
Eden 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.
How:-
The prime reason for the error is the deployment and undeployment of classes all the time .
Let's take the scenario of a web server.
When an application is deployed or undeployed, its class definitions and Classloader are respectively put into and removed from the permanent generation heap. The problem arises when the undeployment of classes happens and they are not fully garbage collected.
Let’s say we have a class A, which is part of a jar in a web application that is deployed on some webserver. In the lib folder of the web server, there is some logging framework present, which has a Log class with the method abc(Class B) with which classes can be registered for logging. Let’s say that the class A gets registered by this method and the Log class starts keeping a reference to the B's object. When the class A gets undeployed, it is still registered with the Log class. The Log class will still have a reference to it and hence, it will never be garbage collected. Moreover, since the class A has a reference to its ClassLoader in turn, the ClassLoader itself will never be garbage collected either, and so will none of the classes it loaded. This will lead to the permgen space error.
How to fix this:
Set –Xms(initial java heap size) and –Xmx(maximum java heap size)
-Xms512m -Xmx1024m -XX:PermSize=1024m -XX:MaxPermSize=2048m

No comments:
Post a Comment