Modify the JVM memory used by tomcat in Linux

Source: Internet
Author: User
Tags xms

Transferred from:

Http://blog.csdn.net/sully2008/article/details/6457570

 

My Server Configuration:

# OS specific support. $ VaR _ must _ be set to either true or false.

Java_opts = "-xms1024m-xmx4096m-xss1024k-XX: permsize = 512 M-XX: maxpermsize = 2048 M"

 

Body:

 

There are two common memory overflow types:

Java. Lang. outofmemoryerror: permgen Space

Java. Lang. outofmemoryerror: Java heap Space

 

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

Take 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 stands for permanent generation space, which is the permanent storage area of the memory,
This memory is mainly used by JVM to store class and meta information. When the class is loaded, it will be placed in permgen space,
Unlike the heap region of the storage instance, GC (garbage collection) is not stored in the masterProgramRuntime pair
Permgen space is cleaned up. Therefore, if your application contains many 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
This error message is generated when the default JVM size (4 MB) is exceeded.
Solution: manually set the maxpermsize
Suggestion: Move the same third-party jar files to the tomcat/shared/lib directory to reduce the memory usage of jar files.

 

Ii. java. Lang. outofmemoryerror: Java heap Space
The JVM heap setting refers to the setting of the memory space that JVM can allocate during the Java program running. The JVM automatically sets the heap size value at startup,
Its initial space (-XMS) is1/64 of physical memory, The maximum space (-xmx) is1/4 of physical memory. You can use options such as-xmn-XMS-xmx provided by JVM.
. The heap size is the sum of young generation and tenured generaion.
Tip: if 98% is used for GC and the available heap size is less than 2% in JVM, this exception is thrown.
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.
Solution: manually set heap size

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

 

Modify the JVM memory size in Linux:

To addTomcatIn Catalina. Sh under the bin, before the position cygwin = false. Note that the quotation marks must be placed, and the red ones are newly added.

# OS specific support. $ VaR _ must _ be set to either true or false.
Java_opts = "-xms256m-xmx512m-xss1024k-XX: permsize = 128 M-XX: maxpermsize = 256 m"
Cygwin = false

 

Modify the JVM memory size in Windows:

Scenario 1: to decompress tomcat, you must start Tomcat through startup. BAT to load the configuration.

To addTomcatIn Catalina. bat under the bin

Rem guess catalina_home if not defined
Set current_dir = % Cd % followed by red.

Set java_opts =-xms256m-xmx512m-XX: permsize = 128 M-XX: maxnewsize = 256 m-XX: maxpermsize = 256 m-djava. AWT. Headless = true

 

Scenario 2:Installed versionTomcatNo Catalina. bat

Windows Services run bin/Tomcat. EXE. It reads the value in the registry, instead of setting Catalina. bat.

Modify the Registry HKEY_LOCAL_MACHINE/software/Apache Software Foundation/TomcatService Manager/tomcat5/parameters/javaoptions
Original Value:
-Dcatalina. Home = "C:/apachegroup/Tomcat5.0"
-Djava. endorsed. dirs = "C:/apachegroup/Tomcat5.0/common/endorsed"
-Xrs

Add-xms300m-xmx350m
Restart Tomcat Service, the settings take effect

 

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

Ratio of each parameter:

The sum of xmx and permsize cannot exceed the total memory available by JVM

Permsize cannot be greater than xmx

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

How to set the memory size of Tomcat JVM Virtual Machine

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 use the command line to change the memory used by the VM. The following table shows two parameters used to set the memory used by the VM.
Parameters
Description
-XMS
Size of the JVM initialization heap
-Xmx
Maximum JVM heap Value
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, persistent objects are used for applications with large data volumes, 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 shortest threshold 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.
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.
In addition, the garbage collection mechanism provided by Java needs to be considered. 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.
Tomcat 5 common optimization and Configuration
1. JDK memory optimization:
By default, Tomcat memory is 128 MB. In Windows, the file {tomcat_home}/bin/Catalina. in bat and UNIX, the file {tomcat_home}/bin/Catalina. before SH, add the following settings:
Java_opts = '-XMS [initial 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 related to the number of connections include:
Maxthreads:
Tomcat uses a thread to process each received request. This value indicates the maximum number of threads that Tomcat can create. The default value is 150.
Acceptcount:
Specify the number of threads that can be used to process requests, and the number of requests that can be placed in the processing queue. Requests that exceed this number will not be processed. The default value is 10.
Minsparethreads:
The number of threads created during Tomcat initialization. The default value is 25.
Maxsparethreads:
Once the created thread exceeds this value, Tomcat closes the socket thread that is no longer needed. The default value is 75.
Enablelookups:
Whether to reverse query the domain name. The default value is true. Set to false to improve processing capability.
Connnectiontimeout:
Network Connection timeout. The default value is 60000. Unit: milliseconds. If it is set to 0, it indicates that the request Never times out. This setting has potential risks. Generally, it can be set to 30000 ms.
Maxkeepaliverequests:
Number of requests to be retained. The default value is 100.
Buffersize:
The input stream buffer size. The default value is 2048 bytes.
Compression:
Compression and transmission. Optional values: On, off, and force. Default Value: off.
The parameters related to the maximum number of connections are maxthreads and acceptcount. To increase the number of concurrent connections, increase these two parameters at the same time. The maximum number of connections allowed by web server is also subject to * Kernel Parameter settings of the system. Generally, Windows has about 2000 connections and Linux has about 1000 connections.
3. How to disable and allow files in the column directory in Tomcat
In {tomcat_home}/CONF/Web. XML, set the listings parameter to false, as shown below:
<Servlet>
...
<Init-param>
<Param-Name> listings </param-Name>
<Param-value> false </param-value>
</Init-param>
...
</Servlet>
4. How to disable and allow access from hosts or IP addresses in Tomcat
<Host name = "localhost"...>
...
<Valve classname = "org. Apache. Catalina. Valves. remotehostvalve"
Allow = "* .mycompany.com, www.yourcompany.com"/>
<Valve classname = "org. Apache. Catalina. Valves. remoteaddrvalve"
Deny = "192.168.1. *"/>
...
</Host>
Server Configuration
Java_opts = '-server-xms512m-xmx768m-XX: newsize = 128 M-XX: maxnewsize = 192 m-XX: Export vorratio = 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.