Tomcat memory overflow in Linux
Tomcat itself cannot run directly on a computer. It depends on the operating system and a Java virtual machine. When the JAVA program starts, the JVM allocates an initial memory and the maximum memory to the APP. When the memory required by the APP exceeds the maximum memory size, the VM will prompt a memory overflow and cause the application service to crash.
I. There are three common Java memory overflow types:
1. java. lang. OutOfMemoryError: Java heap space (JVM Heap Overflow)
Explanation: The JVM Heap value is automatically set at startup. The JVM Heap setting refers to the setting of memory space that can be provisioned by JVM during java program running. The initial space is 1/64 of the physical memory by default, and the maximum space cannot exceed the physical memory. JVM provides options such as-Xmn-Xms-Xmx for configuration.
Error scenario: In JVM, if 98% of the time is used for GC and the available Heap size is less than 2%, JVM Heap overflow will occur.
Solution: Modify the JVM Heap size.
2. java. lang. OutOfMemoryError: PermGen space, that is, PermGen space overflow.
Explanation: PermGen space refers to the permanent storage area of the memory. This area mainly stores Class and Meta information. When a Class is loaded, it is placed in PermGen space.
Error scenario: If the APP loads many classes, PermGen space overflow may occur. (Because sun's GC will not clean up PermGen space when the program is running ). When the web server pre-compile the JSP
Solution: Modify the MaxPermSize.
3. java. lang. StackOverflowError indicates stack overflow.
Explanation: The JVM uses a stack-type virtual machine. The call process of functions is reflected in the stack and rollback.
Error scenario: the stack size is usually 1-2 MB. If too many "layers" are called for constructor, Stack Overflow may occur.
Solution: modify the program
Ii. Tomcat JVM memory overflow Solution
In the production environment, if tomcat memory is not properly configured, JVM memory overflow may easily occur. The solution is to modify the catalina. sh file in Tomcat.
In the catalina. sh file, locate cygwin = false and add parameters before this line, as shown below:
# Vi TOMCAT_HOME/bin/catalina. sh
JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: PermSize = 256 m-XX: MaxPermSize = 512 m-XX: MaxNewSize = 512 m"
Other Instructions:
1. "m" indicates that the Unit is MB. Otherwise, the default value is KB.
2. Generally, 80% of the physical memory is used as the heap size.
3. Set-Xms to the same size as-Xmx.
4. Set-Xmn to 1/4 of the-Xmx value.
5. allocate 50% to 60% of the total heap size to the newly generated pool.
Iii. jvm parameter description:
-The server must be the first parameter to enable the JDK server version. When multiple CPUs are used, the initial size of the Xms java Heap can be better. The default value is 1/64 of the physical memory. -Maximum value of Xmx java heap. We recommend that you set the value to 80% of the physical memory. The physical memory cannot exceed. -The minimum value of Xmn java heap is generally set to one of the 3 and 4 points of Xmx. -XX: PermSize: Set the initial size of the permanent memory storage area. The default value is 64 MB. -XX: MaxPermSize sets the maximum permanent memory size. The default value is 64 MB. -XX: Required vorratio = 2. The default value is 2. If garbage collection becomes a bottleneck, you can customize the initial size of the newly generated pool-XX: NewSize. The default value is 2 MB. -XX: the maximum size of the newly generated pool in MaxNewSize. The default value is 32 MB. + XX: AggressiveHeap allows the jvm to ignore the Xmx parameter, frantically eat a G physical memory, and then eat a G swap. -Stack size of each Xss thread-verbose: gc actual garbage collection information-Xloggc: gc. log specifies the garbage collection log file-XX: + UseParNewGC to shorten the minor collection time-XX: + UseConcMarkSweepGC to shorten the major collection time-XX: userParNewGC can be used to set parallel collection (multiple CPUs) -XX: ParallelGCThreads can be used to increase the degree of parallelism (multiple CPUs)-XX: UseParallelGC can be used to clear the collector (multiple CPUs) in parallel)