JProfiler試用手記
所謂“工欲善其事,必先利其器”,好的工具確能起到事半工倍的作用。我用到的最多的就兩個JConsole 和JProfiler 。JConsole監(jiān)控系統(tǒng)內(nèi)存變化情況,如果有內(nèi)存溢出的話,垃圾回收將會呈現(xiàn)鋸齒狀。發(fā)現(xiàn)問題以后,使用JProfiler,在小壓力(或無壓力)的情況下監(jiān)控對象變化,定位內(nèi)存溢出原因。
JProfiler是一款Java的性能監(jiān)控工具??梢圆榭串?dāng)前應(yīng)用的對象、對象引用、內(nèi)存、CPU使用情況、線程、線程運行情況(阻塞、等待等),同時可以查找應(yīng)用內(nèi)存使用得熱點,即:哪個對象占用的內(nèi)存比較多;或者CPU熱點,即:哪兒方法占用的較大得CPU資源。我使用的是4.3.2版本,以前試用過3**版本,不過那個bug比較多,容易死,4**版本穩(wěn)定多了。
有了上面那些信息對于系統(tǒng)的調(diào)優(yōu)會有很大幫助。這里提供有幾篇文章供參考:獲取、介紹,簡單入門,使用JProfiler解決實際問題。這幾篇文章基本介紹了常見東西了,下面說點心得。
- JProfiler監(jiān)控是要消耗系統(tǒng)資源的,所以一般情況下不要用于性能測試時候的監(jiān)控。
- 如果要用于相對大壓力情況下,可以有選擇的打開監(jiān)控項,不用所有都打開。主要有兩個,一個是內(nèi)存監(jiān)控,打開的情況下可以查找內(nèi)存分配熱點。一個是CPU監(jiān)控,打開的情況下可以查看CPU使用熱點。
如圖所示,紅筆標(biāo)注部分。如果兩個都關(guān)閉的話,還是可以跑一定壓力的,同時還可以監(jiān)控對象數(shù)量。
- 個人認(rèn)為最好用的(也是用的最多的)是查詢當(dāng)前的對象的數(shù)量。數(shù)量監(jiān)控很重要,如果你使用了單例,那么你只會看到有一個對象存在,如果多了就說明程序有問題了。同樣,如果應(yīng)用進(jìn)行一系列操作,檢查一下該銷毀的對象是否還繼續(xù)存在,如果沒有釋放,就得考慮是否存在內(nèi)存溢出了。
- JProfiler還提供了一個比較好的檢查內(nèi)存溢出得工具。他可以查找某個對象的引用情況,即:當(dāng)你發(fā)現(xiàn)某個該釋放掉的對象沒有釋放,就可以看一下哪個實例在引用它,找到了根即找到了溢出點。
- 具體操作如下:在 “Memory Views”界面中右鍵選擇你要監(jiān)控的對象,選擇第一項“Take Heap Snapshot for Selection”,選擇完成后會進(jìn)入“Heap Walker”界面,界面下面提供幾個功能,選擇“References”即可 。如圖:
- JProfiler提供不同的觀察粒度,提供對類的監(jiān)控、對包的監(jiān)控、對J2EE組件的監(jiān)控,同時過濾器也比較好用,直接定位你關(guān)注的包或類即可。
- JProfiler的監(jiān)控可能與應(yīng)用之間存在一定時間差,所以有些時候需要等待刷新,才能顯示正確系統(tǒng)情況。
Windows客戶端的JProfiler遠(yuǎn)程監(jiān)控Linux上的Tomcat
1.測試環(huán)境
服務(wù)器:RedHat Linux 3.4.3-9.EL4(內(nèi)核版本 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.服務(wù)器端 JProfiler 安裝:
把 jprofiler_linux_4.3.2.sh 上傳到到服務(wù)器,假設(shè)路徑為 /opt/jprofiler
- # cd /opt/jprofiler
- # chmod +x *.sh
- # ./jprofiler_linux_4.3.2.sh -c
按照提示來安裝,提示都很簡單,不在多說。安裝路徑選擇 /opt/jprofiler4
注意,這里的 -c 意思是用字符方式來安裝,如果機(jī)器上沒有 X 則加上該參數(shù).
5.客戶端連接配置
1). 運行 JProfiler 。第一次打開會有向?qū)?,忽略它?/p>
2). 選擇 Session->Integration Wizard->New Remote Integratation
3). 選擇 On a remote computer;Platform of remote computer 選擇 Linux x86/AMD 64;Next
4). 輸入服務(wù)器 IP ;Next
5). 輸入服務(wù)器上的 jprofiler 的安裝路徑,如 /opt/jprofiler4 ;next
6). 選擇服務(wù)器的 JDK 環(huán)境,這里是:Sun,1.5.0,hotspot;next
7). 輸入端口:這里是默認(rèn)值 8849;next
8). 選擇啟動模式:這里選第一種 wait for a connection from the jprofiler GUI;next
9). 這里會列出需要在服務(wù)器端做的配置:
- Integration type: [Generic application]
- Selected JVM: Sun 1.5.0 (hotspot)
- 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.服務(wù)器端的配置
(1)修改系統(tǒng)環(huán)境配置文件 /etc/profile ,增加
- JPROFILER_HOME=/opt/jprofiler4/bin/linux-x86
- export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$JPROFILER_HOME
(2)修改TOMCAT啟動文件catalina.sh,添加-agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar 內(nèi)容到CATALINA_OPTS中;
“-agentlib:jprofilerti=port=8849 -Xbootclasspath/a:/opt/jprofiler4/bin/agent.jar ” 此內(nèi)容由客戶端軟件生成
7.Reboot Linux and startup Tomcat using startup.sh;
The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:
- JProfiler> Protocol version 23
- JProfiler> Using JVMTI
- JProfiler> 32-bit library
- JProfiler> Listening on port: 8849.
- JProfiler> Native library initialized
- JProfiler> Waiting for a connection from the JProfiler GUI
8.啟動客戶端軟件
點擊jprofiler菜單 session>start center>Open Session
Available session configurations中列出了剛才配置的連接,選中使用就OK了?。?/p>
9.The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:
- JProfiler> Using dynamic instrumentation
- JProfiler> Time measurement: elapsed time
- JProfiler> CPU profiling enabled
- JProfiler> Hotspot compiler enabled
- JProfiler> Starting org/apache/catalina/startup/Bootstrap
10.當(dāng)中斷JProfiler連接時
The log of tomcat which is $CATALINA_HOME/logs/catalina.out will show:
- JProfiler> Disconnected. Waiting for reconnection.
- JProfiler> Listening on port: 8849.
原文鏈接:http://hi.baidu.com/xuwanbest/blog/item/605f9f3335f05e46ac4b5f07.html
【編輯推薦】
- Java多線程的監(jiān)控分析工具(VisualVM)
- Java幾款性能分析工具的對比
- 棧的Java實現(xiàn)和棧的應(yīng)用舉例
- 實戰(zhàn)是硬道理:記Java技術(shù)面試
- Java設(shè)計模式:責(zé)任鏈