tomcat 性能调优

性能调优:主要从以下几个方面入手

  • 应用代码:假如代码开发的不好,它会导致性能问题,比如数据库的连接在该关闭的时候没有适当的关闭,将会导致应用运行慢。
  • 数据库的调优:如果数据库的响应比较慢,那么应用也肯定就会回应的比较慢了。
  • JVM的调优:假如应用需要较多的内存来运行,而你分配比较小的内存,那么就会导致内存溢出,因而也会导致性能问题。
  • 中间件服务:比如我们在选择消息队列,缓存或者设计好时,也会导致性能问题。
  • 基础架构和OS:比如,网络丢包,系统配置不合理等。

适当的日志记录和监控:日志和监控有助于分析和排错。

Tomcat连接器的类型:

Java HTTP 连接器:

是基于HTTP协议,支持HTTP1.1,它使tomcat服务器扮演一个独立 服务器和JSP/servlet功能的服务器。

Java AJP 连接器:

JAVA AJP是基于Apache JServ 协议的,该连接器常常是在你不想暴露自己的Java servlet容器到Internet.

APR(AJP/HTTP)连接器:

Apache Portable Runtime(APR)是在扩容,性能和不同web服务器之间的兼容最好 的。它提供了比如OPENSSL,共享内存,Unix大套接字等。

线程调优:

线程沲定义了web服务器连接请求连接的数量,可以定义两种线程沲:一是共享沲,二是专用沲。该配置在TOMCAT_HOME/conf/server.xml文件中定义的。

共享线程沲:

假如你配置了四个连接器,那么你可以共享使用这个线程沲。
配置如下:

(1)定义线程沲
<Executor name="tomcatThreadPool"
                            namePrefix="catalina-exec-"
                            maxThreads="150"
                            minSpareThreads="4"/>
(2)引用定义的线程沲
<Connector executor="tomcatThreadPool"
port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
专用线程沲:也是在server.xml文件中进行定义的
<Connector port="8443" protocol="HTTP/1.1"
SSLEnabled="true"
maxThreads="150"
scheme="https"
secure="true"
clientAuth="false" sslProtocol="TLS" />
下表是专用线程沲和共享线程沲经常使用场景对比
  • Features Shared thread pool Dedicated thread pool
  • Number of users less High
  • Environment Development Production
  • Performance low Good
maxThreads:

认定义的最大线程沲是150个,在生产环境中,可以根据服务器的性能来调下该参数。

maxKeepAlive:

也相当于并发数一样,认值是1,也就是相当于关闭

JVM的调优:

JMAP(内存映射)

JMAP显示共享JAVA虚拟机内存信息,对查看共享内存的状态有用的。下面是一些常用选项:
Options Description

  • -dump Dumps the Java heap in hprof binary format
  • -finalizer info Prints information on objects awaiting finalization
  • -heap Prints a heap summary
  • -histo Prints a histogram of the heap
  • -permstat Prints class loader-wise statistics of permanent generation of the Java heap

jmap的语法:

./jmap --heap <process id>

比如我们的JAVA的ID是4306,那么就执行./jmap -heap 4306

Attaching to process ID 4306, please wait...
Debugger attached successfully.
Client compiler detected.
JVM version is 19.1-b02
using thread-local object allocation.
Mark Sweep Compact GC
Heap Configuration:
MinHeapFreeRatio = 40
MaxHeapFreeRatio = 70
MaxHeapSize = 268435456 (256.0MB)
NewSize = 1048576 (1.0MB)
MaxNewSize = 4294901760 (4095.9375MB)
OldSize = 4194304 (4.0MB)
NewRatio = 2
SurvivorRatio = 8
PermSize = 12582912 (12.0MB)
MaxPermSize = 67108864 (64.0MB)
Heap Usage:
New Generation (Eden + 1 Survivor Space):
capacity = 5111808 (4.875MB)
used = 3883008 (3.703125MB)
free = 1228800 (1.171875MB)
75.96153846153847% used
Eden Space:
capacity = 4587520 (4.375MB)
used = 3708360 (3.5365676879882812MB)
free = 879160 (0.8384323120117188MB)
80.83583286830357% used
From Space:
capacity = 524288 (0.5MB)
used = 174648 (0.16655731201171875MB)
free = 349640 (0.33344268798828125MB)
33.31146240234375% used
To Space:
capacity = 524288 (0.5MB)
used = 0 (0.0MB)
free = 524288 (0.5MB)
0.0% used
tenured generation:
capacity = 11206656 (10.6875MB)
used = 3280712 (3.1287307739257812MB)
free = 7925944 (7.558769226074219MB)
29.274673908077485% used
Perm Generation:
capacity = 12582912 (12.0MB)
used = 6639016 (6.331459045410156MB)
free = 5943896 (5.668540954589844MB)
52.762158711751304% used

