Tomcat outofmemoryerror (permgen space) Solution
Recently I have been familiar with a project that has been developed for several years. I need to port the database from MySQL to Oracle. First, I need to point the JDBC connection to MySQL and package it into tomcat to run it. There is no problem, however, when the JDBC connection is directed to Oracle, Tomcat continuously throws Java. lang. outofmemoryerror: I googled the Internet and learned about the running mechanism of Tomcat. I also solved the problem and shared it for future reference.
1. First, java. Lang. outofmemoryerror: Java heap Space
Explanation:
Heap size settings
JVM heap setting refers to the setting of memory space that JVM can allocate during Java program running. the JVM automatically sets the heap size value at startup. The initial space (-XMS) is 1/64 of the physical memory, and the maximum space (-xmx) is 1/4 of the physical memory. You can use options such as-xmn-XMS-xmx provided by JVM to set the configuration. The heap size is the sum of young generation and tenured generaion.
Tip: if 98% is used for GC and the available heap size is less than 2% in JVM, this exception is thrown.
Tip: the heap size should not exceed 80% of the available physical memory. Generally, you must set the-XMS and-xmx options to the same, and-xmn to the-xmx value of 1/4.
Solution:
Manually set heap size
Modify tomcat_home/bin/Catalina. BAT and add the following lines to "Echo" using catalina_base: $ catalina_base:
Set java_opts = % java_opts %-server-xms800m-xmx800m-XX: maxnewsize = 256 m
Or modify Catalina. Sh.
Add the following lines to "Echo" using catalina_base: $ catalina_base:
Java_opts = "$ java_opts-server-xms800m-xmx800m-XX: maxnewsize = 256 m"
2. Followed by: Java. Lang. outofmemoryerror: permgen Space
Cause:
The full name of permgen space is permanent generation space, which refers to the permanent storage area of the memory. This memory is mainly used by JVM to store class and meta information, when a class is loaded, it will be placed in the permgen space. It is different from the heap region where the class instance is stored. GC (garbage collection) permgen space will not be cleaned up during the main program running period. Therefore, if your application has a class, the permgen space error may occur. This error is common in the web server pre-processing JSP.
Compile. If your web app uses a large number of third-party jar files and the size exceeds the default JVM size (4 MB), this error message is generated.
Solution:
1. manually set maxpermsize
Modify tomcat_home/bin/Catalina. BAT (Catalina. Sh in Linux) and add the following lines to "Echo" using catalina_base: $ catalina_base:
Set java_opts = % java_opts %-server-XX: permsize = 128 M-XX: maxpermsize = 512 m
Catalina. sh:
Java_opts = "$ java_opts-server-XX: permsize = 128 M-XX: maxpermsize = 512 M"
I also saw another post and thought it was quite good. The excerpt is as follows:
Analysis of Java. Lang. outofmemoryerror: permgen Space
Many people found that the problem was caused by spring, hibernate, and tomcat. They dynamically generated classes, resulting in permanent Heap Overflow in JVM. There are different solutions. Some people say that upgrading tomcat to the latest version does not even require tomcat. Some people suspect that the issue of spring is very intense in the spring Forum, because the use of cblib in spring in AOP will dynamically generate many classes.
But the question is, why is the open source of these trump cards having the same problem? Is it a more basic reason? Tomcat answers this question in a vague manner in Q & A. We know this question, but this question is generated by a more basic question.
So someone checked the more basic JVM and found the key to the problem. In the past, sun's JVM divided the memory into different zones, one of which is the permenter area used to store a lot of classes and class descriptions. Originally sun thought that this region was fixed during JVM startup, but he did not expect that the current dynamics would be so widely used. In addition, this region has a special garbage collection mechanism. The problem now is that GC cannot be recycled after classes are dynamically loaded to this region!
For the above two problems, my solution is:
Add the following in the first line of Catalina. BAT:
Set java_opts =-xms64m-xmx256m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m
Add the following in the first line of Catalina. sh:
Java_opts =-xms64m-xmx256m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m
Else ---------------------------------------------------------------------------------------------------------------------------------
Permgen space stands for permanent generation space, which refers to the permanent storage area of the memory. This problem is often encountered when Sun JDK + Tomcat 5.5.20 is running the service, after the server runs for a few days, it will crash and report Java. lang. outofmemoryerror: permgen space exception.
Many people found that the problem was caused by spring, hibernate, and tomcat. They dynamically generated classes, resulting in permanent Heap Overflow in JVM. There are different solutions. Some people say that upgrading tomcat to the latest version does not even require tomcat. Some people suspect that the issue of spring is very intense in the spring Forum, because the use of cblib in spring in AOP will dynamically generate many classes.
But the question is, why is the open source of these trump cards having the same problem? Is it a more basic reason? Tomcat answers this question in a vague manner in Q & A. We know this question, but this question is generated by a more basic question.
So someone checked the more basic JVM and found the key to the problem. In the past, sun's JVM divided the memory into different zones, one of which is the permenter area used to store a lot of classes and class descriptions. Originally sun thought that this region was fixed during JVM startup, but he did not expect that the current dynamics would be so widely used. In addition, this region has a special garbage collection mechanism. The problem now is that the permsize is only 4 MB by default, and GC cannot be recycled after the class is dynamically loaded to this region, this eventually leads to insufficient use! This error is common when the web server pre-compile the JSP.
Solution: manually set maxpermsize and modify tomcat_home/bin/Catalina. SH: Add the following lines to "Echo" using catalina_base: $ catalina_base ": java_opts ="-server-XX: permsize = 64 m-XX: maxpermsize = 128 M ", we recommend that you move the same third-party jar file to the tomcat/shared/lib directory to reduce
Jar files occupy the memory repeatedly. This is a temporary solution. To solve this problem, you must use sun's JDK instead of BEA's jrokit.
In addition, if Java. Lang. outofmemoryerror: Java heap Space
The heap size setting of JVM heap refers to the setting of memory space that can be provisioned by JVM during Java program running. the JVM automatically sets the heap size value at startup. The initial space (-XMS) is 1/64 of the physical memory, and the maximum space (-xmx) is 1/4 of the physical memory. You can use options such as-xmn-XMS-xmx provided by JVM to set the configuration. The heap size is the sum of young generation and tenured generaion. Tip: if 98% is used for GC and the available heap size is less than 2% in JVM, this exception is thrown. Tip: heap
The size should not exceed 80% of the available physical memory. Generally, you must set the-XMS and-xmx options to the same, and-xmn is the-xmx value of 1/4.
Solution: manually set heap size
Modify tomcat_home/bin/Catalina. Sh
Add the following lines to "Echo" using catalina_base: $ catalina_base:
Java_opts = "-server-xms800m-xmx800m-XX: maxnewsize = 256 m"
The following provides a reference for setting Java JVM parameters in a 1 GB memory environment:
Java_opts = "-server-xms800m-xmx800m-XX: permsize = 64 m-XX: maxnewsize = 256 m-XX: maxpermsize = 128 M-djava. AWT. Headless = true"
Test in Linux (4 GB memory)
Java_opts = "-server-xms2560m-xmx2560m-XX: permsize = 256 m-XX: maxnewsize = 800 m-XX: maxpermsize = 512 M-djava. AWT. Headless = true"