JProfiler試用手記

JProfiler是一款Java的性能監控工具。可以查看當前應用的對象、對象引用、內存、CPU使用情況、線程、線程運行情況(阻塞、等待等),同時可以查找應用內存使用得熱點,這裏提供有幾篇文章供參考:獲取、介紹,簡單入門,使用JProfiler解決實際問題。這幾篇文章基本介紹了常見東西了,下面說點心得。

AD:51CTO學院:IT精品課程在線看!

所謂“工欲善其事,必先利其器”,好的工具確能起到事半工倍的作用。我用到的最多的就兩個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. 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章