从上面看可以看到如下主要信息:

  • 应用的堆配置
  • 每个JVM组件的堆内存利用率
  • 垃圾收集器使用的算法

堆内存的配置:

在catalina.sh中JAVA_OPTS的选项。
JAVA_OPTS="-xms128m -Xmx512m -XX:MaxPermSize=256m"

垃圾收集器主要有三种方式:

  • 串行收集
  • 并行收集
  • 并发低暂停收集

串行收集的特征如下:

  • Features Serial collector
  • Process Single thread is used for GC
  • GC pause High
  • Threading Single threaded
  • Application Small application (data less than 100 MB)
  • Advantage There is single thread communication

并行收集的特征如下:

  • Features Parallel collector
  • Process Parallel thread does minor GC
  • GC pause Less than Serial
  • Threading Multithreaded
  • Application Mid-large
  • Advantage Used in applications when peak performance is needed

并发收集的特征如下:

  • Features Concurrent collector
  • Process GC is done concurrently
  • GC pause Short pause
  • Threading Multithreaded
  • Application Mid-large
  • Advantage Used in applications when a response is needed

JVM的选项分为标准和非标准:

主要有以下选项:

  • Options Parameter Description
  • Behavioral Options -XX:+ScavengeBeforeFullGC Do young generation GC prior to a full GC
  • Behavioral Options --XX:-UseParallelGC Use parallel garbage collection for scavenges
  • Performance Options -XX:MaxNewSize=size Maximum size of new generation (in bytes)
  • Performance Options -XX:MaxPermSize=64m Size of the Permanent Generation (after exceeding Xmxvalue)
  • Performance Options -xms Minimum heap memory for the startup of Tomcat
  • Performance Options Xmx Maximum memory allocated to the instance
  • Performance Options -Xss Stack size for the heap
  • Debugging Options -XX:-CITime Prints time spent in the JIT Compiler
  • Debugging Options -XX:ErrorFile=./hs_err_pid<pid>.log If an error occurs, save the error data to this file
  • Debugging Options -XX:HeapDumpPath=./java_pid<pid>.hprof Path to the directory or filename for the heap dump
  • Debugging Options -XX:-HeapDumpOnOutOfMemoryError Dump the heap to the file whenjava.lang.OutOfMemoryError is thrown
  • Options Parameter Description
  • Debugging Options -XX:OnError="<cmd args>;<cmd args>" Run user-defined commands on Fatal error
  • Debugging Options -XX:OnOutOfMemoryError="<cmd args>; Run user-defined commands when an OutOfMemoryError is first thrown
  • Debugging Options -XX:-PrintClassHistogram Print a histogram of class instances on Ctrl-Break
  • Parameters displayed in the logs for GC
  • GC prints the output of the garbage collection to the stdout stream. At every garbage collection, the following five fields are printed:
  • [%T %B->%A(%C), %D]

  • %T: This is "GC" when the garbage collection is a scavenge, and "Full GC:" is performed, then scavenge collects live objects from the new generation only, whereas a full garbage collection collects objects from all spaces in the Java heap.
  • %B: It is the size of the Java heap used before the garbage collection, in KB.
  • %A: It is the size of the Java heap after the garbage collection, in KB.
  • %C: It is the current capacity of the entire Java heap, in KB.
  • %D: It is the duration of the collection in seconds.

  • SurvivorRatio
  • It is defined as a ratio of eden to the survivor space size. The default value is 8, meaning that eden is 8 times bigger than from and to, each. The Syntax for the SurvivorRatio is -XX:SurvivorRatio=<size>.
  • The following are some examples:
系统调优:
  • 建议选择64位系统
  • 文件尺寸限制
  • 打开连接限制
  • 页面尺寸

相关文章

今天小编给大家分享一下excel图案样式如何设置的相关知识点,...
这篇文章主要讲解了“win10设置过的壁纸如何删除”,文中的讲...
这篇“Xmanager怎么显示远程linux程序的图像”文章的知识点大...
今天小编给大家分享一下xmanager怎么连接linux的相关知识点,...
这篇“如何重置Linux云服务器的远程密码”文章的知识点大部分...
本篇内容介绍了“Linux云服务器手动配置DNS的方法是什么”的...