How to set up Tomcat's JVM virtual machine memory size

Source: Internet
Author: User
Tags command line garbage collection memory usage version thread domain name tomcat xms

Tomcat itself cannot run directly on the computer, and relies on a hardware-based operating system and a Java virtual machine. You can choose your own version of the operating system and the corresponding JDK (as long as it complies with the Sun-issued Java specification), but we recommend that you use the JDK published by Sun. Make sure that the version you are using is up to date because sun and other companies have been upgrading their Java virtual machines to improve performance. Some reports show that JDK1.4 has increased performance by nearly 10% to 20% compared to JDK1.3.

You can set the memory used for the Java Virtual machine, but the virtual machine will not compensate if your choice is incorrect. You can change the size of the virtual machine's memory using the command line. As shown in the following table, two parameters are used to set the amount of memory used by the virtual machine.

Parameters

Describe

-xms

The size of the JVM initialization heap

-xmx

Maximum value of JVM heap

The size of these 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.

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.

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.

Tomcat 5 common optimizations and configurations

1, JDK memory optimization:

Tomcat default can use memory for 128mb,windows under file {Tomcat_home}/bin/catalina.bat,unix, in file {tomcat_home}/bin/ Before catalina.sh, add the following settings:

Java_opts= '-xms[initialization memory size]-xmx[maximum memory available]

Generally, you should use 80% of the physical memory as the heap size.

2, connector optimization:

In the configuration in the tomcat configuration file Server.xml, the parameters associated with the number of connections are:

MaxThreads:

Tomcat uses threads to handle each request that is received. This value represents the maximum number of threads that Tomcat can create. The default value is 200.

Acceptcount:

Specifies the number of requests that can be placed in the processing queue when all available processing requests are used, and requests that exceed this number are not processed. The default value is 10.

Minsparethreads:

The number of threads created when Tomcat was initialized. The default value is 4.

Maxsparethreads:

Once a thread is created that exceeds this value, tomcat closes the socket thread that is no longer needed. The default value is 50.

Enablelookups:

The default value is true if the domain name is checked back. To improve processing power, set to False

Connnectiontimeout:

Network connection timeout, default value 60000, unit: milliseconds. A setting of 0 means never time out, and this setting is hidden. Typically, it can be set to 30000 milliseconds.

Maxkeepaliverequests:

Keep the number of requests, the default value is 100.

BufferSize:

Input stream buffer size, default value 2048 bytes.

Compression

Compress the transfer, take the value On/off/force, and the default value is off.

The parameters associated with the maximum number of connections are maxthreads and Acceptcount. If you want to increase the number of concurrent connections, both of these parameters should be increased. The maximum number of connections allowed by Web server is also subject to the kernel parameter settings of the operating system, typically Windows is 2000 or so, and Linux is about 1000.

3. How to prohibit and allow files under the column directory in Tomcat

In {tomcat_home}/conf/web.xml, set the listings parameter to False, as follows:

       
        
         
        <servlet>...<init-param><param-name>listings</param-name><param-value>false</ How to prohibit and allow host or IP addresses to access 

This is our server configuration, the specific file can not give you, because it involves company secrets, so I give you the line, I copied it down

java_opts= '-server-xms512m-xmx768m-xx:newsize=128m-xx:maxnewsize=192m-xx:survivorratio=8 '



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.