1. 程式人生 > >tomcat 性能調優

tomcat 性能調優

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位系統
  • 文件尺寸限制
  • 打開連接限制
  • 大頁面尺寸

tomcat 性能調優