Write in front
As the system concurrency increases, the memory occupied by Tomcat will become larger and larger. If the memory management of Tomcat is improper, it may cause the problem of Tomcat memory overflow. So, how to prevent Tomcat memory overflow? Let's discuss this issue together today.
Preventing Tomcat memory overflow can be summarized into two schemes: one is to set the initial memory for Tomcat startup, and the other is to prevent JVM memory overflow used by Tomcat. Next, we will make a brief introduction to these two programs.
Set initial boot memory
The initial space (ie -Xms) is 1/64 of physical memory, and the maximum space (-Xmx) is 1/4 of physical memory. It can be set by using options such as -Xmn -Xms -Xmx provided by JVM.
Examples
The parameter setting reference of java jvm in 1G memory environment is given below:
JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=64M -XX:MaxNewSize=256m -XX:MaxPermSize=128m -Djava.awt.headless=true "
JAVA_OPTS="-server -Xms768m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256m -XX:NewSize=192m -XX:MaxNewSize=384m"
CATALINA_OPTS="-server -Xms768m -Xmx768m -XX:PermSize=128m -XX:MaxPermSize=256m -XX:NewSize=192m -XX:MaxNewSize=384m"
Linux
In the catalina.sh file in the /usr/local/apache-tomcat-7.0/bin directory, add: JAVA_OPTS = '-Xms512m -Xmx1024m', add "m" to indicate MB, otherwise it is KB, start tomcat It will report insufficient memory.
- -Xms: initial value
- -Xmx: maximum value
- -Xmn: minimum value
Windows
Add set JAVA_OPTS = -Xms128m -Xmx350m at the front of catalina.bat. If you start tomcat with startup.bat, OK settings take effect. It is enough to allocate 200M memory successfully. But if you do not execute startup.bat to start tomcat but use the Windows system service to start tomcat service, the above setting will not take effect, that is, set JAVA_OPTS = -Xms128m -Xmx350m does not work. The above allocation of 200M memory is OOM. .
The windows service executes bin \ tomcat.exe. It reads the value in the registry, not the setting of catalina.bat.
Solution
Modify the registry
HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Tomcat Service Manager\Tomcat5\Parameters\JavaOptions
Original value
-Dcatalina.home="C:\ApacheGroup\Tomcat 7.0"
-Djava.endorsed.dirs="C:\ApacheGroup\Tomcat 7.0\common\endorsed"
-Xrs
Join -Xms300m -Xmx350m to
restart tomcat service, the settings take effect.
Prevent memory overflow of used JVM
1.java.lang.OutOfMemoryError: Java heap space
Explanation
Heap size setting
The JVM heap setting refers to the setting of the memory space that can be used by the JVM during the running of the Java program. The JVM will automatically set the Heap size value at startup. Its initial space (ie -Xms) is 1/64 of the physical memory. The maximum space (-Xmx) is 1/4 of physical memory. It can be set by using options such as -Xmn -Xms -Xmx provided by JVM. Heap size is the sum of Young Generation and Tenured Generaion.
Tip: If 98% of the time is used for GC in the JVM and the available Heap size is less than 2%, this exception message will be thrown.
Tip: The maximum size of Heap Size should not exceed 80% of the available physical memory. Generally, the -Xms and -Xmx options should be set to the same, and -Xmn is the 1/4 -Xmx value.
Solution
Set Heap size manually
Modify TOMCAT_HOME / bin / catalina.bat and add the following code above "echo" Using CATALINA_BASE: $ CATALINA_BASE "".
set JAVA_OPTS=%JAVA_OPTS% -server -Xms800m -Xmx800m -XX:MaxNewSize=256m
set JAVA_OPTS=%JAVA_OPTS% -server -Xms800m -Xmx800m -XX:MaxNewSize=256m
Or modify catalina.sh
Add the following line above "echo" Using CATALINA_BASE: $ CATALINA_BASE "":
JAVA_OPTS="$JAVA_OPTS -server -Xms800m -Xmx800m -XX:MaxNewSize=256m"
2.java.lang.OutOfMemoryError: PermGen space
the reason
The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of memory. This memory is mainly stored by the JVM to store Class and Meta information. When Class is loaded by Loader, it will be placed in PermGen space. (Instance) Heap area is different, GC (Garbage Collection) will not clean the PermGen space during the main program runtime, so if your application has a very CLASS, it is likely to appear PermGen space error, this error is common in When the web server precompiles the JSP. If you use a lot of third-party jars under your WEB APP, the size of which exceeds the jvm default size (4M) then this error message will be generated.
Solution
Manually set the MaxPermSize size
Modify TOMCAT_HOME / bin / catalina.bat (catalina.sh under Linux),
add the following line above the code "echo" Using CATALINA_BASE: $ CATALINA_BASE "":
set JAVA_OPTS=%JAVA_OPTS% -server -XX:PermSize=128M -XX:MaxPermSize=512m
Add the following line to "echo" Using CATALINA_BASE: $ CATALINA_BASE "":
set JAVA_OPTS=%JAVA_OPTS% -server -XX:PermSize=128M -XX:MaxPermSize=512m
The modification of the catalina.sh file is as follows.
Java code
JAVA_OPTS="$JAVA_OPTS -server -XX:PermSize=128M -XX:MaxPermSize=512m"
3.分析java.lang.OutOfMemoryError: PermGen space
I found that many people attribute the problem to: spring, hibernate, tomcat, because they dynamically generate classes, causing the permanent heap in the JVM to overflow. Then there are many different solutions. Some people say that upgrading tomcat version to the latest or even not using tomcat. Others doubt the problem of spring, and the discussion on the spring forum is very intense, because spring uses CBLIB when AOP generates many classes dynamically.
But the question is why the same problem occurs in these ace open source, is it a more basic reason? tomcat answered this vaguely in Q & A. We know this problem, but this problem is caused by a more basic problem.
So someone checked the more basic JVM and found the key to the problem. It turns out that the Sun's JVM divides the memory into different areas, one of which is the permenter area used to store the most used classes and class descriptions. Originally, when SUN was designing, this area was fixed when the JVM was started, but he did not expect that dynamics would be used so widely now. And there is a special garbage collection mechanism in this area, the problem now is that after dynamically loading classes into this area, gc can't recycle at all!
For the above two problems, my treatment is:
In the first line of catalina.bat add:
set JAVA_OPTS=-Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m
Add in the first line of catalina.sh:
JAVA_OPTS= -Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m
Write at the end
If you find the article helpful to you, please search and follow the WeChat account of " Binghe Technology " on WeChat, and learn high-concurrency programming techniques with Binghe.
Finally, attach the core skills knowledge map that concurrent programming needs to master. I wish you all to avoid detours when learning concurrent programming.