Tomcat environment and thread pool, JDK configuration detailed

Source: Internet
Author: User
Tags xms

Tomcat environment and thread pool, JDK configuration detailed
One, the common Java memory overflow has the following three kinds:
1. JVM Heap Overflow: Java.lang.OutOfMemoryError:Java heap Space
The JVM will automatically set the JVM heap value when it is started, and its initial space (that is,-XMS) is 1/64 of the physical memory, and the maximum space (-XMX) cannot exceed the physical memory.
You can use options such as the-XMN-XMS-XMX provided by the JVM to set it up. The size of the heap is the sum of young Generation and tenured generaion.
This exception information is thrown in the JVM if 98% of the time is used for GC, and the available heap size is less than 2%.
Workaround: Manually set the size of the JVM heap (heap)

2. PermGen space overflow: Java.lang.OutOfMemoryError:PermGen space
The full name of PermGen space is permanent Generation space, which refers to the permanent storage area of memory.
Why memory overflow, this is because the memory is mainly stored by the JVM class and meta information, class in the load is loaded into the PermGen space area, which is stored in the instance heap area, sun The GC does not clean up permgen space during the main program's run time, so if your app loads many classes, there's a good chance that permgen space will overflow.
Workaround: Manually set the MaxPermSize size

3. Stack Overflow: java.lang.StackOverflowError
Stack overflows, the JVM is still a stack-based virtual machine, which is the same as C and Pascal. The procedure for calling a function is on the stack and on the fallback.
There are too many "layers" of calls to the constructor to overflow the stack area.
Generally speaking, the general stack area is much smaller than the heap area, because the function call process is often not more than thousands of layers, and even if each function call requires 1K of space (this is approximately equivalent to a C function declared 256 int type variable), then the stack area is just need 1MB of space. The size of the stack is usually 1-2mb.
Often recursion does not have too many levels of recursion, it is easy to overflow.
Workaround: Modify the program.

Second, the solution
In a production environment, Tomcat memory settings are not good enough to be prone to JVM memory overflow.
1. Tomcat under Linux in front of Cygwin=false
Modify Tomcat_home/bin/catalina.sh
Export Java_home= "/USR/LOCAL/JDK"
Export Jre_home= "/usr/local/jdk/jre"

# OS specific support. $var _must_ is set to either true or false.
.............
.............
# GET Standard Environment variables
Prgdir= ' dirname "$PRG" '

# only set Catalina_home if not already set
[-Z "$CATALINA _home"] && catalina_home= ' CD $PRGDIR/... ">/dev/null; PWD '

# Copy Catalina_base from Catalina_home if not already set
[-Z "$CATALINA _base"] && catalina_base= "$CATALINA _home"

Export catalina_opts= "-server-xss512k-xms1024m-xmx1024m-xx:permsize=128m-xx:maxpermsize=256m-xx:+ DISABLEEXPLICITGC-XX:+USECONCMARKSWEEPGC-XX:+USEPARNEWGC-XX:PARALLELGCTHREADS=8-VERBOSE:GC-XLOGGC: $CATALINA _ Base/logs/gc.log-xx:+heapdumponoutofmemoryerror-xx:heapdumppath= $CATALINA _base/-dcom.sun.management.jmxremote- Dcom.sun.management.jmxremote.port=10001-dcom.sun.management.jmxremote.authenticate=false- Dcom.sun.management.jmxremote.ssl=false "

Third, JVM parameter description:
-server: Be sure to act as the first parameter because Tomcat is running in a mode called java–client, and server means that your tomcat is running in a real production mode
-xms:java Heap Initial size. It is the best practice to set XMS to the same value as Xmx two.
-xmx:java heap Maximum value. It is the best practice to set XMS to the same value as Xmx two.
-xx:permsize: Sets the initial size of the memory's permanent save, the default value is 64M.
-xx:maxpermsize: Sets the maximum size of the memory's permanent save, the default value is 64M.
-xx:survivorratio=2: The size of the survivor pool, default is 2, if garbage collection becomes a bottleneck, you can try to customize the build pool settings
-xx:newsize: The initial size of the newly generated pool. The default value is 2M.
-xx:maxnewsize: The maximum size of the newly generated pool. The default value is 32M.
If the JVM has a heap size greater than 1GB, you should use the value:-xx:newsize=640m-xx:maxnewsize=640m-xx:survivorratio=16, or allocate 50% to 60% of the total size of the heap to the newly generated pool. Adjust the DA object area to reduce the number of full GC.
+xx:aggressiveheap will make XMS meaningless. This parameter allows the JVM to ignore the XMX parameter, frantically eating a g of physical memory, and then eating a G swap.
-XSS: Stack size per thread, "-XSS 15120" This causes JBoss to consume 15M of memory as soon as each additional thread is added (thread), and the best value should be 128K, and the default value seems to be 512k.
-VERBOSE:GC Reality Garbage Collection Information
-xloggc:gc.log specifying garbage collection log files
The heap size of the-xmn:young generation, generally set to Xmx of 3, 4, of the entire heap size = younger generation size + old generation size + persistent generation size. The permanent average fixed size is 64m, so increasing the younger generation will reduce the size of older generations. This value has a large impact on system performance, and Sun's official recommendation is 3/8 for the entire heap.
-XX:+USEPARNEWGC: Shortening the time of minor collection
-XX:+USECONCMARKSWEEPGC: Shorten major collection time this option is more appropriate if the heap size is large and major collection time is longer.
-XX:USERPARNEWGC can be used to set up parallel collection "multi-CPU"
-xx:parallelgcthreads can be used to increase the degree of parallelism "multi-CPU"
-XX:USEPARALLELGC can be set to use parallel purge collector "multi-CPU"
Use JVISUALVM remote monitoring Tomcat to add the following parameters to Tomcat's catalina.sh:
Set java_opts=-dcom.sun.management.jmxremote-dcom.sun.management.jmxremote.port= "9004"- Dcom.sun.management.jmxremote.authenticate= "false"-dcom.sun.management.jmxremote.ssl= "false"
-DCOM.SUN.MANAGEMENT.JMXREMOTE.PORT=9004 specifies a JMX-initiated proxy port, which is the port to which the Visual VM is connected
-dcom.sun.management.jmxremote.ssl = "false" Specifies whether JMX is enabled for SSL
-dcom.sun.management.jmxremote.authenticate =false Specifies that no password authentication connection is required

