Tomcat memory overflow analysis and solution

The JVM manages two types of memory, heap and non-heap. The heap is for developers. As mentioned above, it is created when the JVM starts; the non-heap is reserved for the JVM itself to store class information. Unlike the heap, the GC does not free space during runtime. 

1. Types of memory overflow 1. java.lang.OutOfMemoryError: PermGen space  JVM manages two types of memory, heap and non-heap. The heap is for developers. As mentioned above, it is created when the JVM starts; the non-heap is reserved for the JVM itself to store class information. Unlike the heap, the GC does not free space during runtime. If the web app uses a large number of third-party jars or the application has too many class files and the MaxPermSize is set to a small value, it will also cause excessive memory usage and overflow, or the tomcat will not clear the front during hot deployment. The loaded environment will only change the context to the newly deployed one, and there will be more and more non-heaped content. The full name of PermGen space is Permanent Generation space, which refers to the permanent storage area of ​​memory. This memory is mainly used by JVM to store Class and Meta information. When Class is loaded by Loader, it will be placed in PermGen space, which is the same as the storage class instance. The Heap area of ​​(Instance) is different, and GC (Garbage Collection) will not clean up the PermGen space during the main program runtime, so if your application has a lot of CLASS, it is likely to have a PermGen space error, which is common in When the web server precompiles the JSP. If your WEB APP uses a large number of third-party jars, the size of which exceeds the default size of jvm (4M), then this error message will be generated. An example of the best configuration: (I verified that since using this configuration, there has been no tomcat death)  






set JAVA_OPTS=-Xms800m -Xmx800m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m 
Add the code shown in red in tomcathome/conf/catalina.sh under linux: you can add The memory of tomcat jvm, so that the phenomenon of memory overflow is not easy to occur! 
# ----- Execute The Requested Command ---------------------------------------- - 
JAVA_OPTS="-server -Xms512m -Xmx2048m -XX:PermSize=128m -XX:MaxNewSize=256m -XX:MaxPermSize=256m" 
# Bugzilla 37848: only output this if we have a TTY 
2, java.lang.OutOfMemoryError: Javaheap
The first case of space  is a supplement, and the main problem is in this case. The default space (ie -Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) is 1/4 of the physical memory. If the remaining memory is less than 40%, the JVM will increase the heap to the value set by Xmx, and if the remaining memory exceeds 70%, the JVM will reduce the heap to the value set by Xms. Therefore, the Xmx and Xms settings of the server should generally be set the same to avoid adjusting the size of the virtual machine heap after each GC. Assuming that the physical memory is infinite, the maximum value of the JVM memory is related to the operating system. Generally, the 32-bit machine is between 1.5g and 3g, while the 64-bit machine has no limit. 

Notice:If Xms exceeds the Xmx value, or the sum of the heap maximum and non-heap maximum exceeds the physical memory or the maximum limit of the operating system, the server will not start. 

The role of garbage collection GC The frequency of JVM calling GC is still very high. There are two main cases for garbage collection:  when the application thread is idle; the other is when the java memory heap is insufficient, and the GC will be called continuously. When there is no problem of insufficient memory heap, an out of memory error will be reported. Because this exception is determined according to the system operating environment, it is impossible to predict when it will appear. According to the GC mechanism, the operation of the program will cause changes in the system operating environment, increasing the chance of GC triggering. To avoid these problems, programs should be designed and written to avoid the memory footprint of garbage objects and the overhead of GC. The display call System.GC() can only suggest that the JVM needs to recycle garbage objects in memory, but it does not have to be recycled immediately.  One is that it cannot solve the situation of empty memory resources, and it will also increase the consumption of GC. 2. The composition of JVM memory area In short, the heap and stack in  java java divides memory into two types: one is stack memory and the other is heap memory  1. Basic type variables defined in functions and reference variables of objects are both Allocate in the stack memory of the function;  2. The heap memory is used to store objects and arrays created by new. When  a variable is defined in a function (code block), java allocates memory space for the variable in the stack. After the scope, java will automatically release the memory space allocated for the variable; the memory allocated in the heap is managed by the automatic garbage collector of the java virtual machine.  The advantage of the heap is that the memory size can be dynamically allocated, and the lifetime does not need to be in advance. Tell the compiler because it allocates memory dynamically at runtime. The disadvantage is that the memory needs to be dynamically allocated at runtime, and the access speed is slow;  






 






栈的优势是存取速度比堆要快,缺点是存在栈中的数据大小与生存期必须是确定的无灵活性。 

java堆分为三个区:New、Old和Permanent 
GC有两个线程: 
新创建的对象被分配到New区,当该区被填满时会被GC辅助线程移到Old区,当Old区也填满了会触发GC主线程遍历堆内存里的所有对象。Old区的大小等于Xmx减去-Xmn 
java栈存放 
栈调整:参数有+UseDefaultStackSize -Xss256K,表示每个线程可申请256k的栈空间 
每个线程都有他自己的Stack 

三、JVM如何设置虚拟内存 
提示:在JVM中如果98%的时间是用于GC且可用的Heap size 不足2%的时候将抛出此异常信息。 
提示:Heap Size 最大不要超过可用物理内存的80%,一般的要将-Xms和-Xmx选项设置为相同,而-Xmn为1/4的-Xmx值。 
提示:JVM初始分配的内存由-Xms指定,默认是物理内存的1/64;JVM最大分配的内存由-Xmx指定,默认是物理内存的1/4。 
默认空余堆内存小于40%时,JVM就会增大堆直到-Xmx的最大限制;空余堆内存大于70%时,JVM会减少堆直到-Xms的最小限制。因此服务器一般设置-Xms、-Xmx相等以避免在每次GC 后调整堆的大小。 
提示:假设物理内存无限大的话,JVM内存的最大值跟操作系统有很大的关系。 
简单的说就32位处理器虽然可控内存空间有4GB,但是具体的操作系统会给一个限制, 
这个限制一般是2GB-3GB(一般来说Windows系统下为1.5G-2G,Linux系统下为2G-3G),而64bit以上的处理器就不会有限制了 

注意:如果Xms超过了Xmx值,或者堆最大值和非堆最大值的总和超过了物理内存或者操作系统的最大限制都会引起服务器启动不起来。 

提示:设置NewSize、MaxNewSize相等,"new"的大小最好不要大于"old"的一半,原因是old区如果不够大会频繁的触发"主" GC ,大大降低了性能 
JVM使用-XX:PermSize设置非堆内存初始值,默认是物理内存的1/64; 
由XX:MaxPermSize设置最大非堆内存的大小,默认是物理内存的1/4。 
解决方法:手动设置Heap size 
修改TOMCAT_HOME/bin/catalina.bat 
在“echo "Using CATALINA_BASE: $CATALINA_BASE"”上面加入以下行: 
JAVA_OPTS="-server -Xms800m -Xmx800m -XX:MaxNewSize=256m" 

四、性能检查工具使用 
定位内存泄漏: 
JProfiler工具主要用于检查和跟踪系统(限于Java开发的)的性能。JProfiler可以通过时时的监控系统的内存使用情况,随时监视垃圾回收,线程运行状况等手段,从而很好的监视JVM运行情况及其性能。 
1. 应用服务器内存长期不合理占用,内存经常处于高位占用,很难回收到低位。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327094140&siteId=291194637