“java.lang.OutOfMemoryError: PermGen space ”: have you seen this error before ? If you have never seen it, you haven't probably been involved in creating large java server applications as of lately. Let's understand what this error means.
When you get OutOfMemoryError, it means that (so obvious) your application doesn't have enough memory to go on. But the rest of the message, “PermGen space” means that it's not the common object heap space that it's lacking, but only the memory space for the binary code of classes and methods is lacking.
Let's go deeper into this subject to understand properly what PermGen means. Well, PermGen is an acronym for “Permanent Generation”. The term Generation comes from the concept of “generational collection” and the term “generational collection” refers to an heuristic in the garbage collection mechanism that is used to intelligently identify only the most probable objects for garbage collection. There are 3 kinds of Generations in the VM memory: the young generation, the tenured generation and the permanent generation.
Let's go back to the original “Permanent Generation” term. As I said, it identifies a particular set of objects which are candidate for garbage collection and handled differently by the GC. These kind of objects which belong to the “Permanent Generation” are only the binary codes of all classes and methods. So, the binary code of classes and methods are not stored in the same memory area as the object instances. Now, I think you already understand what this error mean. “java.lang.OutOfMemoryError: PermGen space ” means that the virtual machine ran out of space for loading the binary code of classes and methods.
There are two steps ahead: knowing understanding why this error has happened to your application, learning to monitor the permanent generation size in order to prevent the occurrence of this error; and learning how to increase the permanent generation size.
Why has this error happened ?
The default size for permanent generatoin is 64M. But how come my not so large web application, which totals 2 megabytes of class code, could exceed this limit ? You're probably not counting it correctly. Your web application probably runs inside Jboss, Tomcat or another J2EE server. Add the size of Java SE libraries, the size of Jboss, the size of all libraries used by Jboss internally, the size of all libraries in the WEB-INF/lib directory of you WAR and you will get a large, large binary code area.
Aren't Java classes loaded on demand ? Yes, they are loaded on demand. So, you will probably have to factor that large number by 3 or 4. But there are other factors for the enlargement of the permanent generation area. If you web application has JSP pages, remember that every JSP page is converted into a servlet class file, before being executed. So, you should add that to the previous number. Also, maybe there are other web applications running under Jboss than you own. If you use Hibernate, for each persistent class that you create, Hibernate will dynamically create an enhanced class. If you use RMI or EJB, many kinds of proxy classes will be created and loaded in memory.
Now that you believe that the size of the binary code of classes and methods can exceed 64M, you have probably given up summing the sizes of all classes created and loaded by the VM, Jboss and you application. You'd better off monitoring the use of PermGen area than trying to calculate its size. That leads us to the next step: how can I monitor the increase in PermGen area.
How can I monitor the increase of PermGen area ?
(I'll finish this 2-part series on PermGen errors tomorrow)