Increase Tomcat memory under Linux

Source: Internet
Author: User
Tags app service garbage collection xms

The configuration of my server:

# OS specific support. $var _must_ is set to either true or false.

Java_opts= "-xms1024m-xmx4096m-xss1024k-xx:permsize=512m-xx:maxpermsize=2048m"

Body:

Common memory overflows are listed in the following two ways:

Java.lang.OutOfMemoryError:PermGen Space

Java.lang.OutOfMemoryError:Java Heap Space

---------------------------------------------------------

Here, for example, the Tomcat environment, other Web servers, such as Jboss,weblogic, are the same reason.


One, Java.lang.OutOfMemoryError:PermGen space

The full name of PermGen space is permanent Generation space, which refers to the permanent storage area of memory,
This memory is primarily stored by the JVM with class and meta information, and class is placed in PermGen space when it is loader.
Unlike the heap area where the class instance (Instance) is stored, the GC (garbage Collection) does not operate on the main program
PermGen space for cleanup, so if you have a lot of classes in your app, you're likely to have permgen space errors,
This error is common when the Web server pre-compile the JSP. If you use a large number of third-party jars under your web app, its size
The default size of the JVM (4M) is exceeded, and this error message is generated.
Workaround: Manually set the MaxPermSize size
Recommendation: Move the same third-party jar files to the Tomcat/shared/lib directory, which can reduce the memory consumption of the jar document.

Second, Java.lang.OutOfMemoryError:Java heap space
The JVM heap setting is the set of memory space that the JVM can provision during the run of the Java program. The JVM automatically sets the value of the heap size when it is started.
Its initial space (i.e.-XMS) is the 1/64 of physical memory, and the maximum space (-XMX) is the area of physical memory. You can use options such as the-XMN-XMS-XMX provided by the JVM to
To set. The size of Heap size is the sum of young Generation and tenured generaion.
Tip: This exception message is thrown in the JVM if 98% of the time is used for GC and the available heap size is less than 2%.
Tip: The Heap Size does not exceed 80% of the available physical memory, generally the-XMS and-XMX options are set to the same, and the-XMX value of-xmn is 1/4.
Workaround: Manually set the heap size

----------------------------------------------------------

Modify JVM memory size under Linux:

To add the catalina.sh under Tomcat's bin, position cygwin=false before. Note the quotation marks to be taken, red for the newly added.

# OS specific support. $var _must_ is set to either true or false.
Java_opts= "-xms256m-xmx512m-xss1024k-xx:permsize=128m-xx:maxpermsize=256m"
Cygwin=false

Modify JVM memory size under windows:

Scenario One: Unzip the version of Tomcat to launch Tomcat via Startup.bat to load the configuration

To add the Catalina.bat under Tomcat's Bin

REM Guess catalina_home if not defined
The set current_dir=%cd% is added later, and the red ones are added for the new ones.

Set Java_opts=-xms256m-xmx512m-xx:permsize=128m-xx:maxnewsize=256m-xx:maxpermsize=256m-djava.awt.headless=true

Scenario Two: Installed version of Tomcat under no Catalina.bat

The Windows service performs bin/tomcat.exe. He reads the value in the registry instead of the Catalina.bat setting.

Modify the registry Hkey_local_machine/software/apache software Foundation/tomcat Service manager/tomcat5/parameters/javaoptions
The original value is
-dcatalina.home= "C:/apachegroup/tomcat 5.0"
-djava.endorsed.dirs= "C:/apachegroup/tomcat 5.0/common/endorsed"
-xrs

Join-xms300m-xmx350m
Restart Tomcat service, set to take effect

---------------------------------------------------------

The proportions of each parameter:

Xmx with PermSize and cannot exceed the total amount of memory that the JVM can obtain

PermSize must not be greater than xmx

================

How to set the JVM virtual machine memory size for Tomcat

