Tomcat Memory Size Configuration _studing

Source: Internet
Author: User
Tags memory usage xms

Tomcat itself cannot run directly on the computer, and relies on a hardware-based operating system and a Java virtual machine. When the Java program starts, the JVM allocates an initial memory and maximum memory to the application. This initial memory and maximum internal presence will affect the performance of the program. For example, when the application uses the maximum memory, the JVM is going to do the garbage collection action, releasing some memory occupied. So if you want to adjust the initial and maximum memory for Tomcat startup, you need to declare to the JVM that normal Java programs can adjust the initial and maximum memory of the application through-XMS-XMX: The size of the two values is generally set as needed. The size of the initialization heap performs the size of the memory that the virtual machine requests to the system at startup. Generally speaking, this parameter is not important. However, some applications in a large load of the case will be a sharp use of more memory, at this time this parameter is very important, if the virtual machine is set up at the start of the memory is relatively small and in this case there are many objects to initialize, the virtual machine must repeatedly increase the 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. When the application needs more memory than the maximum of the heap, the virtual machine prompts for a memory overflow and causes the application service to crash. Therefore, the maximum value of the generic recommended heap is set to 80% of the maximum available memory.

Tomcat can use the default memory of 128MB, in larger application projects, this memory is not enough, need to be scaled up. There are several ways to choose:

The first method:

Under Windows, under File/bin/catalina.bat,unix, in front of the file/bin/catalina.sh, add the following settings:

java_opts= '-xms ' Initialize memory size '-XMX ' Maximum memory available '

You need to increase the value of this two parameter. For example:

java_opts= '-xms256m-xmx512m '

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

The second method: Set variable name in environment variable: java_opts variable value:-xms512m-xmx512m

The third method: the first two methods for the bin directory is catalina.bat (such as direct decompression of Tomcat, etc.), but some installed version of Tomcat without Catalina.bat, this time you can use the following methods, of course, this method is also the most common method: Open the Tomcath Ome//bin//tomcat5w.exe, click on the Java tab, and then you'll find that there are two of them: Initial memory pool and maximum memory pool. Initial memory Pool This is the size of the memory to initialize the settings. Maximum memory Pool This is the maximum size of the memory set, just press OK and restart Tomcat. You'll find that the memory available to the JVM in Tomcat has changed

Another thing to consider is the garbage collection mechanism provided by Java. The heap size of the virtual machine determines the time and frequency at which the virtual machine spends collecting garbage. 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 generation, examine 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.

A place to note: It is recommended to reduce the maximum value of memory to the minimum value of the difference, otherwise it will waste a lot of memory, the lowest value increases, the highest value can be arbitrarily set, but to the actual physical memory, if the memory settings are too large, such as set the 512M maximum memory, but if there is no 512M available memory, Tomcat will not boot, and there may be memory being reclaimed by the system to terminate the process.

Add (is the second line) under the @echo off of Catalina.bat

Set java_opts=-server-xms512m-xmx1024m-xx:maxnewsize=512m-xx:maxpermsize=256m

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.