linux solves the memory overflow problem of Tomcat

  • 2020-05-14 05:53:29
  • OfStack

Tomcat itself does not run directly on a computer and relies on the operating system and one JAVA virtual machine. When the JAVA program starts, JVM allocates 1 initial memory and the maximum memory to the program. When the program needs more memory than the maximum memory value, the virtual machine will prompt a memory leak and cause the application service to crash.

1. There are three common Java memory leaks:

1. java.lang.OutOfMemoryError: Java heap space JVM Heap overflow

Explanation: JVM will automatically set the JVM Heap value at startup. JVM heap setting refers to the amount of memory space that JVM can allocate during the operation of java program. The default initial space is 1/64 of the physical memory, and the maximum space cannot exceed the physical memory. JVM provides options such as -Xmn-Xms-Xmx to set.

Error scenario: in JVM, if 98% of the time is spent on GC and less than 2% of Heap size is available, an JVM Heap overflow will occur

Solution: modify the size of JVM Heap.

2. java.lang.OutOfMemoryError: PermGen space, PermGen space

Explanation: PermGen space refers to the permanent storage area of memory. This area mainly stores Class and Meta information, and Class will be put into PermGen space when it is Load.

Error scenario: if APP loads a lot of CLASS, an PermGen space overflow may occur. (because GC of sun does not clean PermGen space while the program is running.) it is common to see pre compile web server pre compile JSP

Solution: modify the MaxPermSize size

3. java.lang.StackOverflowError is stack overflow

Explanation: JVM is a stack virtual machine, the function call process is reflected in the stack and the stack.

Error scenario: stack sizes are usually 1-2MB, and stack overflows can occur if too many "layers" are called to the constructor

Solution: modify the program

2. Tomcat's JVM memory overflow solution

In a production environment, a bad tomcat memory setting can easily lead to an JVM memory overflow. The solution is to modify the catalina.sh file in Tomcat.
In the catalina.sh file, find cygwin=false and insert the parameter in front of this line as follows

# vi TOMCAT_HOME/bin/catalina.sh
JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxNewSize=512m"

Other instructions:

1. "m" indicates that the unit is MB, otherwise the default is KB
2.1 generally use 80% of physical memory for heap size
3.1 generally, -Xms and -Xmx are set to be as large as 1
4.1 generally set -Xmn to 1/4 of -Xmx
5.1 generally allocate 50 to 60 percent of the total heap size to the newly generated pool

3. Description of jvm parameters:

-server 1 must be used as the first parameter. JDK version server is enabled, which is good for multiple CPU
-Xms java Heap initial size. The default is 1/64 of physical memory.
-Xmx java heap maximum. It is recommended that they all be set to 80% of physical memory. Cannot exceed physical memory.
-Xmn java heap minimum, 1 is generally set to 3/4 of Xmx.
-XX :PermSize sets the initial size of the permanent storage area of memory. The default value is 64M.
-XX :MaxPermSize sets the maximum size of the permanent storage area of memory, and the default is 64M.
-XX :SurvivorRatio=2 the size of the pool for survivors, default is 2. Such as
-XX: the initial size of NewSize's newly generated pool. The default value is 2M.
-XX: maximum size of MaxNewSize newly generated pool. The default value is 32M.
+XX:AggressiveHeap asks jvm to ignore the Xmx parameter, frantically eat up 1 G physical memory, and then eat up 1 G swap.
-Xss size Stack per thread
-verbose :gc reality garbage collection information
-Xloggc: gc.log specifies the garbage collection log file
-XX :+UseParNewGC reduces the collection time of minor
-XX :+UseConcMarkSweepGC reduces the collection time of major
-XX :userParNewGC can be used to set up parallel collections (multiple CPU)
-XX:ParallelGCThreads can be used to increase parallelism (multiple CPU)
-XX: parallel scavenger collector can be used after UseParallelGC is set (multiple CPU)


Related articles: