Tomcat itself cannot run directly on the computer, it needs to rely on the operating system and a Java virtual machine. The JVM allocates an initial memory and maximum memory to the program when the Java program starts. The virtual machine prompts for memory overflow when the program needs more memory than the maximum memory, and causes the application service to crash.
One, the common Java memory overflow has the following three kinds:
1. Java.lang.OutOfMemoryError:Java heap space that JVM heap overflow
Explanation: The JVM automatically sets the value of the JVM heap at startup, and the JVM heap setting refers to the set of memory space that the JVM can use to deploy during the Java program run. Its initial space is 1/64 of the physical memory by default, and the maximum space cannot exceed the physical memory. The JVM provides options such as-XMN-XMS-XMX to set up.
Error scenario: In the JVM, JVM heap overflow occurs if 98% of the time is used for GC and the available heap size is less than 2%
Workaround: Modify the size of the JVM heap.
2. Java.lang.OutOfMemoryError:PermGen space is PermGen space overflow
Explanation: PermGen space refers to the permanent storage area of memory. This area primarily holds class and meta information, and class is placed in PermGen space when it is load.
Error scenario: If the app loads a lot of class, there may be a permgen space overflow. (because Sun's GC does not clean up the PermGen space while the program is running) is common when the Web server makes pre compile for JSP
Workaround: Modify the MaxPermSize size
3. Java.lang.StackOverflowError is Stack Overflow
Explanation: The JVM uses a stack-style virtual machine, and the function invocation process is reflected on the stack and the fallback stack.
Error scenarios: Usually the size of the stack is 1-2MB, and if there are too many "layers" to call the constructor, a stack overflow will occur
Workaround: Modify the program
Two, Tomcat's JVM memory overflow solution
In a production environment, the Tomcat memory settings are not good enough for JVM memory overflows, and the solution is to modify the catalina.sh file in Tomcat.
In the catalina.sh file, locate the Cygwin=false, and add the arguments to the front of the line, as follows
# VI tomcat_home/bin/catalina.sh
Java_opts= "-server-xms800m-xmx800m-xx:permsize=256m-xx:maxpermsize=512m-xx:maxnewsize=512m"
Other Notes:
1. "M" description unit is MB, otherwise the default is KB
2. Generally use 80% of physical memory as the heap size
3.-xms and-XMX are generally set to the same size
4. Generally set the-XMN to the-XMX value of 1/4
5. Generally allocate 50% to 60% of the total size of the heap to the newly generated pool
Third, the JVM parameter description:
-server Be sure to enable the server version of JDK as the first parameter, and perform better on multiple CPUs
-xms java heap Initial size. The default is 1/64 of physical memory.
-xmx Java heap maximum value. The recommendation is set to 80% of the physical memory. cannot exceed physical memory.
-xmn Java heap minimum, typically set to one of the 3 and 4 points of xmx.
-xx:permsize sets the initial size of the permanent save area for the memory, with a default value of 64M.
-xx:maxpermsize sets the maximum size of the memory's permanent save area, with a default value of 64M.
The size of the-xx:survivorratio=2 survivor pool is 2 by default. Such as
-xx:newsize the initial size of the newly generated pool. The default value is 2M.
-xx:maxnewsize the maximum size of the newly generated pool. The default value is 32M.
+xx:aggressiveheap let the JVM ignore the XMX parameter, eat a G-physical memory madly, and then eat a G swap.
-XSS the stack size of each thread
-VERBOSE:GC Real Garbage Collection information
-xloggc:gc.log Specify garbage collection log files
-XX:+USEPARNEWGC shorten the minor collection time
-XX:+USECONCMARKSWEEPGC Shorten the major collection time
-XX:USERPARNEWGC can be used to set up parallel collections (multiple CPUs)
-xx:parallelgcthreads can be used to increase the degree of parallelism (multiple CPUs)
-XX:USEPARALLELGC can be used for parallel purge collectors (multiple CPUs) after setting
The above Linux solution to the problem of Tomcat memory overflow is small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.