Tomcat memory overflow reasons
Bad tomcat memory settings in a production environment is prone to memory overflow. Cause memory overflow is not the same, of course, not the same approach.
Here is a summary of the usually encountered and related information. The common general there will be the following three conditions:
1.OutOfMemoryError: Java heap space
2.OutOfMemoryError: PermGen space
3.OutOfMemoryError: unable to create new native thread.
Tomcat memory overflow solution
For the first two cases, the application itself is not a memory leak tomcat jvm parameters settings to solve. (-Xms-Xmx-XX: PermSize-XX: MaxPermSize)
Finally, one may need to adjust the operating system and the tomcat jvm parameters adjusted simultaneously in order to achieve their goals.
The first: a heap overflow.
Analysis:
JVM heap setting is the java program is running in the JVM memory space deployment settings. JVM startup automatically set Heap size value, the initial space (ie-Xms) is the physical memory of 1/64 maximum space (-Xmx) is 1/4 of the physical memory. JVM can be used to provide the-Xmn-Xms-Xmx and other options can be set. The Heap size size is the Young Generation and the Tenured Generaion and.
If 98% of the time for the JVM GC and Heap size available for less than 2% of the time will throw this exception.
Heap Size should not exceed 80% of the available physical memory, the To-Xms and-Xmx option set to the same-Xmn 1/4-Xmx value.
Memory leak case, adjust-Xms-Xmx parameter can be resolved.
-Xms: Initial heap size
-Xmx: maximum heap size
But the impact of the size of the heap by the following three aspects:
A data model of the operating system (32-bt or 64-bit) limit; (32-bit systems, the general restrictions in 1.5G and 2G; 2003 server system (physical memory: 4G and 6G, jdk: 1.6 ) test 1612M, 64-bit operating system, unlimited memory.)
System available virtual memory limit;
3. System available physical memory limit.
Heap size of java-Xmx *** M version command can be used to test. Support jdk version number will appear, does not support error.
Better-Xms-Xmx generally configured as such set JAVA_OPTS =-Xms1024m-Xmx1024m
Its initial space (ie-Xms) is the physical memory of 1/64, the largest space (-Xmx) is 1/4 of the physical memory. JVM can be used to provide the-Xmn-Xms-Xmx options
Set
Instance, the reference given below 1G memory environment java jvm parameter settings:
JAVA_OPTS = “-server-Xms800m-Xmx800m-XX: PermSize = 64M-XX: MaxNewSize = 256m-XX: MaxPermSize = 128m-Djava.awt.headless = true”
JAVA_OPTS = “-server-Xms768m-Xmx768m-XX: PermSize = 128m-XX: MaxPermSize = 256m-XX:
NewSize = 192m-XX: MaxNewSize = 384m “
CATALINA_OPTS = “-server-Xms768m-Xmx768m-XX: PermSize = 128m-XX: MaxPermSize = 256m
-XX: NewSize = 192m-XX: MaxNewSize = 384m “
Server 1G memory: JAVA_OPTS = “-server-Xms800m-Xmx800m-XX: PermSize = 64M-XX: MaxNewSize = 256m-XX: MaxPermSize = 128m-Djava.awt.headless = true”
Server 64, the 2G memory: JAVA_OPTS = ‘-server-Xms1024m-Xmx1536m-XX: PermSize = 128M-XX: MaxNewSize = 256m-XX: MaxPermSize = 256m’
——————- Solution 1: ————————— –
Premise: perform startup.bat start tomcat
Linux server:
Catalina.sh under the the / usr/local/apache-tomcat-5.5.23/bin directory
Add: JAVA_OPTS = ‘-Xms512m-Xmx1024m’
Or JAVA_OPTS = “-server-Xms800m-Xmx800m-XX: MaxNewSize = 256m”
Or the CATALINA_OPTS = “-server-Xms256m-Xmx300m”
Windows Server:
Join in catalina.bat the front
set JAVA_OPTS =-Xms128m-Xmx350m
Or set CATALINA_OPTS =-Xmx300M-Xms256M
(The difference is a direct set jvm memory, another set tomcat memory and JAVA_OPTS, CATALINA_OPTS seems to be the indiscriminate use)
Basic Parameter Description
-Client,-server
These two parameters are used to set up virtual machines which run mode must be used as the first parameter, client mode is enabled faster runtime performance and memory management less efficient than server mode, usually for the client application. Instead, server mode starts slower than the client, but the availability of higher operating performance.
On the windows, type the default virtual machine client mode, if you want to use the server mode, you need to start a virtual machine add-server parameters in order to obtain a more high-performance, server-side applications, server mode is recommended, especially Multiple CPU system. Linux, Solaris server mode by default.
In addition, in the multi-cup, proposed to use the server mode
-Xms <size>
Set up a virtual machine available memory of the initial size of the heap, the default unit of bytes, the size of an integer multiple of 1024 and greater than 1MB, k (K) or m (M) is a unit available to set up a large number of memory . The initial heap size is 2MB. Plus “m” MB, or is KB.
For example:-Xms6400K,-Xms256M
-Xmx <size>
Set the maximum available size of the virtual machine, the default unit is bytes. The value must be 1024 integer multiple of, and greater than 2MB. Available k (K) or m (M) to set up a large number of memory units. The default heap maximum of 64MB.
For example:-Xmx81920K,-Xmx80M
When the application for large memory to run virtual machine throws a java.lang.OutOfMemoryError: Java heap space error, you need to use the-Xmx settings available memory heap.
PermSize / MaxPermSize: definition of Perm segment size, ie permanent preservation of the size of the area, PermSize JVM startup memory size initialization Perm; MaxPermSize maximum memory size occupied Perm. These two values are set to the same general user production environment, in order to reduce the overhead spent during the operation of the system in the memory application.
If startup.bat start tomcat, OK settings to take effect. Successful enough allocated 200M memory.
——————- Solution 2: ————————
Premise: perform startup.bat start tomcat
Manually set the Heap size
Windows Server:
Modify the TOMCAT_HOME / bin / catalina.bat, in the “echo” Using CATALINA_BASE: $ CATALINA_BASE “” above, add the following line:
Java code
set JAVA_OPTS =% JAVA_OPTS%-server-Xms800m-Xmx800m-XX: MaxNewSize = 256m
Note: JAVA_OPTS retain the previous settings.
Linux server:
Modify the TOMCAT_HOME / bin / catalina.sh
In “echo” Using CATALINA_BASE: $ CATALINA_BASE “” above, add the following line:
JAVA_OPTS = “$ JAVA_OPTS-server-Xms800m-Xmx800m-XX: MaxNewSize = 256m”
Note: $ JAVA_OPTS retain the previous settings.
——————- Solution 3: ————————— –
Premise: system services for the implementation of the windows start tomcat
But if not execute startup.bat start tomcat but to use the windows system services start tomcat service, the above settings will not take effect,
That set JAVA_OPTS =-Xms128m-Xmx350m did not work. The above allocation 200M memory on OOM ..
The windows services performed the bin \ tomcat.exe. reads registry value, and not catalina.bat settings.
The solution:
Modify the registry HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ Tomcat Service Manager \ Tomcat5 \ Parameters \ JavaOptions
Cost for
-Dcatalina.home = “C: \ ApacheGroup \ Tomcat 5.0”
-Djava.endorsed.dirs = “C: \ ApacheGroup \ Tomcat 5.0 \ common \ endorsed”
-Xrs
Join-Xms300m-Xmx350m
Restart the tomcat service, set to take effect
——————- Solution 4: ————————— –
Premise: system services for the implementation of the windows start tomcat
Install tomcat if so check the “NT Service (NT/2000/XP only)”
After the installation is complete there will be a “bin” directory in the installation directory tomcat.exe file
Tomcat’s first service will be turned off
In the command line mode (Run, enter CMD)
Change directory to the bin directory of tomcat
Use the following command to remove the service
tomcat-uninstall “Apache Tomcat 4.1”
Next, write a batch.
Reads as follows
set SERVICENAME = Apache Tomcat 4.1
set CATALINA_HOME = E: \ Tomcat 4.1.24
set CLASSPATH = D: \ j2sdk1.4.1_01 \ lib
set JAVACLASSPATH =% CLASSPATH%
set JAVACLASSPATH =% JAVACLASSPATH%;% CATALINA_HOME% \ bin \ bootstrap.jar
set JAVACLASSPATH =% JAVACLASSPATH%;% CATALINA_HOME% \ common \ lib \ servlet.jar
set JAVACLASSPATH =% JAVACLASSPATH%;% JAVA_HOME% \ lib \ tools.jar
tomcat.exe-install “% SERVICENAME%” “% JAVA_HOME% \ jre \ bin \ server \ jvm.dll”-Djava.class.path = “% JAVACLASSPATH%”-Dcatalina.home = “% CATALINA_HOME%”-Xms512m – Xmx768m-start org.apache.catalina.startup.Bootstrap-params start-stop org.apache.catalina.startup.Bootstrap-params stop-out “% CATALINA_HOME% \ logs \ stdout.log”-err “% CATALINA_HOME% \ logs \ stderr.log “
Note that the last line from tomcat.exe-install! Do not carriage return line feed this line is divided into several paragraphs. Saved in the command line, run this bat file, when you pay attention to the implementation of the “service” window is closed.
The second: permanent preservation area overflow
Analysis:
PermGen space stands for Permanent Generation space, refers to the permanent preservation area of memory, this memory the JVM storage Class and Meta information, Class Loader, will be on the PermGen space, and store instances of the class (Instance) Heap area, GC (Garbage Collection) is not in the main program running to clean up the PermGen space, so if your application very CLASS, it is likely PermGen space error, this error is common in web server JSP to pre compile time. If you WEB APP with a large number of third-party jar size than the the jvm default size (4M) it will produce this error message. Hibernate and spring projects are also very prone to this problem. Might these frameworks dynamically class and jvm the gc will not clean PemGen space over jvm default size (4M) cause a memory overflow.
Recommendation: the same third-party jar files relocated to the tomcat / shared / lib directory, so you can achieve the purpose of reducing jar document repeated memory.
This is a general increase-XX: PermSize-XX: MaxPermSize to solve the problem.
-XX: PermSize initial size of the permanent preservation areas
-XX: PermSize initial maximum permanent preservation areas
This is generally combined with the first use, such as set JAVA_OPTS =-Xms1024m-Xmx1024m-XX: PermSize = 128M-XX: PermSize = 256M
One thing to note: the maximum heap memory java-Xmx *** M version command to test-Xmx and-XX: PermSize and such as system support jvm heap size things 1.5G, that-Xmx1024m-XX: PermSize = 768M is unable to run.
—————– Solution 1: ————————-
Linux server:
Increase in the the catalina.sh first line:
JAVA_OPTS =
-Xms64m
-Xmx256m
-XX: PermSize = 128M
-XX: MaxNewSize = 256m
-XX: MaxPermSize = 256m
Or
In “echo” Using CATALINA_BASE: $ CATALINA_BASE “” above, add the following line:
JAVA_OPTS = “-server-XX: PermSize = 64M-XX: MaxPermSize = 128m
Windows Server:
Increase in the first row of the catalina.bat:
set JAVA_OPTS =-Xms64m-Xmx256m-XX: PermSize = 128M-XX: MaxNewSize = 256m-XX: MaxPermSize = 256m
—————– Solution 2: ————————
Modify TOMCAT_HOME / bin / catalina.bat (Linux catalina.sh) code in Java
“Echo” Using CATALINA_BASE: $ CATALINA_BASE “” above, add the following line:
set JAVA_OPTS =% JAVA_OPTS%-server-XX: PermSize = 128M-XX: MaxPermSize = 512m
“Echo” Using CATALINA_BASE: $ CATALINA_BASE “” above, add the following line:
set JAVA_OPTS =% JAVA_OPTS%-server-XX: PermSize = 128M-XX: MaxPermSize = 512m
The catalina.sh next:
Java code
JAVA_OPTS = “$ JAVA_OPTS-server-XX: PermSize = 128M-XX: MaxPermSize = 512m”
JAVA_OPTS = “$ JAVA_OPTS-server-XX: PermSize = 128M-XX: MaxPermSize = 512m”
Third: Can not create a new thread.
This phenomenon is relatively rare, strange proportion and the jvm with system memory.
Such strange because the JVM has been assigned by the system a lot of memory (such as 1.5G), and it takes up at least half of the available memory. It was found that, in many cases the number of threads allocated to the JVM memory more, then the likelihood of the above error is greater.
Cause Analysis
(Learned from this blog reason: http://hi.baidu.com/hexiong/blog/item/16dc9e518fb10c2542a75b3c.html):
A 32-bit process can use up to 2G of memory available, because another 2G retained operating system. It is assumed that the 1.5G to the JVM, then the remaining 500M available memory. Part of the 500M memory must be used for the loading of the system dll, then really the rest perhaps only 400M, key places now appeared: When you use Java to create a thread, will create a Thread object in the JVM memory but also in the operating system at the same time to create a real physical thread (reference JVM specification), the operating system will create the physical thread in the remaining 400 MB of RAM, rather than created in the memory heap of the JVM 1500M. Jdk1.4 inside, the default stack size is 256KB, but in jdk1.5 inside the default stack size for 1M per thread, therefore, in the remaining 400M of memory available inside most we can only create 400 threads available.
This conclusion came out, in order to create more threads, you must reduce the maximum memory allocated to the JVM. Another approach is to let the the JVM host in your JNI code inside.
Give an estimation formula for the maximum number to be able to create threads:
(MaxProcessMemory – JVMMemory – ReservedOsMemory) / (ThreadStackSize) = Number of threads
For jdk1.5 terms, assuming that the operating system retain 120M memory:
1.5GB JVM: (2GB-1.5Gb-120MB) / (1MB) = ~ 380 threads
1.0GB JVM: (2GB-1.0Gb-120MB) / (1MB) = ~ 880 threads
Have a 2000/XP/2003 inside the boot.ini startup options like :/ PAE / 3G allows users to process maximum memory expansion to 3G when the operating system can only occupy up to 1G of virtual memory. Should be able to let the JVM as create more threads.
Therefore, this case requires a combination of operating system-related adjustment.
So: we need to combine different tomcat memory allocation different diagnosis in order to fundamentally solve the problem.
Detection 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 are said JVM memory usage rather than the operating system’s memory;
maxMemory () method returns the java virtual machine (the process) can structure dug up from the operating system’s memory, in bytes, when running java program, no add-Xmx parameter is 64 trillion, that is maxMemory () returned about 64 * 1024 * 1024 bytes, which is the default java virtual machine the dug memory from the operating system. If you add the-Xmx parameter, will be behind the value of this parameter shall prevail, such as java-cp ClassPath-Xmx512m ClassName, the maximum memory is 512 * 1024 * 0124 bytes.
totalMemory () method returns the java virtual machine has now been dug up from the operating system, memory size, this process is the java virtual machine was occupied all memory. -Xms parameters when running java add java program running process, memory is always slowly dug from the operating system, basically how much digging how much straight dug maxMemory () so far So the totalMemory () is slowly increasing. If you use the-Xms parameter, the program in the startup will be unconditional from the operating system, the number of dig the-Xms back defined memory, and memory used about the same time, and then dig.
freeMemory () What is it, just talked about-Xms parameters when running java add java program running process, memory is always slowly dug from the operating system, is basically how much dig how much, but the java virtual machine will be 100% of the cases a little more digging point, they dug up but no access memory, is actually freeMemory (), so the value is generally the case freeMemory () are very small, but if you run java program using the-Xms, this time because of the program in the start time will be unconditional dug from the operating system, number of memory-Xms defined later, this time, dig around memory may on most of the useless, so this time freeMemory () may be some
——————– Solution ————————–
JVM heap size adjustment
Sun HotSpot 1.4.1 generational collector, the heap is divided into three main domains: a new domain, the old domain, as well as permanent domain. Jvm generated by all the new object in the new domain. Once an object has experienced a certain number of garbage collection cycle, the use of the old domain. Class and method objects are stored in a permanent domain jvm. On the configuration, the permanent domain is an independent domain and is not considered to be part of the heap.
Here’s how to control the size of these domains. Use-Xms and-Xmx control the entire original size or maximum heap.
The following command is to set the initial size to 128M:
java-Xms128m
The-Xmx256m control the size of the new domain, you can use the-XX: NewRatio set up a new domain in the heap proportion.
The following command to set the whole heap 128m, a new domain ratio is set to 3, that the new domain with the old domain ratio of 1:3, a new domain for 1/4 or heap 32M:
java-Xms128m-Xmx128m
-XX: NewRatio = 3-XX: NewSize and-XX: MaxNewsize set the initial value and the maximum value of the new domains.
The following command to set the initial value of the new domain and maximum 64m:
java-Xms256m-Xmx256m-Xmn64m
The permanent domain default size of 4m. When the program is run, jvm will adjust the size of the permanent domain in order to meet their needs. Each adjustment, jvm be heap the once completely garbage collection.
Use the-XX: MaxPerSize flag to take the size increase permanent domain. WebLogic Server application load more often need to increase the maximum value of the permanent domain. When the jvm loaded class time, permanent domain object sharply increased, enabling the jvm continuous adjustment permanent domain size. In order to avoid the adjustment, use the-XX: PerSize flag to set the initial value.
Permanent domain below the initial value is set to 32m, and the maximum value is set to 64m.
java-Xms512m-Xmx512m-Xmn128m-XX: PermSize = 32m-XX: MaxPermSize = 64m
Default, HotSpot copying collector used in the new domain. The domain is generally divided into three parts. The first part of Eden, is used to generate a new object. The other two parts are called drop-in space, when Eden is full, the collector to stop the application, all reachable objects are copied to the current from rescue space, once the current from rescue space is full, the collector put reachable object replication to the current ‘s to rescue space. From and to rescue space swap roles. Objects remain active in rescue space copy, until they get to use, and transferred to the old domain. Use the-XX: SurvivorRatio can control the size of the new domain subspace.
With NewRation like the, SurvivorRation provisions in a rescue domain Eden space ratio. For example, the following command set to the new domain 64m Eden accounted for 32m, each rescue domain each 16m:
java-Xms256m-Xmx256m-Xmn64m-XX: SurvivorRation = 2
As mentioned earlier, by default HotSpot use the new domain copying collector, use the mark on the old domain – Clear – compressed collector. Copying collector in the new domain to use a lot of sense, because the application generates most of the objects are short-lived. Under ideal conditions, all the transition when the objects in and out of the Eden space will be collected. If this is the case, and the object out of the Eden space is a long-life, then theoretically you can immediately they moved into the old domain, to avoid repeated replication in space rescue. However, the application is not suitable for this ideal state, because they have a small part of the long-lived object. It is best to keep these long-lived object and placed in a new domain, because the object than compressed copy a small part of the old domain cheap. To control the replication of new objects in the domain, the available-XX: TargetSurvivorRatio control the relief space ratio (this value is used in a proportion of the set relief space. Such relief sterically 1M, 50 represents the value of the available 500K). The value is a percentage, the default value is 50. When the the larger stack lower sruvivorratio should increase this value to 80-90, in order to make better use of space rescue. With the-XX: maxtenuring threshold upper control limit.
Copy all placed all hope of objects from eden extension to the old domain to the MaxTenuring Threshold can be set to 0. After setting, in fact, no longer use the relief space, and should therefore be SurvivorRatio set to maximum in order to maximize the Eden space, are set as follows:
java …-XX: MaxTenuringThreshold = 0-XX: SurvivorRatio = 50000 …
Garbage collection:
Multi-level garbage collection points, 0 for All (Full) garbage collection, recycling the OLD segment in the garbage; 1 or more for some of the garbage collection, only recycling Young garbage memory overflow usually occurs in the OLD segment or Perm segment after the garbage collection is still free memory space to accommodate the new Java objects.
When a URL is accessed, the the memory application process is as follows:
A. JVM will attempt to initialize a memory area for the relevant Java objects in Eden
B. when Eden space enough, the closing of the application memory. Otherwise, to the next step
C. JVM trying to release all inactive objects in Eden (which belong to one or more advanced garbage collection); after the release of the Eden space is still not enough to put a new object, trying to be an active part of the Eden object into the Survivor District / OLD area
D. Survivor zone is used to as Eden and OLD intermediate exchange region, Survivor zone object will be moved to the Old OLD area space enough, otherwise it will be retained in Survivor District
E. OLD area is not enough space, JVM in OLD area full of garbage collection (0)
F. totally garbage collection, Survivor and OLD District still unable to store part of the object copied from Eden, the JVM is not in the Eden District for the new object to create a memory region, “out of memory error”
Java heap parameters:
ms / mx: defined YOUNG + OLD segment size, ms JVM startup YOUNG + OLD memory size; mx occupied YOUNG + OLD memory size. These two values are set to the same general user production environment, in order to reduce the overhead spent during the operation of the system in the memory application.
NewSize / MaxNewSize: size of defined YOUNG segment, YOUNG NewSize JVM startup memory size a; MaxNewSize of occupied YOUNG memory size. These two values are set to the same general user production environment, in order to reduce the overhead spent during the operation of the system in the memory application.
PermSize / MaxPermSize: defined Perm segment size, PermSize the JVM startup Perm memory size; MaxPermSize maximum memory size occupied Perm. These two values are set to the same general user production environment, in order to reduce the overhead spent during the operation of the system in the memory application.
SurvivorRatio: set the proportion of the Survivor space and the Eden space
Example:
MEM_ARGS = “-Xms512m-Xmx512m-XX: NewSize = 256m-XX: MaxNewSize = 256m-XX: PermSize = 128m-XX: MaxPermSize = 128m-XX: SurvivorRatio = 6”
In the above example:
YOUNG + OLD: 512M
YOUNG: 256M
Perm: 128M
Eden: YOUNG * 6 / (6 +1 +1) = 192M
Survivor: YOUNG / (6 +1 +1) = 32M
The total size of the Java heap = YOUNG + OLD + Perm = 640M
Recent Comments