Iv. thread pool configuration: in./conf/server.xml:
Search for "<executor name=" Tomcatthreadpool "", open and adjust to: Max thread 500 (general server sufficient), minimum number of idle threads 20, maximum idle time 60 seconds for thread.
<!--2015/03/03 14:32 Changee by Wangyingchun--
<executor name= "Tomcatthreadpool" nameprefix= "catalina-exec-"
maxthreads= "minsparethreads=" maxidletime= "60000"/>

Search for "port=" 8080 "", open and modify <connector ...> nodes, add executor attributes such as:
<!--2015/03/03 14:32 Changee by Wangyingchun--
<connector executor= "Tomcatthreadpool"
Port= "8001" protocol= "http/1.1"
Connectiontimeout= "60000"
redirectport= "443" uriencoding= "UTF-8"
minsparethreads= "30"
maxsparethreads= "300"
Enablelookups= "false"
Disableuploadtimeout= "true"
Compression= "on" compressionminsize= "4096"
Nocompressionuseragents= "Gozilla, Traviata"
Compressablemimetype= "text/html,text/xml,text/javascript,text/css,text/plain,application/json,application/ X-javascript "
Maxthreads= "/>"

Maxthreads:tomcat the maximum number of threads that can be created, and each thread processes a request;
Minsparethreads: The minimum number of standby threads, the number of threads initialized at Tomcat startup;
Maxsparethreads: Maximum number of spare threads, once the thread is created that exceeds this value, Tomcat shuts down the socket thread that is no longer needed;
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.
Is the number of requests that are queued, and requests that exceed this number will reject the connection.
Connnectiontimeout: Network connection time-out, unit: milliseconds. Set to 0 to never time out, so the setting is hidden. It can usually be set to 30000 milliseconds.
Enablelookups: Whether DNS queries are allowed
Uriencoding= "UTF-8" allows Tomcat to parse the URL of a file containing a Chinese name
Maxsparethreads means that if the number of threads in the idle state is greater than the number of settings, the threads are aborted, reducing the total number of threads in the pool.
Minsparethreads the minimum number of standby threads, the number of threads initialized at tomcat startup.
Enablelookups This effect is set to off like the hostnamelookups in Apache.
ConnectionTimeout the number of milliseconds that the network connection time out.
MaxThreads Tomcat uses threads to process each request that is received. This value represents the maximum number of threads that Tomcat can create, that is, the maximum number of concurrent.
Acceptcount is when the number of threads reaches MaxThreads, subsequent requests will be placed in a waiting queue, the Acceptcount is the size of this queue, if the queue is full, it is directly refuse connection

V. JNDI configuration
[Email protected] conf]# VI context.xml
MySQL Driver pack: http://mirrors.sohu.com/mysql/Connector-J/mysql-connector-java-5.1.34.zip
Oracle Driver Pack: http://www.oracle.com/technetwork/cn/articles/oem/jdbc-112010-094555-zhs.html
Tomcat:http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.56/bin/apache-tomcat-7.0.56.zip
Edit the Context.xml file under Conf to insert the following resource information in the context module
<Context>
<resource name= "Jdbc/cfgds"
Auth= "Container"
Type= "Javax.sql.DataSource"
Driverclassname= "Oracle.jdbc.driver.OracleDriver"
Url= "jdbc:oracle:thin:@ (description= (address_list= (address=) (PROTOCOL=TCP) (host=10.10.5.18)) ( Connect_data= (SERVICE_NAME=HCCDB))) "
Username= "MPM"
Password= "MPM"
Maxactive= "30"
maxidle= "0"
maxwait= "30000"/>
<resource name= "JDBC/BOOKDB"
Auth= "Container"
Driverclassname= "Com.mysql.jdbc.Driver"
Type= "Javax.sql.DataSource" url= "Jdbc:mysql://10.10.6.77:3306/amdb?characterencoding=utf-8"
Username= "Root"
Password= "Mangocity"
Maxactive= "30"
maxidle= "0"
maxwait= "30000"/>
</Context>


Verify:
Create a Web. XML under web-inf/
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE Web-app Public
'-//sun Microsystems, INC.//DTD Web application 2.3//en ' HTTP://JAVA.SUN.COM/J2EE/DTDS/WEB-APP_2_3.DTD ' >
<web-app>
<resource-ref>
<description>db connection</description>
<res-ref-name>jdbc/BookDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
5. Create a JSP file, index.jsp
<%@ page language= "java" import= "java.util.*,javax.naming.*,java.sql.*,javax.sql.*" pageencoding= "UTF-8"%>
<%
Context CTX = new InitialContext ();
String strlookup = "JAVA:COMP/ENV/JDBC/BOOKDB";
DataSource ds = (DataSource) ctx.lookup (strlookup);
Connection con = ds.getconnection ();
if (con! = null) {
Out.print ("Success");
}else{
Out.print ("failure");
}
%>

Tomcat environment and thread pool, JDK configuration detailed

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.