You can set the memory used for the Java Virtual machine, but the virtual machine will not compensate if your choice is not correct. The size of the memory used by the virtual machine can be changed by the command line. As shown in the following table, there are two parameters to set the size of the 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 initialized heap performs the amount of memory requested by the virtual machine to the system at startup. Generally speaking, this parameter is not important. However, some applications in the case of heavy load will take up more memory, when this parameter is very important, if the virtual machine is set up when 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-XMS and-xmx as large, and the maximum value of the heap is limited by the physical memory used by the system. Applications that use large amounts of data generally use persistent objects, and memory usage can grow rapidly. The virtual machine prompts for memory overflow when the application needs more memory than the heap, and causes the app service to crash. Therefore, it is generally recommended that the maximum heap value be set to 80% of the maximum available memory.
Tomcat can use 128MB of memory by default, and in larger applications, this memory is not enough and needs to be scaled up.
Under Windows, under File/bin/catalina.bat,unix, in front of file/bin/catalina.sh, add the following settings:
java_opts= '-xms ' Initialize memory size "-XMX" maximum memory that can be used "'
You need to increase the value of this two parameter. For example:
java_opts= '-xms256m-xmx512m '
Indicates that the initialized memory is 256MB and the maximum memory that can be used is 512MB.
Another thing to consider is the garbage collection mechanism provided by Java. The heap size of a 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 time and frequency of actual garbage collection. If the heap size is large, then the total garbage collection will be slow, but the frequency will be reduced. If you match the size of the heap to the needs of the memory, it will be collected very quickly, but more often. The purpose of sizing the heap is to minimize garbage collection time to maximize processing of customer requests over a specific time period. In benchmarking, to ensure the best performance, the size of the heap to be large, to ensure that garbage collection does not occur throughout the baseline test 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, check the detailed output of the garbage collection, and investigate the performance impact of garbage collection parameters. Generally speaking, you should use 80% of the physical memory as the heap size. When adding processors, remember to increase the memory because allocations can be done in parallel, and garbage collection is not parallel.
Tomcat 5 common optimizations and configurations
1. JDK Memory Optimization:
Tomcat defaults to the memory used by 128mb,windows, under file {Tomcat_home}/bin/catalina.bat,unix, in file {tomcat_home}/bin/ In front of the catalina.sh, add the following settings:
Java_opts= '-xms[initialize memory size]-xmx[maximum memory that can be used]
Generally speaking, you should use 80% of the physical memory as the heap size.
2, connector optimization:
In the configuration in Tomcat configuration file Server.xml, the parameters related to the number of connections are:
MaxThreads:
Tomcat uses threads to process each request that is received. This value represents the maximum number of threads that Tomcat can create. The default value is 150.
Acceptcount:
Specifies the number of requests that can be placed in the processing queue when all the threads that can be used to process the request are used, and requests that exceed this number will not be processed. The default value is 10.
Minsparethreads:
The number of threads created when Tomcat was initialized. The default value is 25.
Maxsparethreads:
Once you create a thread that exceeds this value, tomcat closes the socket thread that is no longer needed. The default value is 75.
Enablelookups:
Whether to reverse the domain name, the default value is true. To improve processing power, set to False
Connnectiontimeout:
Network connection timed out, default value 60000, in milliseconds. Set to 0 to never time out, so the setting is hidden. It can usually be set to 30000 milliseconds.
Maxkeepaliverequests:
Keep the number of requests, the default value of 100.
BufferSize:
Input stream buffer size, default value 2048 bytes.
Compression
Compressed transfer, value On/off/force, default value 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 parameters should be increased. The maximum number of connections allowed by Web server is also subject to the system's kernel parameter setting, usually Windows is about 2000, and Linux is about 1000.
3. How to disable and allow files in a 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</param-value>
</init-param>
...
</servlet>
4. How to disable and allow host or IP address access in Tomcat
...
< Valve classname= "Org.apache.catalina.valves.RemoteHostValve"
allow= "*.mycompany.com,www.yourcompany.com"/>
< Valve classname= "Org.apache.catalina.valves.RemoteAddrValve"
deny= "192.168.1.*"/>
...
Configuration of the server
java_opts= '-server-xms512m-xmx768m-xx:newsize=128m-xx:maxnewsize=192m-xx:survivorratio=8 '

Increase Tomcat memory under Linux

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.