CentOS to modify the JVM non heap memory default configuration in tomcat resolve memory overflow

Source: Internet
Author: User
Tags garbage collection ini memory usage string find centos xms

System CentOS6.4 Yum installed Tomcat6 and jdk1.6, the installation configuration process is as follows:

Http://www.111cn.net/sys/CentOS/72007.htm

The deployment of two projects A and B in Tomcat, as well as the time of memory overflow error in the deployment times, the system CPU load is soaring, and a, B, and only multiple a, or just multiple B-project systems are deployed separately for normal operation.

View Log error: Outofmemoryerror:permgen space ....
Query data learned: Non-heap overflow (persistent save area overflow)
This error is common when the Web server is pre compile the JSP. If your web app uses a large number of third-party jars that are larger than the JVM default size (4M), this error message is generated. If the Web app uses a large number of third-party jars or if the application has too many class files and happens to have a small set of maxpermsize settings, it can cause this memory to overflow too much, or it will not clean up the previously loaded environment when the tomcat is hot deployed. will only change the context to a new deployment, the contents of the non-dump will be more and more.
Workaround: Modify JVM non heap memory default size
The methods available online are:
Modifying Tomcat is a configuration in the catalina.sh file in the bin directory, but Yum installed tomcat the contents of this file is empty, the configuration item java_opts= "-xx:permsize=512m-xx:maxpermsize=1024m "Add catalina.sh file, reboot tomcat, invalid."
Search Java_opts string Find Tomcat's profile location
Find/etc |xargs grep "Java_opts"

  code is as follows copy code

[root@ay1407031 4494954704eZ tomcat6]# find/etc |xargs grep "java_opts"
/etc/tomcat6/tomcat6.conf: #JAVA_OPTS = "-xminf0.1-xmaxf0.3 "
/etc/tomcat6/tomcat6.conf:# use java_opts to set Java.library.path for libtcnative.so
/etc/tomcat6/tomcat6.co NF: #JAVA_OPTS = "-djava.library.path=/usr/lib"
/etc/tomcat6/tomcat6.conf:java_opts= "${java_opts}"- Djavax.sql.datasource.factory=org.apache.commons.dbcp.basicdatasourcefactory "
in/etc/tomcat6/ tomcat6.conf file, add
java_opts= "-xx:permsize=512m-xx:maxpermsize=1024m"

The restart Tomcat project is functioning properly.
PS: The specific setting size can be adjusted according to physical memory and the actual operational requirements of the project.
Yum install Tomcat's default path:/usr/share/tomcat6/
Other relevant paths are as follows: (for reference)
Clipboard
........................................................................................................

Java JVM Memory overflow and reasonable configuration in Tomcat

Tomcat itself cannot run directly on the computer, and relies on a hardware-based operating system and a Java virtual machine. Tomcat's memory overflow is essentially a JVM memory overflow, so at the beginning of this article, you should start with a detailed description of the Java JVM's memory-related knowledge.

Introduction to Java JVM memory

The JVM manages two types of memory, heaps, and not heaps. According to the official statement: "Java virtual machines have a heap, the heap is the Run-time data region, all class instances and arrays of memory are allocated from here." The heap was created when the Java virtual machine was started. "" Memory outside the heap in the JVM is called Non heap memory (non-heap memory). Simply put, the heap is the memory that Java code can have, is reserved for developers, and not heaps are for the JVM to use, so the method area, JVM internal processing or tuning required memory (such as JIT-compiled code caching), each class structure (such as running a constant pool, field and method data) And the code for methods and constructs are in non heap memory, which is different from the heap, and the GC does not release its space during the runtime.

(1). Heap memory allocation
The initial memory allocated by the JVM is specified by-XMS, and the default is physical memory 1/64;JVM the maximum allocated memory is specified by-XMX, which defaults to 1/4 of physical memory. When the default free heap memory is less than 40%, the JVM increases the maximum limit for the heap until-xmx, and when the free heap memory is greater than 70%, the JVM reduces the heap until the minimum limit of-XMS. Therefore, the server generally sets-xms and-xmx equal to avoid resizing the heap after each GC. You can use options such as the-XMN-XMS-XMX provided by the JVM to make heap memory settings, typically to set the-XMS and-XMX options to the same, and-xmn to a-xmx value of 1/4, the recommended heap is set to a maximum of 80% of the maximum available memory.

The size of the initialization heap is the size of the memory that the JVM requests to the system at startup. Generally speaking, this parameter is not important. However, some applications in the case of heavy load will be a sharp use of more memory, at this time this parameter is very important, if the JVM started with a small set of memory, and in this case there are many objects to initialize, the JVM must repeatedly increase memory to meet the use. For this reason, we generally set the-XMS and-xmx to be the same size, while the maximum of the heap is limited by the physical memory used by the system. Typically, applications that use large amounts of data use persistent objects, and memory usage is likely to grow rapidly. The JVM prompts for a memory overflow when the application needs more memory than the maximum of the heap, and causes the application service to crash. Therefore, if the XMS exceeds the XMX value, or if the sum of the heap maximum and non heap maximum exceeds the physical memory or the maximum operating system limit, it will cause the server to start up.

