Tomcat memory size adjustment

Source: Internet
Author: User
Tags benchmark xms

Tomcat memory size adjustment

Default category: 209 Comment 0 Font size: Large LargeMedium MediumSmall Small

I,

Tomcat memory settings

When a Java program is used to query a large amount of data from the database or the application server (such as Tomcat, JBoss, and WebLogic) loads the jar package, a java. Lang. outofmemoryerror error occurs. This is mainly caused by insufficient memory on the application server. This exception often happens in the following situations (taking the Tomcat environment as an example, other Web servers, such as JBoss and WebLogic, share the same principle ):

1. java. Lang. outofmemoryerror: permgen Space

Permgen space is called permanent generation space, which refers to the permanent storage area of the memory outofmemoryerror: permgen space. In terms of text, memory overflow occurs. The solution is to increase the memory. Why is the memory overflow? This is because the memory is mainly used by JVM to store class and meta information. When the class is loaded, it is placed in the permgen space area, unlike the heap region where the instance is stored, GC (garbage collection) does not clean up permgen space during the main program running, so if your app loads a lot of classes, 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.

Solution: manually set the maxpermsize

A. If Tomcat is started in bat mode, set it as follows:

Modify tomcat_home/bin/Catalina. Sh

Add the following lines to "Echo" using catalina_base: $ catalina_base:

Java_opts = "-server-XX: permsize = 64 m-XX: maxpermsize = 128 m

B. If Tomcat is registered as a Windows Service and started in Services Mode, You need to modify the corresponding key value in the registry.

Open the registry and find the HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ procrun 2.0 \ htfty \ Parameters \ Java directory. The red mark (such as htfty) in the directory address needs to be modified according to different situations, register the Tomcat service as the name of the Windows service. Jvmms and jvmmx are displayed. jvmms sets the minimum memory usage parameters and jvmmx sets the maximum memory usage parameters. Set the values of jvmms and jvmmx, and restart the Tomcat server.

Suggestion: Move the same third-party jar files to the tomcat/shared/lib directory to reduce the memory usage of jar files.

2. java. Lang. outofmemoryerror: Java heap Space

The JVM heap setting refers to the setting of the 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.

Solution: manually set heap size

A. If Tomcat is started in bat mode, set it as follows:

Modify tomcat_home/bin/Catalina. Sh

Add the following lines to "Echo" using catalina_base: $ catalina_base:

Java_opts = "-server-xms800m-xmx800m-XX: maxnewsize = 256 m"

B. If Tomcat is registered as a Windows Service and started in Services Mode, You need to modify the corresponding key value in the registry.

Open the registry and find the HKEY_LOCAL_MACHINE \ SOFTWARE \ Apache Software Foundation \ procrun 2.0 \ htfty \ Parameters \ Java directory. The red mark (such as htfty) in the directory address needs to be modified according to different situations, register the Tomcat service as the name of the Windows service. Jvmms and jvmmx are displayed. jvmms sets the minimum memory usage parameters and jvmmx sets the maximum memory usage parameters. Set the values of jvmms and jvmmx, and restart the Tomcat server.

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.

 

2. Tomcat itself cannot run directly on a computer and relies on an operating system and a Java Virtual Machine Based on hardware. When the Java program starts, the JVM allocates an initial memory and the maximum memory to the application. The initial memory and maximum memory will affect the performance of the program to a certain extent. For example, when applications use the maximum memory, the JVM needs to first perform garbage collection to release some occupied memory. Therefore, to adjust the initial memory and maximum memory at Tomcat startup, you must declare to JVM, generally, when running Java programs, you can adjust the initial memory and maximum memory of the application through "-XMS-xmx": the size of these two values is generally set as needed. The size of the initialization heap is the size of the memory applied by the VM to the system at startup. In general, this parameter is not important. However, some applications use more memory at a high load, and this parameter is very important, if the VM is set to use a small amount of memory during startup and many objects are initialized in this case, the VM must repeatedly increase the memory to meet the requirement. For this reason, we generally set-XMS to the same size as-xmx, and the maximum heap size is limited by the physical memory used by the system. Generally, applications with large data volumes use persistent objects, and memory usage may increase rapidly. When the memory required by the application exceeds the maximum value of the heap, the VM will prompt a memory overflow and cause the application service to crash. Therefore, we recommend that you set the maximum value of the heap to 80% of the maximum available memory.

By default, Tomcat can use 128 MB of memory. In large application projects, this memory is insufficient and needs to be increased. You can use the following methods:

Method 1:

In Windows, add the following settings before file/bin/Catalina. bat, UNIX, and file/bin/Catalina. sh:

Java_opts = '-XMS [initial memory size]-xmx [maximum memory available ]'

You need to increase the value of these two parameters. For example:

Java_opts = '-xms256m-xmx512m'

Indicates that the initial memory is 256 MB, and the maximum memory available is 512 MB.

Method 2: Set the java_opts variable value in the environment variable:-xms512m-xmx512m

Method 3: The first two methods are for Catalina under the bin directory. BAT (for example, directly decompress Tomcat), but some installed versions of Tomcat do not have Catalina. bat, you can use the following method at this time, of course, this method is also the most common method: Open tomcathome/\ bin/\ tomcat5w.exe, click the Java tab, there are two items: initial memory pool and maximum memory pool. initial memory pool: memory size set for initialization. Maximum memory pool. When the maximum memory size is set, press OK and restart tomcat. You will find that the JVM memory available in Tomcat has changed.

In addition, you need to consider the garbage collection mechanism provided by Java. The size of the Virtual Machine heap determines the time and frequency of the Virtual Machine spending on garbage collection. The acceptable speed of garbage collection is related to the application. The actual garbage collection time and frequency should be analyzed to adjust. If the heap size is large, the garbage collection process will be slow, but the frequency will decrease. If you keep the heap size consistent with the memory size, the full collection will be fast, but it will be more frequent. The purpose of adjusting the heap size is to minimize the garbage collection time to maximize the processing of customer requests within a specific period of time. During the benchmark test, to ensure the best performance, set the heap size to a large value to ensure that garbage collection does not appear throughout the benchmark test. If the system spends a lot of time collecting garbage, reduce the heap size. A complete garbage collection should not exceed 3-5 seconds. If garbage collection becomes a bottleneck, You need to specify the generation size, check the detailed 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 a processor, remember to increase the memory, because the allocation can be performed in parallel, and garbage collection is not in parallel.

Note: We recommend that you reduce the difference between the maximum and minimum values of the memory. Otherwise, it will waste a lot of memory. The minimum value increases and the maximum value can be set at will, but depending on the actual physical memory, if the memory is too large, such as setting the maximum memory of MB, but without the available memory of MB, Tomcat cannot be started. There may also be situations where the memory is recycled by the system and the process is terminated.

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.