Set the jvm vm memory size for Tomcat

Source: Internet
Author: User
Tags xms

Tomcat itself cannot run directly on a computer. It depends on an operating system based on hardware and a Java virtual machine. 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 use mid-XMS-xmx to adjust the initial memory and maximum memory of the application:
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
Variable name: java_opts
Variable value:-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 and click the Java tab. The following two items are displayed: 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 size of MB, but without the available memory size 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.