1. 程式人生 > >JProfiler入門

JProfiler入門




JProfiler是一款Java的效能監控工具。可以檢視當前應用的物件、物件引用、記憶體、CPU使用情況、執行緒、執行緒執行情況(阻塞、等待等),同時可以查詢應用記憶體使用得熱點,這裡提供有幾篇文章供參考:獲取、介紹,簡單入門,使用JProfiler解決實際問題。這幾篇文章基本介紹了常見東西了,下面說點心得。

AD:

所謂“工欲善其事,必先利其器”,好的工具確能起到事半工倍的作用。我用到的最多的就兩個JConsole 和JProfiler 。JConsole監控系統記憶體變化情況,如果有記憶體溢位的話,垃圾回收將會呈現鋸齒狀。發現問題以後,使用JProfiler,在小壓力(或無壓力)的情況下監控物件變化,定位記憶體溢位原因。

JProfiler是一款Java的效能監控工具。可以檢視當前應用的物件、物件引用、記憶體、CPU使用情況、執行緒、執行緒執行情況(阻塞、等待等),同時可以查詢應用記憶體使用得熱點,即:哪個物件佔用的記憶體比較多;或者CPU熱點,即:哪兒方法佔用的較大得CPU資源。我使用的是4.3.2版本,以前試用過3**版本,不過那個bug比較多,容易死,4**版本穩定多了。

有了上面那些資訊對於系統的調優會有很大幫助。這裡提供有幾篇文章供參考:獲取、介紹,簡單入門,使用JProfiler解決實際問題。這幾篇文章基本介紹了常見東西了,下面說點心得。

  1. JProfiler監控是要消耗系統資源的,所以一般情況下不要用於效能測試時候的監控。
  2. 如果要用於相對大壓力情況下,可以有選擇的開啟監控項,不用所有都開啟。主要有兩個,一個是記憶體監控,開啟的情況下可以查詢記憶體分配熱點。一個是CPU監控,開啟的情況下可以檢視CPU使用熱點。

 

如圖所示,紅筆標註部分。如果兩個都關閉的話,還是可以跑一定壓力的,同時還可以監控物件數量。

  1. 個人認為最好用的(也是用的最多的)是查詢當前的物件的數量。數量監控很重要,如果你使用了單例,那麼你只會看到有一個物件存在,如果多了就說明程式有問題了。同樣,如果應用進行一系列操作,檢查一下該銷燬的物件是否還繼續存在,如果沒有釋放,就得考慮是否存在記憶體溢位了。
  2. JProfiler還提供了一個比較好的檢查記憶體溢位得工具。他可以查詢某個物件的引用情況,即:當你發現某個該釋放掉的物件沒有釋放,就可以看一下哪個例項在引用它,找到了根即找到了溢位點。
  3. 具體操作如下:在 “Memory Views”介面中右鍵選擇你要監控的物件,選擇第一項“Take Heap Snapshot for Selection”,選擇完成後會進入“Heap Walker”介面,介面下面提供幾個功能,選擇“References”即可 。如圖:

  4. JProfiler提供不同的觀察粒度,提供對類的監控、對包的監控、對J2EE元件的監控,同時過濾器也比較好用,直接定位你關注的包或類即可。
  5. JProfiler的監控可能與應用之間存在一定時間差,所以有些時候需要等待重新整理,才能顯示正確系統情況。

Windows客戶端的JProfiler遠端監控Linux上的Tomcat

1.測試環境

伺服器:RedHat Linux 3.4.3-9.EL4(核心版本 2.6.9-5.EL),Tomcat5.5.20,Sun JDK 1.5.0_09,JProfiler 4.3.2 for linux(安裝包:jprofiler_linux_4_3_2.sh)

客戶端:Windows XP,JProfiler 4.3.2 for windows(安裝包:jprofiler_windows_4_3_2.exe)

2.JProfiler軟體下載地址 http://www.ej-technologies.com/

3.客戶端 JProfiler 安裝 略

4.伺服器端 JProfiler 安裝:

把 jprofiler_linux_4.3.2.sh 上傳到到伺服器,假設路徑為 /opt/jprofiler


   
  1. # cd /opt/jprofiler  
  2. # chmod +x *.sh   
  3. # ./jprofiler_linux_4.3.2.sh -c 

按照提示來安裝,提示都很簡單,不在多說。安裝路徑選擇 /opt/jprofiler4

注意,這裡的 -c 意思是用字元方式來安裝,如果機器上沒有 X 則加上該引數.

5.客戶端連線配置

1). 執行 JProfiler 。第一次開啟會有嚮導,忽略它。

2). 選擇 Session->Integration Wizard->New Remote Integratation

3). 選擇 On a remote computer;Platform of remote computer 選擇 Linux x86/AMD 64;Next

4). 輸入伺服器 IP ;Next

5). 輸入伺服器上的 jprofiler 的安裝路徑,如 /opt/jprofiler4 ;next

6). 選擇伺服器的 JDK 環境,這裡是:Sun,1.5.0,hotspot;next

7). 輸入埠:這裡是預設值 8849;next

8). 選擇啟動模式:這裡選第一種 wait for a connection from the jprofiler GUI;next

9). 這裡會列出需要在伺服器端做的配置:


   
  1. Integration type: [Generic application]  
  2. Selected JVM: Sun 1.5.0 (hotspot)  
  3. Startup mode: Wait for JProfiler GUI 

(1) Please insert

-agentlib:jprofilerti=port=8849  -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar

into the start command of your remote application right after the java command.

(2) Please add

/opt/jprofiler4/bin/linux-x86

to the environment variable LD_LIBRARY_PATH.

A remote session named Remote application on 192.168.40.15 will be created that connects to a running instance of the remote application that is started with the modified start command.

6.伺服器端的配置

(1)修改系統環境配置檔案 /etc/profile ,增加


   
  1. JPROFILER_HOME=/opt/jprofiler4/bin/linux-x86  
  2. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JPROFILER_HOME 

(2)修改TOMCAT啟動檔案catalina.sh,新增-agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar 內容到CATALINA_OPTS中;

“-agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar ” 此內容由客戶端軟體生成

CATALINA_OPTS="$CATALINA_OPTS -Xms128m -Xmx128m $JPDA_OPTS -agentlib:jprofilerti=port=8849   -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar"

7.Reboot Linux and startup Tomcat using startup.sh;

The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:


   
  1. JProfiler> Protocol version 23  
  2. JProfiler> Using JVMTI  
  3. JProfiler> 32-bit library  
  4. JProfiler> Listening on port: 8849.  
  5. JProfiler> Native library initialized  
  6. JProfiler> Waiting for a connection from the   JProfiler GUI  

8.啟動客戶端軟體

點選jprofiler選單 session>start center>Open Session

Available session configurations中列出了剛才配置的連線,選中使用就OK了!!

9.The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:


   
  1. JProfiler> Using dynamic instrumentation  
  2. JProfiler> Time measurement: elapsed time  
  3. JProfiler> CPU profiling enabled  
  4. JProfiler> Hotspot compiler enabled  
  5. JProfiler> Starting org/apache/catalina/startup/Bootstrap  

10.當中斷JProfiler連線時

The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:


   
  1. JProfiler> Disconnected. Waiting for reconnection.  
  2. JProfiler> Listening on port: 8849. 

原文連結:http://hi.baidu.com/xuwanbest/blog/item/605f9f3335f05e46ac4b5f07.html