溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

tomcat 性能調(diào)優(yōu)

發(fā)布時(shí)間:2020-07-31 22:30:28 來源:網(wǎng)絡(luò) 閱讀:391 作者:aishangwei 欄目:建站服務(wù)器

性能調(diào)優(yōu):主要從以下幾個(gè)方面入手

  • 應(yīng)用代碼:假如代碼開發(fā)的不好,它會(huì)導(dǎo)致性能問題,比如數(shù)據(jù)庫的連接在該關(guān)閉的時(shí)候沒有適當(dāng)?shù)年P(guān)閉,將會(huì)導(dǎo)致應(yīng)用運(yùn)行慢。
  • 數(shù)據(jù)庫的調(diào)優(yōu):如果數(shù)據(jù)庫的響應(yīng)比較慢,那么應(yīng)用也肯定就會(huì)回應(yīng)的比較慢了。
  • JVM的調(diào)優(yōu):假如應(yīng)用需要較多的內(nèi)存來運(yùn)行,而你分配比較小的內(nèi)存,那么就會(huì)導(dǎo)致內(nèi)存溢出,因而也會(huì)導(dǎo)致性能問題。
  • 中間件服務(wù):比如我們在選擇消息隊(duì)列,緩存或者設(shè)計(jì)好時(shí),也會(huì)導(dǎo)致性能問題。
  • 基礎(chǔ)架構(gòu)和OS:比如,網(wǎng)絡(luò)丟包,系統(tǒng)配置不合理等。

適當(dāng)?shù)娜罩居涗浐捅O(jiān)控:日志和監(jiān)控有助于分析和排錯(cuò)。

Tomcat連接器的類型:

Java HTTP 連接器:

是基于HTTP協(xié)議,支持HTTP1.1,它使tomcat服務(wù)器扮演一個(gè)獨(dú)立 服務(wù)器和JSP/servlet功能的服務(wù)器。

Java AJP 連接器:

JAVA AJP是基于Apache JServ 協(xié)議的,該連接器常常是在你不想暴露自己的Java servlet容器到Internet.

APR(AJP/HTTP)連接器:

Apache Portable Runtime(APR)是在擴(kuò)容,性能和不同web服務(wù)器之間的兼容最好 的。它提供了比如OPENSSL,共享內(nèi)存,Unix大套接字等。

線程調(diào)優(yōu):

線程沲定義了web服務(wù)器連接請求連接的數(shù)量,可以定義兩種線程沲:一是共享沲,二是專用沲。該配置在TOMCAT_HOME/conf/server.xml文件中定義的。

共享線程沲:

假如你配置了四個(gè)連接器,那么你可以共享使用這個(gè)線程沲。
配置如下:

(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文件中進(jìn)行定義的
<Connector port="8443" protocol="HTTP/1.1"
SSLEnabled="true"
maxThreads="150"
scheme="https"
secure="true"
clientAuth="false" sslProtocol="TLS" />
下表是專用線程沲和共享線程沲經(jīng)常使用場景對比
  • Features Shared thread pool Dedicated thread pool
  • Number of users less High
  • Environment Development Production
  • Performance low Good
maxThreads:

默認(rèn)定義的最大線程沲是150個(gè),在生產(chǎn)環(huán)境中,可以根據(jù)服務(wù)器的性能來調(diào)下該參數(shù)。

maxKeepAlive:

也相當(dāng)于并發(fā)數(shù)一樣,默認(rèn)值是1,也就是相當(dāng)于關(guān)閉。

JVM的調(diào)優(yōu):

JMAP(內(nèi)存映射)

JMAP顯示共享JAVA虛擬機(jī)內(nèi)存信息,對查看共享內(nèi)存的狀態(tài)有用的。下面是一些常用選項(xiàng):
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,那么就執(zhí)行./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

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

  • 應(yīng)用的堆配置
  • 每個(gè)JVM組件的堆內(nèi)存利用率
  • 垃圾收集器使用的算法

堆內(nèi)存的配置:

在catalina.sh中JAVA_OPTS的選項(xiàng)。
JAVA_OPTS="-Xms128m -Xmx512m -XX:MaxPermSize=256m"

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

  • 串行收集
  • 并行收集
  • 并發(fā)低暫停收集

串行收集的特征如下:

  • 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

并發(fā)收集的特征如下:

  • 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的選項(xiàng)分為標(biāo)準(zhǔn)和非標(biāo)準(zhǔn):

主要有以下選項(xiàng):

  • 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:
系統(tǒng)調(diào)優(yōu):
  • 建議選擇64位系統(tǒng)
  • 文件尺寸限制
  • 打開連接限制
  • 大頁面尺寸
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI