[Reprinted] Tomcat memory overflow causes and debugging

Source: Internet
Author: User
Tags xms

In the production environmentTomcatIf the memory is not set properly, memory overflow may easily occur. The memory causes are different, and the processing methods are also different.

Here we will summarize the situation and relevant information. There are three common cases:
1. outofmemoryerror:Java heap Space
2. outofmemoryerror:Permgen Space
3. outofmemoryerror:Unable to create new Native thread.

TomcatMemory overflow Solution

In the first two cases, you can use the settings when the application itself has no memory leakage.Tomcat JVMParameters. (-XMS-xmx-xx:Permsize-xx:Maxpermsize) The last one may need to adjust the operating system andTomcat JVMParameters can be adjusted at the same time.

First: heap overflow.

in JVM If 98 % is used for GC available heap size insufficient 2 % is thrown.

When no memory leakage occurs, adjust-XMS-xmxParameters can be resolved.
-XMS: Initial heap size
-Xmx: Maximum heap size

However, the heap size is affected by the following three factors:

1. Data Models of related operating systems ( 32-bt Or 64-bit ) Restrictions ;( 32 Generally 1.gbps ~ 2G In 2003 Server System (physical memory: 4G And 6g , JDK : 1.6 ) Test 1612 m , 64 Memory is not limited for the operating system .)
2. Virtual Memory limit available for the system;
3. The system's available physical memory limit.

-XMS-xmxIt is generally better to configure the sameSet java_opts =-xms1024m-xmx1024m

Type 2: Permanently saved region Overflow

Permgen Space The full name is Permanent Generation Space Is the permanent storage area of the memory. This part is used for storage Class And Meta Information, Class When Load Is put Permgen Space Region, IT and storage Instance Of Heap Different regions, GC ( Garbage Collection ) Will notProgramRuntime pair Permgen Space So if your App Yes Load Many Class It is very likely that Permgen Space Error. This error is common in Web Server pair JSP Proceed Pre compile . However Hibernate And Spring This problem also occurs easily in projects. Http://www.javaeye.com/topic/80620 ? Page = 1 This topic is discussed in the post. It may be because these frameworks are dynamic Class And JVM Of GC Yes. Pemgen Space , Resulting in memory overflow.

This generally increases -xx : permsize-xx : maxpermsize Solve the problem.
-xx : permsize permanent storage area initial size
-xx : permsize permanent storage area initial maximum value

This is generally used in combination with the first one, suchSet java_opts =-xms1024m-xmx1024m-xx:Permsize = 128 M-xx:Permsize = 256 m

Note the following: Java-xmx *** M version Command to test the maximum heap memory is -Xmx And -Xx : Permsize Such as the largest supported JVM Things 1.5 GB , That -Xmx1024m-xx : Permsize = 768 m It cannot run.
Third: A New thread cannot be created.

This phenomenon is rare and strange, mainlyJVMIt depends on the proportion of the system memory.

This is becauseJVMThe system has allocated a large amount of memory (for example1.5 GB), And it must occupy at least half of the available memory. Someone found that when there are many threadsJVMThe more memory, the more likely the above errors will occur.

The reasons for this phenomenon are as follows:BlogTo learn the cause:Http://hi.baidu.com/hexiong/blog/item/16dc9e518fb10c2542a75b3c.html):

Every 32 Bit processes can be used at most 2G Memory available, because 2G It is retained by the operating system. Assume that 1.5 GB To JVM , Then the remaining 500 m Available memory. This 500 m Part of the memory must be used by the System DLL Load, then the only thing that really remains is 400 m Now the key point is: When you use Java Create a thread in JVM Will also create Thread Object, but a real physical thread will also be created in the operating system (refer JVM Specification), the operating system will 400 Create this physical thread in the memory instead JVM Of 1500 m . In Jdk1.4 The default stack size is 256kb , But in Jdk1.5 The default stack size is 1 m Every thread, therefore, in the remaining 400 m In the available memory, we can only create at most 400 Available threads.

This is the conclusion. to create more threads, you must reduce the number of threads allocatedJVMMaximum memory. Another way isJVMHost in yourJNICodeInside.

An Estimation Formula for the maximum number of threads that can be created is provided:

(Maxprocessmemory-jvmmemory-reservedosmemory)/(Threadstacksize)= Number of threads

ForJdk1.5Assume that the operating system retains120 mMemory:

1.5 gb jvm : ( 2gb-1.5gb-120mb ) / ( 1 MB ) = ~ 380 threads

1.0 gb jvm : ( 2gb-1.0gb-120mb ) / ( 1 MB ) = ~ 880 threads

In2000/XP/2003OfBoot. iniThere is a startup option in it, which seems to be:/PAE/3GAllows the user process to expand the maximum memory3GAt this time, the operating system can only occupy up1g. This should allowJVMCreate more threads.

Therefore, adjustments must be made based on the operating system.

Therefore, we needTomcatMemory Allocation for different diagnoses can fundamentally solve the problem.

The above isTomcatMemory overflow solutions.

Because it was saved a long time agoArticle, Reprint source can not be determined, the relevant articles can access the http://www.google.com.hk/search? Hl = ZH-tw & source = HP & biw = 1366 & BiH = 641 & Q = Tomcat % E5 % 86% E5 % ad % 85% E6 % Ba % A2 % E5 % 98% ba % E8 % A7 % A3 % E5 % 86% B3 % E6 % 96% B9 % E6 % a1 % 88 + & btng = Google + % E6 % 90% 9C % E5 % B0 % 8b & AQ = F & AQI = & AQL = & OQ =

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.