(2). Non-heap memory allocation
Also called a permanently saved area, which holds class and meta information, class is placed in the zone when it is load. Unlike the heap region where the class instance (Instance) is stored, the GC (garbage Collection) does not clean the PermGen space during the main program run time. The JVM uses-xx:permsize to set a non heap memory initial value, which defaults to 1/64 of physical memory, and the maximum amount of non heap memory is set by Xx:maxpermsize, which defaults to 1/4 of physical memory. GC does not clean up the PermGen space, so if your app is going to load a lot of classes, PermGen space error is likely to occur.

(3). JVM memory Limit (maximum)
First, JVM memory is limited to the actual maximum physical memory (nonsense!) , hehe), assuming that physical memory is infinite, the maximum value of JVM memory is very much related to the operating system. Simply put, the 32-bit processor, although controllable memory space has 4GB, the specific operating system will give a limit, which is generally 2GB-3GB (in general, Windows system under the 1.5g-2g,linux system for 2G-3G), And more than 64bit of processors will have no limits.

Description of two or three memory overflow exceptions

1. Outofmemoryerror:java Heap Space Heap Overflow

The main problem with memory overflow is that it appears in this case. This exception information is thrown when 98% of the time in the JVM is for GC and the available HEAP size is less than 2%.

2. Outofmemoryerror:permgen space Non-heap overflow (persistent save area overflow)

This error is common when the Web server is pre compile the JSP. If your web app uses a large number of third-party jars that are larger than the JVM default size (4M), this error message is generated. If the Web app uses a large number of third-party jars or if the application has too many class files and happens to have a small set of maxpermsize settings, it can cause this memory to overflow too much, or it will not clean up the previously loaded environment when the tomcat is hot deployed. will only change the context to a new deployment, the contents of the non-dump will be more and more.

3. Outofmemoryerror:unable to create new native thread. Unable to create new thread

This phenomenon is relatively rare and strange, mainly related to the ratio of JVM to system memory. The strange thing is that the JVM has been allocated a lot of memory (such as 1.5G) by the system, and it consumes at least half of the available memory.

Third, Java JVM memory configuration

1. There are four parameters for JVM memory allocation settings

-xmx Java Heap Maximum, the default value is 1/4 of physical memory;

-xms Java Heap Initial value, the server-side JVM is best to set-XMS and-xmx to the same value, the development of the test machine JVM can retain the default value;

-xmn Java Heap Young area size, not familiar with the best to retain the default value;

-XSS the stack size of each thread, it's best to keep the default value;

-xx:permsize: Set the permanent storage area of memory;

-xx:maxpermsize: Set the maximum memory of the permanent storage area;

-xx:permsize: Set the permanent storage area of memory;

-xx:newsize: Sets the default size of the ' Cenozoic ' for the JVM heap;

-xx:maxnewsize: Sets the maximum size of the ' Cenozoic ' for the JVM heap;

2. How to set memory allocations for the JVM

(1) When the JVM is started and used at a command prompt (only the currently running class Test takes effect):

java-xmx128m-xms64m-xmn32m-xss16m Test

(2) When the JVM is started and used in an integrated development environment (such as Eclipse):

A. Open Eclipse.ini under the Eclipse root, which defaults to (here is the JVM memory allocation for running the current development tool):-vmargs-xms40m-xmx256m-vmargs indicates that the following set parameters for the virtual machine, you can modify the parameter values. You can also add-xmn,-xss, in addition, Eclipse.ini can also be set within the heap memory, such as:-xx:permsize=56m,-xx:maxpermsize=128m.

B. Open eclipse-Window-Preferences-java-installed JRE (effective for Java programs running in the current development environment) edit the currently used JRE, and enter the default VM parameter:-xmx128m-xms64m-xmn32m–xss16m.

C. Turn on Eclipse-run-run the-java application (only for Java classes that are set up) Select the class-argument that you want to set the memory allocation to, and enter in the VM's argument:-xmx128m-xms64m-xmn32m-xss16m Note: If both the B and C settings are in place in the same development environment, the B setting takes effect and the C setting is not valid, for example, if the development environment is set to:-xmx256m, and Class test is set to:-xmx128m-xms64m, the setting that takes effect when you run test is:-xmx256m- xms64m.

(3) When starting and using the JVM in a server environment (such as Tomcat) (the Java program takes effect for the current server environment):

A. Setting environment variables: variable name: catalina_opts variable Value:-xmx128m-xms64m-xmn32m-xss16m.

B. Open the Bin folder in the Tomcat root directory, edit the Catalina.bat, and replace the%catalina_opts% (all around) with the following:-xmx128m-xms64m-xmn32m-xss16m.

C. If there is no catalina.bat, only tomcat.exe,tomcat6w.exe; You can configure –java–java option after starting Tomcat6w.exe:

-xmx256m–xms64m

You can also find the registry Hkey_local_machine\software\apache SOFTWARE Foundation\tomcatservice Manager\tomcat6\parameters\javaoptions The original value is-dcatalina.home= "C:\ApacheGroup\Tomcat 6.0″-djava.endorsed.dirs=" C: \apachegroup\tomcat 6.0\common\endorsed "-xrs to join-xms300m-xmx350m (I was joined-xmx350m,tomcat to start, join-xms300m-xmx350m instead of Tom Cat does not start) to restart the Tomcat service, the settings are in effect.

