Solution for Tomcat memory overflow: tomcat Overflow
1. Reasons for Tomcat memory overflow
In the production environment, if Tomcat memory is not properly set, memory overflow may easily occur. Memory overflow is caused by different processing methods.
Here we will summarize the situation and relevant information. There are three common cases:
- OutOfMemoryError: Java heap space
- OutOfMemoryError: PermGen space
- OutOfMemoryError: unable to create new native thread.
Tomcat memory overflow Solution
In the first two cases, you can set the tomcat jvm parameter when the application does not leak memory. (-Xms-Xmx-XX: PermSize-XX: MaxPermSize)
The last possible reason is to adjust the operating system and tomcat jvm parameters at the same time.
2. OutOfMemoryError heap memory overflow
A. Cause Analysis:
JVM heap setting refers to the setting of memory space that JVM can allocate during java program running.
The JVM automatically sets the Heap size value at startup. The initial space (-Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) is 1/4 of the physical memory.
You can use options such as-Xmn-Xms-Xmx provided by JVM to set the configuration. The Heap size is the sum of Young Generation and Tenured Generaion.
This exception is thrown if 98% is used for GC and the available Heap size is less than 2% in JVM.
The Heap Size should not exceed 80% of the available physical memory. Generally, the-Xms and-Xmx options must be set to the same, and-Xmn is the-Xmx value of 1/4.
-Xms: initial heap size
-Xmx: Maximum heap size
However, the heap size is affected by the following three factors:
1. Restrictions on the data model (32-bit or 64-bit) of the relevant operating system; (32-bit systems are generally limited to Gbps ~ 2G; I tested 2003 M in the 1.6 server System (physical memory: 4G and 6G, jdk: 1612). The 64-bit operating system has no restrictions on the memory .)
2. Virtual Memory limit available for the system;
3. Physical memory limit available for the system.
-The Xms-Xmx configuration is usually the same, for example, set JAVA_OPTS =-Xms1024m-Xmx1024m.
The initial space (-Xms) is 1/64 of the physical memory, and the maximum space (-Xmx) is 1/4 of the physical memory. You can use options such as-Xmn-Xms-Xmx provided by JVM to set
B. example. The following provides a reference for setting java jvm parameters in a 1 GB memory environment:
JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: PermSize = 64 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 128 m-Djava. awt. headless = true"
JAVA_OPTS = "-server-Xms768m-Xmx768m-XX: PermSize = 128 m-XX: MaxPermSize = 256 m-XX: NewSize = 192 m-XX: MaxNewSize = 384 m"
CATALINA_OPTS = "-server-Xms768m-Xmx768m-XX: PermSize = 128 m-XX: MaxPermSize = 256m-XX: NewSize = 192 m-XX: MaxNewSize = 384 m"
The server has 1 GB memory: JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: PermSize = 64 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 128 m-Djava. awt. headless = true"
The server is 64-bit, 2 GB memory: JAVA_OPTS = '-server-Xms1024m-Xmx1536m-XX: PermSize = 128 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 256m'
--------------------------------------- Solution 2.1 :------------------------------------------------------
Prerequisite: Execute startup. bat to start tomcat.
Linux Server:
Catalina. sh in the/usr/local/apache-tomcat-5.5.23/bin directory
Add: JAVA_OPTS = '-Xms512m-xmx1024m'
Or JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: MaxNewSize = 256 m"
Or CATALINA_OPTS = "-server-Xms256m-Xmx300m"
Windows Server:
Add at the beginning of catalina. bat
Set JAVA_OPTS =-Xms128m-Xmx350m
Or set CATALINA_OPTS =-Xmx300M-Xms256M
(The difference is that one sets the jvm memory directly, and the other sets the tomcat memory. CATALINA_OPTS seems to be available for use with JAVA_OPTS in a different area)
Basic parameter description
-Client,-server
The two parameters are used to set the running mode used by the Virtual Machine. As the first parameter, the client mode is faster to start, but the runtime performance and memory management efficiency are inferior to the server mode, which is usually used for client applications.
Server mode is slower to start than client, but higher running performance can be achieved.
In windows, the default Virtual Machine Type is client mode. To use server mode, you need to add the-server parameter when starting the virtual machine to achieve higher performance and server-side applications, server mode is recommended, especially for multiple CPU Systems.
In Linux, Solaris adopts the server mode by default.
-Xms <size>
Set the initial size of the available memory heap of the VM. The default unit is byte. The size is an integer multiple of 1024 and must be greater than 1 MB. The available size is k or m (M) to set a large memory size. The initial heap size is 2 MB. "M" indicates that it is MB, otherwise it is KB.
Example:-Xms6400K,-Xms256M
-Xmx <size>
Sets the maximum available size of the VM. The default unit is byte. The value must be an integer multiple of 1024 and greater than 2 MB. You can set a large memory size in the unit of k (K) or m (M. The maximum heap size is 64 MB by default.
Example:-Xmx81920K,-Xmx80M
When the application requests a large memory runtime, the VM throws a java. lang. OutOfMemoryError: Java heap space error, you need to use-Xmx to set a large available memory heap.
PermSize/MaxPermSize: defines the size of the Perm segment, that is, the size of the permanent storage area. PermSize indicates the memory size of the Perm initialized during JVM startup. MaxPermSize indicates the maximum memory size of the Perm.
In the user production environment, these two values are generally set to the same, to reduce the overhead of the system in the memory application during running.
If you start tomcat with startup. bat, the OK setting takes effect. The memory is allocated MB.
-------------------------------------- Solution 2.2 :-----------------------------------------------------
Prerequisite: Execute startup. bat to start tomcat.
Manually set Heap size
Windows Server:
Modify TOMCAT_HOME/bin/catalina. bat and add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
Set JAVA_OPTS = % JAVA_OPTS %-server-Xms800m-Xmx800m-XX: MaxNewSize = 256 m
Note: JAVA_OPTS retains the previous settings.
Linux Server:
Modify TOMCAT_HOME/bin/catalina. sh
Add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
JAVA_OPTS = "$ JAVA_OPTS-server-Xms800m-Xmx800m-XX: MaxNewSize = 256 m"
Note: $ JAVA_OPTS retains the previous settings.
------------------------------------------ Solution 2.3 :--------------------------------------------------
Prerequisite: You can run windows system services to start tomcat.
However, if you do not execute startup. bat to start tomcat, but use the windows System Service to start the tomcat service, the above settings will not take effect,
That is to say, set JAVA_OPTS =-Xms128m-Xmx350m does not work. The above allocated M memory will become OOM ..
The windows service executes bin \ tomcat.exe. It reads the value in the registry, instead of setting catalina. bat.
Solution:
Modify the Registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ Tomcat Service Manager \ Tomcat5 \ Parameters \ JavaOptions
Original Value:
-Dcatalina. home = "C: \ ApacheGroup \ Tomcat 5.0"
-Djava. endorsed. dirs = "C: \ ApacheGroup \ Tomcat 5.0 \ common \ endorsed"
-Xrs
Add-Xms300m-Xmx350m
Restart the tomcat service and the settings take effect.
3. OutOfMemoryError permanent saving area Overflow
A. Cause Analysis:
PermGen space stands for Permanent Generation space, which is the Permanent storage area of the memory.
This memory is mainly used by the JVM to store Class and Meta information. When the Class is loaded, it will be placed in PermGen space. It is different from the Heap region of the Instance,
GC (Garbage Collection) does not clean up the PermGen space during the main program running period. Therefore, if your application has a CLASS, the PermGen space error may occur.
This error is common when the web server pre-compile the JSP. If your web app uses a large number of third-party jar files and the size exceeds the default jvm size (4 MB), this error message is generated.
However, the current hibernate and spring projects are prone to such problems. It may be because these frameworks will dynamically class and the jvm gc will not clean up the PemGen space, exceeding the default jvm size (4 MB), leading to memory overflow.
B. Suggestions:
Move the same third-party jar files to the tomcat/shared/lib directory to reduce the memory usage of jar files.
This is generally solved by increasing-XX: PermSize-XX: MaxPermSize. -XX: PermSize permanent storage region initial size-XX: PermSize permanent storage region initial maximum value
This is generally used in combination with the first one, such as set JAVA_OPTS =-Xms1024m-Xmx1024m-XX: PermSize = 128 M-XX: PermSize = 256 M
--------------------------------------- Solution 3.1 :-----------------------------------------------------
Linux Server:
Add the following in the first line of catalina. sh:
JAVA_OPTS= -Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m
Alternatively, add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
JAVA_OPTS="-server -XX:PermSize=64M -XX:MaxPermSize=128m
Windows Server:
Add the following in the first line of catalina. bat:
set JAVA_OPTS=-Xms64m -Xmx256m -XX:PermSize=128M -XX:MaxNewSize=256m -XX:MaxPermSize=256m
-------------------------------------- Solution 2 :-------------------------------------------------------
Modify TOMCAT_HOME/bin/catalina. bat (catalina. sh in Linux)
Add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
set JAVA_OPTS=%JAVA_OPTS% -server -XX:PermSize=128M -XX:MaxPermSize=512m
Catalina. sh:
JAVA_OPTS="$JAVA_OPTS -server -XX:PermSize=128M -XX:MaxPermSize=512m"
3. OutOfMemoryError: A New thread cannot be created.
This phenomenon is rare and strange, mainly due to the ratio of jvm to system memory.
This is strange because the JVM has been allocated a large amount of memory (such as 1.5 GB) by the system, and it must occupy at least half of the available memory.
When there are many threads, the more memory you allocate to the JVM, the higher the possibility of the above errors.
A. Cause Analysis
Each 32-bit process can use up to 2 GB of available memory, because the other 2 GB is retained by the operating system. Assume that GB memory is used for JVM, then MB of memory is available.
Part of the 400 M memory must be used for system dll loading, so the real rest may only be M.
When you use Java to create a Thread, a Thread object will be created in the JVM memory, but a real physical Thread will also be created in the operating system (refer to JVM specifications ).
The operating system will create this physical thread in the remaining 400 mb of memory, instead of creating it in the JVM's MB memory heap.
In JDK, the default stack size is kb, but in JDK, the default stack size is 1 MB per Thread.
Therefore, we can create a maximum of 400 available threads in the remaining MB of available memory.
This is the conclusion. to create more threads, you must reduce the maximum memory allocated to the JVM. Another way is to host the JVM in your JNI code.
An Estimation Formula for the maximum number of threads that can be created is provided:
(MaxProcessMemory-JVMMemory-ReservedOsMemory)/(ThreadStackSize) = Number of threads
For JDK, assume that the operating system retains MB of memory:
1.5 gb jvm: (2GB-1.5Gb-120MB)/(1 MB) = ~ 380 threads
1.0 gb jvm: (2GB-1.0Gb-120MB)/(1 MB) = ~ 880 threads
Therefore, in this case, you need to adjust the operating system and diagnose tomcat memory allocation based on different situations to fundamentally solve the problem.
Check the current JVM memory usage:
System. out. println ("jvm max memory:" + Runtime. getRuntime (). maxMemory ()/1024/1024 + "M ");
System. out. println ("jvm is using memory:" + Runtime. getRuntime (). totalMemory ()/1024/1024 + "M ");
System. out. println ("jvm is free memory:" + Runtime. getRuntime (). freeMemory ()/1024/1024 + "M ");
These three methods refer to the JVM memory usage rather than the operating system memory:
The maxMemory () method returns the maximum memory that the Java Virtual Machine (this process) can dig from the operating system, in bytes.
If the-Xmx parameter is not added when running the java program, it is 64 MB, that is, maxMemory () returns about 64*1024*1024 bytes, this is the maximum memory that the Java Virtual Machine can dig from the operating system by default.
If the-Xmx parameter is added, the value following this parameter prevails. For example, java-cp ClassPath-Xmx512m ClassName, the maximum memory size is 512*1024*0124 bytes.
The totalMemory () method returns the memory size that the Java Virtual Machine has dug from the operating system, that is, all the memory occupied by the Java Virtual Machine process at that time.
If the-Xms parameter is not added when running java, the memory is always slowly dug from the operating system during the java program running process, basically how much is used to dig, until maxMemory (), totalMemory () increases slowly.
If the-Xms parameter is used, the program will unconditionally dig the memory size defined after-Xms from the operating system at startup, and then when the memory usage is similar, then dig.
What is freeMemory ()? I just mentioned that if the-Xms parameter is not added while running java.
During the java program running process, the memory is always slowly dug from the operating system, basically how much is used to dig.
However, when the Java Virtual Machine is 100%, it will dig a little more. The memory is actually freeMemory ().
Therefore, the value of freeMemory () is usually very small, but if you use-Xms when running java programs.
In this case, when the program is started, it will unconditionally dig the memory size defined after Xms from the operating system. Most of the collected memory may be useless, so freeMemory () may be a little large at this time.
------------------------------------------------ Solution ------------------------------------------------------
JVM heap size adjustment
Sun HotSpot 1.4.1 uses the generational collector, which divides the heap into three main domains: new domain, old domain, and permanent domain.
All new objects generated by Jvm are placed in the new domain. Once an object goes through a certain number of garbage collection cycles, it will get the validity period and enter the old domain.
In the permanent domain, jvm stores class and method objects. In terms of configuration, the permanent domain is an independent domain and is not considered a part of the heap.
The following describes how to control the size of these domains. You can use-Xms and-Xmx to control the original size or maximum value of the entire heap.
The following command sets the initial size to 128 MB:
Java-Xms128m
-Xmx256m: to control the size of the new domain, you can use-XX: NewRatio to set the proportion of the new domain in the heap.
The following command sets the entire heap to 128 m, and the ratio of the new domain to 3, that is, the ratio of the new domain to the old domain is, and the new domain is 1/4 or 32 M of the heap:
Java-Xms128m-Xmx128m
-XX: NewRatio = 3 You can use-XX: NewSize and-XX: MaxNewsize to set the initial value and maximum value of the new domain.
The following command sets the initial value and maximum value of the new domain to 64 MB:
Java-Xms256m-Xmx256m-Xmn64m
The default size of the permanent domain is 4 MB. When the program is running, the jvm will adjust the size of the permanent domain to meet the needs. During each adjustment, the jvm will perform a full garbage collection on the heap.
Use the-XX: MaxPerSize flag to increase the size of the permanent domain. When WebLogic Server Applications load many classes, you often need to increase the maximum value of the permanent domain.
When the jvm loads a class, the object in the permanent domain increases sharply, so that the jvm continuously adjusts the permanent domain size. To avoid adjustment, you can use the-XX: PerSize flag to set the initial value.
Set the initial value of the permanent domain to 32 MB and the maximum value to 64 MB.
Java-Xms512m-Xmx512m-Xmn128m-XX: PermSize = 32 m-XX: MaxPermSize = 64 m