Tomcat memory overflow Analysis and Solution

Source: Internet
Author: User
Tags jprofiler

JVM manages two types of memory, heap and non-heap. Heap is used by developers. It is created at JVM startup. Non-heap is reserved for JVM and used to store class information. Unlike the heap, GC does not release space during runtime.

I. memory overflow type
1. java. lang. OutOfMemoryError: PermGen space
JVM manages two types of memory, heap and non-heap. Heap is used by developers. It is created at JVM startup. Non-heap is reserved for JVM and used to store class information. Unlike the heap, GC does not release space during runtime. If the web app uses a large number of third-party jar files or the application has too many class files, but the MaxPermSize setting is small, exceeding this will also cause excessive memory usage and overflow, or during hot deployment of tomcat, the Environment loaded above will not be cleaned up, but the context will be changed to a new deployment, so there will be more and more non-heap content.

The full name of PermGen space is Permanent Generation space, which refers to the Permanent storage area of the memory. This memory is mainly used by JVM to store Class and Meta information, when a Class is loaded, it will be placed in the PermGen space. It is different from the Heap region where the Class Instance is stored. GC (Garbage Collection) permGen space is not cleaned up 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.
An example of the best configuration: (I have verified that tomcat has never died since I used this configuration)

Set JAVA_OPTS =-Xms800m-Xmx800m-XX: PermSize = 128 M-XX: MaxNewSize = 256 m-XX: MaxPermSize = 256 m
In linux, add the Code marked in red in tomcathome/conf/catalina. sh: the memory of tomcat jvm can be increased, so that memory overflow is not easy!
# ----- Execute The Requested Command -----------------------------------------
JAVA_OPTS = "-server-Xms512m-Xmx2048m-XX: PermSize = 128 m-XX: MaxNewSize = 256 m-XX: MaxPermSize = 256 m"
# Bugzilla 37848: only output this if we have a TTY
2. java. lang. OutOfMemoryError: Javaheap space
The first case is a supplement. The main problem is that it appears in this case. The default space (-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 Xmx setting value. If the remaining memory exceeds 70%, the JVM will reduce the heap to the Xms setting value. Therefore, the Xmx and Xms settings of the server should be set to the same value to avoid adjusting the size of the VM heap after each GC operation. If the physical memory is infinitely large, the maximum JVM memory size is related to the operating system. Generally, 32-bit machines are between GB and 3 GB, and 64-bit machines are not limited.

Note:If the Xms value exceeds the Xmx value, or the sum of the maximum heap value and the maximum non-heap value exceeds the maximum limit of the physical memory or operating system, the server cannot be started.

Garbage collection GC role
JVM calls GC frequently. Garbage collection is mainly performed in two cases:
When the application thread is idle, and when the java memory heap is insufficient, GC is continuously called. If continuous collection fails to solve the problem of insufficient memory heap, an out of memory error is reported. This exception is determined by the system running environment, so it cannot be expected when it will appear.
According to the GC mechanism, program running may cause changes in the system runtime environment and increase the chance of GC triggering.
To avoid these problems, the program design and writing should avoid the memory usage and GC overhead of spam objects. It is shown that calling System. GC () can only be recommended that JVM recycle junk objects in memory, but not immediately,
One is that it cannot solve the problem of insufficient memory resources and increase GC consumption.

Ii. JVM memory zone Composition
Java stack and stack
Java divides memory into two types: stack memory and heap memory.
1. The basic type variables defined in the function and the referenced variables of the object are allocated in the function stack memory;
2. 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. When the scope of the variable is exceeded, java Automatically releases the memory space allocated for the variable. The memory allocated in the heap is managed by the java Virtual Machine's automatic garbage collector.
The advantage of heap is that the memory size can be dynamically allocated, and the lifetime does not need to be told to the compiler in advance because it dynamically allocates memory at runtime. The disadvantage is that the memory needs to be dynamically allocated at runtime, And the access speed is slow;
The advantage of stack is that the access speed is faster than the heap speed, but the disadvantage is that the data size and lifetime in the stack must be determined without flexibility.

Java heap is divided into three areas: New, Old, and Permanent.
GC has two threads:
The newly created object is allocated to the New area. When the area is filled, it will be moved to the Old area by the GC auxiliary thread, when the Old area is full, the main GC thread is triggered to traverse all objects in the heap memory. The size of the Old area is equal to Xmx minus-Xmn.
Java stack Storage
Stack adjustment: the parameter is + usedefastackstacksize-Xss256K, indicating that each thread can apply for a stack space of kb.
Each thread has its own Stack

Iii. How to Set virtual memory in JVM
Tip: if 98% is used for GC and the available Heap size is less than 2% in JVM, this exception is thrown.
Tip: the Heap Size should not exceed 80% of the available physical memory. Generally, you must set the-Xms and-Xmx options to the same, and-Xmn to the-Xmx value of 1/4.
Tip: the initial memory allocated by JVM is specified by-Xms. The default value is 1/64 of the physical memory. The maximum memory allocated by JVM is specified by-Xmx. The default value is 1/4 of the physical memory.
By default, when the free heap memory is less than 40%, the JVM will increase the heap until the maximum limit of-Xmx. When the free heap memory is greater than 70%, the JVM will reduce the minimum limit of heap until-Xms. Therefore, the server generally sets-Xms and-Xmx to be equal to each other to avoid adjusting the heap size after each GC.
Tip: if the physical memory is infinitely large, the maximum JVM memory has a great relationship with the operating system.
Simply put, although the 32-bit processor has a controllable memory space of 4 GB, the specific operating system will impose a limit,
This limit is generally 2 GB-3 GB (1.5 GB-2 GB in Windows and 2 GB-3 GB in Linux ), the 64-bit and above processors will not be limited.

Note:If the Xms value exceeds the Xmx value, or the sum of the maximum heap value and the maximum non-heap value exceeds the maximum limit of the physical memory or operating system, the server cannot be started.

Tip:When NewSize and MaxNewSize are set to equal values, it is recommended that the size of "new" should not be greater than half of the size of "old", because if the old area does not frequently trigger the "Main" GC, it greatly reduces the performance.
JVM uses-XX: PermSize to set the non-heap memory initial value. The default value is 1/64 of the physical memory;
Set the maximum non-heap memory size by XX: MaxPermSize. The default value is 1/4 of the physical memory.
Solution: manually set Heap size
Modify TOMCAT_HOME/bin/catalina. bat
Add the following lines to "echo" Using CATALINA_BASE: $ CATALINA_BASE:
JAVA_OPTS = "-server-Xms800m-Xmx800m-XX: MaxNewSize = 256 m"

Iv. Use of performance check tools
Locate Memory leakage:
JProfiler is mainly used to check and track the performance of the system (limited to Java Development. JProfiler can monitor the system's memory usage and garbage collection and thread running status at any time to monitor the JVM running status and performance.
1. The memory of the application server is unreasonably occupied for a long time. The memory is often occupied at a high level, which is hard to be recycled to a low level.

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.