3. View JVM Memory Information

Runtime.getruntime (). MaxMemory (); Maximum available memory, corresponding to-XMX

Runtime.getruntime (). Freememory (); Current JVM free memory

Runtime.getruntime (). TotalMemory (); Total memory consumed by the current JVM, with a value equal to the total memory used by the current JVM and Freememory ()

About MaxMemory (), Freememory () and TotalMemory (): MaxMemory () is the maximum available memory for the JVM, which can be set by the-XMX setting, with a default value of 1/4 of the physical memory and not higher than the physical memory of the computer; TotalMemory () The total amount of memory occupied by the current JVM, the value of which is the sum of the Memory and Freememory () used by the current JVM, and increases as the JVM uses more memory; Freememory () is the current JVM free memory. Because the JVM consumes physical memory only when memory is needed, the value of freememory () is generally small, whereas the JVM's actual available memory is not equal to freememory () and should be equal to MaxMemory ()-totalmemory () + Freememory ().

4. Example , the following gives reference to parameter setting of Java JVM in 1G memory environment

Java_opts= "-server-xms800m-xmx800m-xx:permsize=64m-xx:maxnewsize=256m-xx:maxpermsize=128m-djava.awt.headless= True "

Large Web engineering, the memory space allocated by Tomcat default cannot be started, and if Tomcat is not started in MyEclipse it can be set for Tomcat:

Tomcat_home\bin\catalina.bat Add this sentence:

Set java_opts=-xmx1024m-xms512m-xx:maxpermsize=256m

If you want to start in MyEclipse, the above changes will not work, you can set the following:

In the MYECLIPSE->PREFERENCES->MYECLIPSE->SERVERS->TOMCAT->TOMCATX.X->JDK panel

Optional Java VM arguments add:-xmx1024m-xms512m-xx:maxpermsize=256m

For a separate. Class, you can set the JVM memory for test runtime in the following ways. The java-xms64m-xmx256m Test-xms is to set the size of the memory initialization-xmx is to set the maximum amount of memory to use.

Iv. JVM memory Configuration and GC

What you need to consider is the garbage collection mechanism provided by Java. The JVM's heap size determines the amount of time and frequency that the JVM spends on garbage collection. The rate at which garbage collection can be accepted is related to the application and should be adjusted by analyzing the actual time and frequency of garbage collection. If the heap is large in size, complete garbage collection can be slow, but the frequency is reduced. If you match the size of the heap with the need for memory, the collection will be complete quickly, but will be more frequent. The purpose of the heap sizing is to minimize the time spent in garbage collection to maximize the processing of customer requests for a specific period of time. In the benchmark, to ensure the best performance, the heap size is set to ensure that garbage collection does not occur throughout the benchmarking process. If your system spends a lot of time collecting garbage, reduce the heap size. A complete garbage collection should be no more than 3-5 seconds. If garbage collection becomes a bottleneck, you need to specify the size of the heap, check the verbose output of garbage collection, and study the impact of garbage collection parameters on performance. Generally, you should use 80% of the physical memory as the heap size. When adding the processor, remember to increase the memory, because the allocation can be done in parallel, and garbage collection is not parallel.

The Java heap is divided into 3 areas:

1.Young 2.Old 3.Permanent. Young saves a newly instantiated object. When the area is filled, the GC moves the object to the old area. The permanent area is responsible for saving reflection objects, and this article does not discuss the area.

The JVM has 2 GC threads:

The first thread is responsible for the recovery of the young area of heap;
The second thread, when heap is insufficient, traverses the heap, upgrades the young to the older area, the older area is equal to-xmx minus-xmn, and the-XMS value cannot be set too large because the second thread is forced to run to degrade the JVM's performance.

Why do some programs frequently occur with GC? There are the following reasons:

1. System.GC () or RUNTIME.GC () is invoked within the program.
2. Some middleware software calls its own GC method, at which point you need to set parameters to prohibit these GC.
3. Java heap is too small, the general default heap values are very small.
4. Frequently instantiate objects, release objects try to save and reuse objects at this time, such as using StringBuffer () and string ().

If you find that after each GC, the remaining space of the heap will be 50% of the total space, which means that your heap is in a healthy state. Many server-side Java programs have a 65% of the remaining space after each GC.

Experience:

1. The server-side JVM is best to set-XMS and-xmx to the same value. In order to optimize GC, it is best to have the-xmn value equal to about 1/3 of-XMX.
2. A GUI program is best to run the GC once every 10-20 seconds, each time within half a second.

Attention:

1. Increasing the size of the heap may reduce the frequency of the GC, but it also increases the time for each GC. And when the GC runs, all the user threads will be paused, i.e., during the GC, the Java application does not do any work.
2. The heap size does not determine the amount of memory used by the process. The memory usage of a process is greater than the value defined by-XMX because Java allocates memory for other tasks, such as stack for each thread

Related Article

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.