Tomcat memory overflow under Linux

Memory overflow means that there is memory that cannot be reclaimed or too much memory is used in the application system, and eventually the memory used by the program to run is larger than the maximum memory that the virtual machine can provide.

There are many reasons for memory overflow , and the common ones are as follows:
  1. The amount of data loaded in memory is too large, such as fetching too much data from the database at one time;
  2. There are references to objects in the collection class, after use If it is not emptied, the JVM cannot be recycled;
  3. There is an infinite loop in the code or there are too many repetitive object entities in the code;
  4. BUG in the third-party software used;
  5. The memory value of the startup parameter is set too small;

The solution to memory overflow : The
first step is to modify the JVM startup parameters to directly increase the memory. (-Xms, -Xmx parameters must not forget to add.)


The second step is to check the error log to see if there are other exceptions or errors before the "OutOfMemory" error.

The third step is to walk through and analyze the code to find out where memory overflows may occur.

Focus on the following points:
1. Check whether there is a query to obtain all data at one time in the database query. Generally speaking, if 100,000 records are fetched into memory at a time, it may cause memory overflow. This problem is relatively hidden. Before going online, there is less data in the database, and it is not easy to cause problems. After going online, there is a lot of data in the database, and a single query may cause memory overflow. Therefore, for database queries, try to use paging methods to query.

2. Check the code for infinite loops or recursive calls.

3. Check whether there is a large loop repeatedly generating new object entities.

4. Check whether there is a query to obtain all data at one time in the database query. Generally speaking, if 100,000 records are fetched into memory at a time, it may cause memory overflow. This problem is relatively hidden. Before going online, there is less data in the database, and it is not easy to cause problems. After going online, there is a lot of data in the database, and a single query may cause memory overflow. Therefore, for database queries, try to use paging methods to query.

5. Check whether the collection objects such as List and MAP are not cleared after use. Collection objects such as List and MAP will always have references to objects, so that these objects cannot be recycled by GC.

The fourth step , use the memory viewing tool to dynamically view the memory usage.




Tomcat itself cannot run directly on the computer and needs to rely on the operating system and a JAVA virtual machine. When a JAVA program starts, the JVM allocates an initial memory and a maximum memory to the APP. When the memory required by the APP exceeds the maximum memory, the virtual machine will prompt a memory overflow and cause the application service to crash.

1. There are three common Java memory overflows:
1. java.lang.OutOfMemoryError: Java heap space, that is, JVM Heap overflow
explanation: JVM will automatically set the value of JVM Heap when it starts, and the setting of JVM heap refers to java The settings of the memory space that the JVM can allocate and use during the running of the program. The default initial space is 1/64 of the physical memory, and the maximum space cannot exceed the physical memory. The JVM provides options such as -Xmn -Xms -Xmx for setting.

Error scenario: In the JVM, if 98% of the time is used for GC and the available Heap size is less than 2%, there will be a JVM Heap overflow
. Solution: Modify the size of the JVM Heap.

2. java.lang.OutOfMemoryError: PermGen space        即PermGen space溢出。 
解释说明:PermGen space是指内存的永久保存区域。这个区域主要存放Class和Meta信息,Class在被Load时就会被放入PermGen space。

出错场景:如果APP载入很多CLASS,就可能会出现PermGen space溢出。(因为sun的GC不会在程序运行时对PermGen space进行清理)。常见在web服务器对JSP进行pre compile的时候 
解决方法:修改MaxPermSize大小

3. java.lang.StackOverflowError                    即栈溢出
解释说明:JVM采用的是栈式的虚拟机,函数的调用过程都体现在堆栈和退栈上。

出错场景:通常栈的大小是1-2MB的,如果调用构造函数的 “层”太多,则会出现栈溢出
解决方法:修改程序

二、Tomcat的JVM内存溢出解决方法
在生产环境中,tomcat内存设置不好很容易出现JVM内存溢,解决方法就是修改Tomcat中的catalina.sh文件。

在catalina.sh文件中,找到cygwin=false,在这一行的前面加入参数,具体如下
# vi TOMCAT_HOME/bin/catalina.sh
JAVA_OPTS="-server -Xms800m -Xmx800m -XX:PermSize=256m -XX:MaxPermSize=512m -XX:MaxNewSize=512m"

其他说明:
1.“m”说明单位是MB,否则默认是KB
2.一般使用物理内存的80%作为堆大小
3.一般把-Xms和-Xmx设为一样大
4.一般把-Xmn设置为-Xmx值的1/4
5.一般将堆的总大小的50%到60%分配给新生成的池

三、jvm参数说明:

-server                一定要作为第一个参数,启用JDK的server版本,在多个CPU时性能佳 
-Xms                    java Heap初始大小。 默认是物理内存的1/64。
-Xmx                    java heap最大值。建议均设为物理内存的80%。不可超过物理内存。
-Xmn                    java heap最小值,一般设置为Xmx的3、4分之一。
-XX:PermSize            设定内存的永久保存区初始大小,缺省值为64M。
-XX:MaxPermSize        设定内存的永久保存区最大大小,缺省值为64M。
-XX:SurvivorRatio=2    生还者池的大小,默认是2。如果垃圾回收变成了瓶颈,您可以尝试定制生成池设置
-XX:NewSize            新生成的池的初始大小。 缺省值为2M。
-XX:MaxNewSize          新生成的池的最大大小。 缺省值为32M。
+XX:AggressiveHeap      让jvm忽略Xmx参数,疯狂地吃完一个G物理内存,再吃尽一个G的swap。 
-Xss                    每个线程的Stack大小
-verbose:gc            现实垃圾收集信息
-Xloggc:gc.log          指定垃圾收集日志文件
-XX:+UseParNewGC        缩短minor收集的时间
-XX:+UseConcMarkSweepGC 缩短major收集的时间
-XX:userParNewGC        可用来设置并行收集(多CPU)
-XX:ParallelGCThreads  可用来增加并行度(多CPU)
-XX:UseParallelGC      设置后可以使用并行清除收集器(多CPU)

RedHat Linux 5.5安装JDK+Tomcat并部署Java项目  http://www.linuxidc.com/Linux/2015-02/113528.htm

Tomcat权威指南(第二版)(中英高清PDF版+带书签)  http://www.linuxidc.com/Linux/2015-02/113062.htm

Tomcat 安全配置与性能优化 http://www.linuxidc.com/Linux/2015-02/113060.htm

Linux下使用Xshell查看Tomcat实时日志中文乱码解决方案 http://www.linuxidc.com/Linux/2015-01/112395.htm

CentOS 64-bit下安装JDK和Tomcat并设置Tomcat开机启动操作步骤 http://www.linuxidc.com/Linux/2015-01/111485.htm

CentOS 6.5下安装Tomcat  http://www.linuxidc.com/Linux/2015-01/111415.htm

Tomcat 的详细介绍请点这里
Tomcat 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-02/114260.htm

出处:https://www.linuxidc.com/Linux/2015-02/114260.htm

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325572845&siteId=291194637